Skip to content

Commit

Permalink
Add HTML representation for bills
Browse files Browse the repository at this point in the history
  • Loading branch information
lukec committed Jan 1, 2010
1 parent 4ad6f0c commit 0034dd7
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app.psgi
Expand Up @@ -27,6 +27,8 @@ my $router = router {
# Bills
match '/parliaments/{parliament}-{session}/bills' =>
to { controller => 'ParlAPI::Bills', action => 'pretty_list' };
match '/parliaments/{parliament}-{session}/bills/{billname}' =>
to { controller => 'ParlAPI::Bills', action => 'show_bill' };
};
my $app = Plack::App::HTTP::Router->new({ router => $router} )->to_app;

Expand Down
25 changes: 25 additions & 0 deletions lib/ParlAPI/Bills.pm
Expand Up @@ -22,6 +22,31 @@ sub pretty_list {
);
}

sub show_bill {
my $self = shift;
my $req = shift;
my $params = shift;

my $parl = $self->model->get_parliament(%$params);
return $self->render('unknown_parliament.html', $params) unless $parl;

my $bill_name = $params->{billname};
unless ($bill_name) {
return $self->render('unknown_bill.html', {
bill_name => $bill_name,
parliament => $parl,
});
}
my $bill = $self->model->get_bill($parl, $bill_name);

return $self->render('bill.html',
{
parliament => $parl,
bill => $bill,
},
);
}


__PACKAGE__->meta->make_immutable;
1;
1 change: 1 addition & 0 deletions lib/ParlAPI/DB.pm
Expand Up @@ -10,6 +10,7 @@ sub sql_execute {
my $statement = shift;
my @bind = @_;

# warn "SQL: $statement (@bind)";
my $sth = $self->dbh->prepare($statement);
$sth->execute(@bind)
|| die "SQL execute failed: " . $sth->errstr;
Expand Down
34 changes: 29 additions & 5 deletions lib/ParlAPI/Model.pm
Expand Up @@ -12,11 +12,18 @@ sub get_parliament {
my $self = shift;
my %p = @_;

my $sth = $self->db->sql_execute(q{
SELECT * FROM parliament
WHERE parl = ? AND session = ?
}, $p{parliament}, $p{session},
);
my ($where, @bind);
if ($p{parl_id}) {
$where = 'parl_id = ?';
push @bind, $p{parl_id};
}
elsif ($p{parliament} and $p{session}) {
$where = 'parl = ? AND session = ?';
push @bind, $p{parliament}, $p{session};
}
else { die "Look ups by parl_id or parliament & session only" }

my $sth = $self->db->sql_execute(qq{SELECT * FROM parliament WHERE $where}, @bind);
my $result = $sth->fetchall_arrayref({});
return undef unless $result->[0];
return ParlAPI::Model::Parliament->new($result->[0]);
Expand Down Expand Up @@ -44,6 +51,23 @@ sub get_member {
return ParlAPI::Model::Member->new($result->[0]);
}


sub get_bill {
my $self = shift;
my $parl = shift;
my $bill_name = uc shift;

my $sth = $self->db->sql_execute(qq{
SELECT *
FROM bill
WHERE parl_id = ? AND name = ?
}, $parl->parl_id, $bill_name
);
my $result = $sth->fetchall_arrayref({});
return undef unless $result->[0];
return ParlAPI::Model::Bill->new($result->[0]);
}

sub delete_member {
my $self = shift;
my $member_id = shift;
Expand Down
35 changes: 31 additions & 4 deletions lib/ParlAPI/Model/Bill.pm
@@ -1,18 +1,20 @@
package ParlAPI::Model::Bill;
use Moose;
use ParlAPI::Model;
use namespace::clean -except => 'meta';

with 'ParlAPI::Model::Collection';

has 'bill_id' => (is => 'ro', isa => 'Int', required => 1);
has 'name' => (is => 'ro', isa => 'Str', required => 1);
has 'parl_id' => (is => 'ro', isa => 'Int', required => 1);
has 'parliament' => (is => 'ro', isa => 'ParlAPI::Model::Parliament', lazy_build => 1);
has 'sponsor_id' => (is => 'ro', isa => 'Maybe[Int]');
has 'summary' => (is => 'ro', isa => 'Str', required => 1);
has 'sponsor' => (is => 'ro', isa => 'ParlAPI::Model::Member', lazy_build => 1);
has 'sponsor' => (is => 'ro', isa => 'Maybe[ParlAPI::Model::Member]', lazy_build => 1);
has 'sponsor_title' => (is => 'ro', isa => 'Str', required => 1);
has 'links' =>
(is => 'rw', isa => 'ArrayRef[HashRef]', default => sub { [] });
has 'links' => (is => 'rw', isa => 'ArrayRef[HashRef]', lazy_build => 1);
has 'official_url' => (is => 'ro', isa => 'Str', lazy_build => 1);

sub table { 'bill' }

Expand All @@ -23,7 +25,7 @@ around 'All' => sub {

sub _build_sponsor {
my $self = shift;
return $self->get_member($self->sponsor_id);
return ParlAPI::Model->get_member($self->sponsor_id);
}

sub insert {
Expand All @@ -48,5 +50,30 @@ sub insert {
}
}

sub _build_official_url {
my $self = shift;
my $parl = $self->parliament;
(my $name = $self->name) =~ s/-//;

return q{http://www2.parl.gc.ca/HouseBills/BillsGovernment.aspx?Language=E}
. q{&Mode=1&Parl=} . $parl->parliament . q{&Ses=} . $parl->session
. q{#} . $name;
}

sub _build_parliament {
my $self = shift;
return ParlAPI::Model->get_parliament(parl_id => $self->parl_id);
}

sub _build_links {
my $self = shift;
my $db = ParlAPI::Model->db;

my $sth = $db->sql_execute(
q{SELECT link_name AS name, link_href AS href
FROM bill_links WHERE bill_id = ?}, $self->bill_id);
return $sth->fetchall_arrayref({});
}

__PACKAGE__->meta->make_immutable;
1;
27 changes: 27 additions & 0 deletions templates/bill.html
@@ -0,0 +1,27 @@
[% WRAPPER wrapper.tt2 %]
<h2>House of Commons Bills - [% bill.name %]</h2>
<strong>Name:</strong> [% bill.name %]<br />
<strong>Parliament:</strong> <a href="[% parliament.url %]">[%
parliament.name %]</a><br />
<strong>Summary:</strong> [% bill.summary %]<br />
<strong>Sponsor title:</strong> [% bill.sponsor_title %]<br />
[% IF bill.sponsor_id %]
<strong>Sponsor ID:</strong>
<a href="http://webinfo.parl.gc.ca/MembersOfParliament/ProfileMP.aspx?Key=[% bill.sponsor_id %]&Language=E">[% bill.sponsor_id %]</a><br />
[% END %]
[% IF bill.sponsor %]
<strong>Sponsor:</strong>[% bill.sponsor.name %]<br />
[% END %]
[% IF bill.links.size > 0 %]
<ul>
[% FOR link IN bill.links %]
<li>
<a href="[% link.href %]">[% link.name %]</a>
</li>
[% END %]
</ul>
[% END %]
<p><a href="[% bill.official_url %]">Official Bill URL</a><br /></p>
<br />
<a href="[% parliament.url %]/bills">Back to bill list</a>
[% END %]
2 changes: 1 addition & 1 deletion templates/member.html
Expand Up @@ -22,5 +22,5 @@ <h2>Members of Parliament - [% member.name %]</h2>
</tr>
</table>
<br />
<a href="/members">Back to /members</a>
<a href="/">Back to /</a>
[% END %]
4 changes: 4 additions & 0 deletions templates/unknown_bill.html
@@ -0,0 +1,4 @@
[% WRAPPER wrapper.tt2 %]
<h2>Unknown Bill</h2>
Sorry, "[% bill_name %]" is an unknown bill for the [% parliament.name %].
[% END %]

0 comments on commit 0034dd7

Please sign in to comment.