Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed the model layer to be something approaching sane.

  • Loading branch information...
commit 8931e52a13dbe367627c85df4b4398bad7101708 1 parent 2a0a01a
@davorg authored
View
147 PoliticalWeb/lib/PoliticalWeb.pm
@@ -2,15 +2,9 @@ package PoliticalWeb;
use Dancer ':syntax';
use Dancer::Plugin::DBIC;
use Dancer::Plugin::Cache::CHI;
-use WebService::TWFY::API;
use Data::Dumper;
-use Encode qw[encode decode];
-my $twfy = WebService::TWFY::API->new({
- key => $ENV{TWFY_KEY}
-}) or die $!;
-my $con_rs = schema->resultset('Constituency');
-my $mp_rs = schema->resultset('Mp');
+use PoliticalWeb::Constituency;
our $VERSION = '0.1';
@@ -19,8 +13,8 @@ check_page_cache;
get '/' => sub {
return template 'index' unless keys %{ +params };
- if (params->{pc}) {
- params->{constit} = get_const_name_from_pc(params->{pc});
+ if (my $pc = params->{pc}) {
+ params->{constit} = PoliticalWeb::Constituency->name_from_postcode($pc);
}
if (params->{constit}) {
redirect 'http://' . request->host . '/constituency/' . params->{constit};
@@ -40,8 +34,8 @@ get '/constituency/?' => sub {
};
get '/constituency/:constname' => sub {
- my $constit = get_const(params->{constname});
- my $mp = get_mp($constit) if $constit;
+ my $constit = PoliticalWeb::Constituency->new_from_name(params->{constname});
+ my $mp = $constit->get_mp if $constit;
if ($constit && $mp) {
my $page = template 'constituency', {
@@ -56,6 +50,7 @@ get '/constituency/:constname' => sub {
};
get '/constituencies/' => sub {
+ my $con_rs = schema->resultset('Constituency');
my $page = template 'constituencies/index', {
constits => [ $con_rs->search({}, { order_by => 'name'} )->all ]
};
@@ -63,134 +58,4 @@ get '/constituencies/' => sub {
return $page;
};
-sub get_const {
- if ($_[0] =~ /Ynys M/) {
- $_[0] = 'Ynys Môn';
- }
-
- return get_const_from_cache(@_)
- || get_const_from_db(@_)
- || get_const_from_twfy(@_);
-}
-
-sub get_const_from_cache {
- debug "get_const_from_cache: @_";
-
- return cache_get "C:$_[0]";
-}
-
-sub get_const_from_db {
- debug "get_const_from_db: @_";
- return;
-}
-
-sub get_const_from_twfy {
- debug "get_const_from_twfy: @_";
-
- my ($con, $ret);
-
- $ret = $twfy->query( 'getConstituency', {
- name => encode('iso-8859-1', $_[0]),
- postcode => '',
- });
-
- # debug Dumper $ret;
-
- if ($ret->{is_success}) {
- $con = from_json(encode('utf8', $ret->{results}));
- return if $con->{error};
- # debug "Setting cache key - 'C:$_[0]'";
- # debug 'Setting cache val - ' . Dumper(from_json(encode('utf8', $ret->{results})));
- cache_set "C:$_[0]", $con, 60*60*60;
- } else {
- return;
- }
-
- # debug Dumper $con;
-
- $con->{db} = $con_rs->find_or_create({ name => $con->{name} });
-
- return $con;
-}
-
-sub get_const_name_from_pc {
- my $ret = $twfy->query( 'getConstituency', {
- postcode => $_[0],
- });
-
- if ($ret->{is_success}) {
- my $constit = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
- # debug Dumper $constit;
- return if $constit->{error};
- cache_set 'C:' . $constit->{name}, $constit, 60*60*60;
- return $constit->{name};
- }
-
- return;
-}
-
-sub get_mp {
- my $constit = shift;
- my $mp = get_mp_from_cache($constit->{name})
- || get_mp_from_db($constit->{name})
- || get_mp_from_twfy($constit->{name});
-
- $mp->{db} = $constit->{db}->mp;
- return $mp;
-}
-
-sub get_mp_from_cache {
- debug "get_mp_from_cache: @_";
-
- return cache_get "M:$_[0]";
-}
-
-sub get_mp_from_db {
- debug "get_mp_from_db: @_";
- return;
-}
-
-sub get_mp_from_twfy {
- debug "get_mp_from_twfy: @_";
- my $mp;
- my $ret = $twfy->query( 'getMP', {
- constituency => encode('iso-8859-1', $_[0]),
- });
-
- # debug Dumper $ret;
-
- if ($ret->{is_success}) {
- $mp = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
- return if $mp->{error};
-
- # debug Dumper $mp;
-
- my $ret = $twfy->query( 'getMPInfo', {
- id => $mp->{person_id},
- });
-
- if ($ret->{is_success}) {
- $mp->{extra} = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
- }
-
- cache_set "M:$_[0]", $mp, 60*60*60;
- } else {
- return;
- }
-
- # debug "TWFY: MP is " . Dumper $mp;
-
- $mp->{db} = schema->resultset('Mp')->find_or_create({
- mp_name => $mp->{full_name},
- twfy_id => $mp->{person_id},
- });
-
- my ($con_db) = schema->resultset('Constituency')->find_or_create({
- name => $_[0],
- });
- $con_db->update({ mp => $mp->{db}->id });
-
- return $mp;
-}
-
true;
View
104 PoliticalWeb/lib/PoliticalWeb/Constituency.pm
@@ -0,0 +1,104 @@
+package PoliticalWeb::Constituency;
+
+use strict;
+use warnings;
+use 5.010;
+
+use Moose;
+use WebService::TWFY::API;
+use Dancer ':syntax';
+use Dancer::Plugin::DBIC;
+use Dancer::Plugin::Cache::CHI;
+use Encode qw[encode decode];
+
+use PoliticalWeb::Schema::Result::Constituency;
+use PoliticalWeb::Mp;
+
+my $twfy_query = WebService::TWFY::API->new({
+ key => $ENV{TWFY_KEY}
+}) or die $!;
+
+has twfy => (
+ is => 'ro',
+ isa => 'HashRef',
+);
+
+has db => (
+ is => 'rw',
+ isa => 'PoliticalWeb::Schema::Result::Constituency',
+);
+
+sub name_from_postcode {
+ my $class = shift;
+ my $postcode = shift;
+
+ my $ret = $twfy_query->query( 'getConstituency', {
+ postcode => $postcode,
+ });
+
+ if ($ret->{is_success}) {
+ my $constit = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
+ # debug Dumper $constit;
+ return if $constit->{error};
+ cache_set 'C:' . $constit->{name}, $constit, 60*60*60;
+ return $constit->{name};
+ }
+
+ return;
+}
+
+sub new_from_name {
+ my $class = shift;
+ my $name = shift;
+
+ if ($name =~ /Ynys M/) {
+ $name = 'Ynys Mon';
+ }
+
+ my $twfy = _get_from_cache($name) || _get_from_twfy($name);
+ my $db = _get_from_db($name);
+
+ return $class->new({ twfy => $twfy, db => $db });
+}
+
+sub _get_from_cache {
+ return cache_get "C:$_[0]";
+}
+
+sub _get_from_twfy {
+ my $name = shift;
+
+ my $ret = $twfy_query->query( 'getConstituency', {
+ name => encode('iso-8859-1', $name),
+ postcode => '',
+ });
+
+ # debug Dumper $ret;
+
+ if ($ret->{is_success}) {
+ my $con = from_json(encode('utf8', $ret->{results}));
+ return if $con->{error};
+ # debug "Setting cache key - 'C:$_[0]'";
+ # debug 'Setting cache val - ' . Dumper(from_json(encode('utf8', $ret->{results})));
+ cache_set "C:$name", $con, 60*60*60;
+ } else {
+ return;
+ }
+}
+
+sub _get_from_db {
+ my $name = shift;
+ return schema->resultset('Constituency')->find_or_create({ name => $name });
+}
+
+sub get_mp {
+ my $self = shift;
+
+ my $mp = PoliticalWeb::Mp->new_from_constituency_name($self->twfy->{name});
+
+ $self->db->update({ mp => $mp->db->id }) if $mp;
+
+ return $mp;
+}
+
+1;
View
82 PoliticalWeb/lib/PoliticalWeb/Mp.pm
@@ -0,0 +1,82 @@
+package PoliticalWeb::Mp;
+
+use strict;
+use warnings;
+use 5.010;
+
+use Moose;
+use WebService::TWFY::API;
+use Dancer ':syntax';
+use Dancer::Plugin::DBIC;
+use Dancer::Plugin::Cache::CHI;
+use Encode qw[encode decode];
+
+my $twfy_query = WebService::TWFY::API->new({
+ key => $ENV{TWFY_KEY}
+}) or die $!;
+
+has twfy => (
+ is => 'ro',
+ isa => 'HashRef',
+);
+
+has db => (
+ is => 'rw',
+ isa => 'PoliticalWeb::Schema::Result::Mp',
+);
+
+sub new_from_constituency_name {
+ my $class = shift;
+ my $constit_name = shift;
+
+ my $twfy = _get_from_cache($constit_name) || _get_from_twfy($constit_name);
+ my $db = _get_from_db($constit_name, $twfy);
+
+ return $class->new({ twfy => $twfy, db => $db });
+}
+
+sub _get_from_cache {
+ return cache_get "M:$_[0]";
+}
+
+sub _get_from_twfy {
+ my $constit_name = shift;
+
+ my $mp;
+ my $ret = $twfy_query->query( 'getMP', {
+ constituency => encode('iso-8859-1', $constit_name),
+ });
+
+ # debug Dumper $ret;
+
+ if ($ret->{is_success}) {
+ $mp = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
+ return if $mp->{error};
+
+ # debug Dumper $mp;
+
+ my $ret = $twfy_query->query( 'getMPInfo', {
+ id => $mp->{person_id},
+ });
+
+ if ($ret->{is_success}) {
+ $mp->{extra} = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
+ }
+
+ cache_set "M:$constit_name", $mp, 60*60*60;
+ } else {
+ return;
+ }
+}
+
+sub _get_from_db {
+ my $constit_name = shift;
+ my $twfy = shift;
+
+ return schema->resultset('Mp')->find_or_create({
+ mp_name => $twfy->{full_name},
+ twfy_id => $twfy->{person_id},
+ });
+}
+
+1;
View
24 PoliticalWeb/views/constituency.tt
@@ -13,14 +13,14 @@ $(document).ready(function () {
</script>
[% META title = 'Constituency' -%]
<div class="span10 media">
-[% IF mp.image %]
+[% IF mp.twfy.image %]
<a class="pull-left" href="#">
- <img class="media-object" src="http://www.theyworkforyou.com/[% mp.image %]" alt="[% mp.full_name %]" />
+ <img class="media-object" src="http://www.theyworkforyou.com/[% mp.twfy.image %]" alt="[% mp.twfy.full_name %]" />
</a>
[% END %]
<div class="media-body">
- <h2>[% constit.name %]</h2>
- <h3>[% mp.full_name %] ([% mp.party %])</h3>
+ <h2>[% constit.twfy.name %]</h2>
+ <h3>[% mp.twfy.full_name %] ([% mp.twfy.party %])</h3>
</div>
</div>
<div class="span6">
@@ -38,20 +38,20 @@ $(document).ready(function () {
<div class="tab-content">
<div class="tab-pane active" id="links">
<ul>
-[% IF mp.extra.wikipedia_url %]
- <li><a href="[% mp.extra.wikipedia_url %]">Wikipedia entry</a></li>
+[% IF mp.twfy.extra.wikipedia_url %]
+ <li><a href="[% mp.twfy.extra.wikipedia_url %]">Wikipedia entry</a></li>
[% END %]
- <li><a href="http://theyworkforyou.com/mp/?m=[% mp.member_id %]">They Work For You</a></li>
- <li><a href="http://www.theyworkforyou.com/search/?pid=[% mp.person_id %]">Recent parliamentary appearances</a> <a href="http://www.theyworkforyou.com/search/rss/?s=speaker%3A[% mp.person_id %]"><img src="/feed_sm.png" alt="web feed" /></a></li>
- <li><a href="http://publicwhip.org.uk/mp.php?constituency=[% constit.name.replace(' ', '+') %]">Voting record from Public Whip</a></li>
- <li><a href="http://www.edms.org.uk/mps/[% mp.person_id %]/">Early Day Motions</a> <a href="http://www.edms.org.uk/mps/[% mp.person_id %]/index.rss"><img src="/feed_sm.png" alt="web feed" /></a></li>
- <li><a href="http://www.guardian.co.uk/politics/constituency/[% constit.guardian_id %]">Guardian constituency page</a></li>
+ <li><a href="http://theyworkforyou.com/mp/?m=[% mp.twfy.member_id %]">They Work For You</a></li>
+ <li><a href="http://www.theyworkforyou.com/search/?pid=[% mp.twfy.person_id %]">Recent parliamentary appearances</a> <a href="http://www.theyworkforyou.com/search/rss/?s=speaker%3A[% mp.twfy.person_id %]"><img src="/feed_sm.png" alt="web feed" /></a></li>
+ <li><a href="http://publicwhip.org.uk/mp.php?constituency=[% constit.twfy.name.replace(' ', '+') %]">Voting record from Public Whip</a></li>
+ <li><a href="http://www.edms.org.uk/mps/[% mp.twfy.person_id %]/">Early Day Motions</a> <a href="http://www.edms.org.uk/mps/[% mp.twfy.person_id %]/index.rss"><img src="/feed_sm.png" alt="web feed" /></a></li>
+ <li><a href="http://www.guardian.co.uk/politics/constituency/[% constit.twfy.guardian_id %]">Guardian constituency page</a></li>
[% FOREACH link IN mp.db.mp_links -%]
<li><a href="[% link.url %]">[% link.description %]</li>
[% END -%]
[% IF mp.guardian_aristotle_id %]
- <li><a href="http://www.guardian.co.uk/politics/person/[% mp.guardian_aristotle_id %]">Guardian MP page</a></li>
+ <li><a href="http://www.guardian.co.uk/politics/person/[% mp.twfy.guardian_aristotle_id %]">Guardian MP page</a></li>
[% END %]
[% FOREACH link IN constit.db.constituency_links -%]
<li><a href="[% link.url %]">[% link.description %]</li>
Please sign in to comment.
Something went wrong with that request. Please try again.