Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fixed the model layer to be something approaching sane.

  • Loading branch information...
commit 8931e52a13dbe367627c85df4b4398bad7101708 1 parent 2a0a01a
Dave Cross authored July 23, 2013
147  PoliticalWeb/lib/PoliticalWeb.pm
@@ -2,15 +2,9 @@ package PoliticalWeb;
2 2
 use Dancer ':syntax';
3 3
 use Dancer::Plugin::DBIC;
4 4
 use Dancer::Plugin::Cache::CHI;
5  
-use WebService::TWFY::API;
6 5
 use Data::Dumper;
7  
-use Encode qw[encode decode];
8 6
 
9  
-my $twfy = WebService::TWFY::API->new({
10  
-  key => $ENV{TWFY_KEY}
11  
-}) or die $!;
12  
-my $con_rs = schema->resultset('Constituency');
13  
-my $mp_rs  = schema->resultset('Mp');
  7
+use PoliticalWeb::Constituency;
14 8
 
15 9
 our $VERSION = '0.1';
16 10
 
@@ -19,8 +13,8 @@ check_page_cache;
19 13
 get '/' => sub {
20 14
   return template 'index' unless keys %{ +params };
21 15
 
22  
-  if (params->{pc}) {
23  
-    params->{constit} = get_const_name_from_pc(params->{pc});
  16
+  if (my $pc = params->{pc}) {
  17
+    params->{constit} = PoliticalWeb::Constituency->name_from_postcode($pc);
24 18
   }
25 19
   if (params->{constit}) {
26 20
     redirect 'http://' . request->host . '/constituency/' . params->{constit};
@@ -40,8 +34,8 @@ get '/constituency/?' => sub {
40 34
 };
41 35
 
42 36
 get '/constituency/:constname' => sub {
43  
-  my $constit = get_const(params->{constname});
44  
-  my $mp      = get_mp($constit) if $constit;
  37
+  my $constit = PoliticalWeb::Constituency->new_from_name(params->{constname});
  38
+  my $mp      = $constit->get_mp if $constit;
45 39
 
46 40
   if ($constit && $mp) {
47 41
     my $page = template 'constituency', {
@@ -56,6 +50,7 @@ get '/constituency/:constname' => sub {
56 50
 };
57 51
 
58 52
 get '/constituencies/' => sub {
  53
+  my $con_rs = schema->resultset('Constituency');
59 54
   my $page = template 'constituencies/index', {
60 55
     constits => [ $con_rs->search({}, { order_by => 'name'} )->all ]
61 56
   };
@@ -63,134 +58,4 @@ get '/constituencies/' => sub {
63 58
   return $page;
64 59
 };
65 60
 
66  
-sub get_const {
67  
-  if ($_[0] =~ /Ynys M/) {
68  
-    $_[0] = 'Ynys Môn';
69  
-  }
70  
-
71  
-  return get_const_from_cache(@_)
72  
-    || get_const_from_db(@_)
73  
-    || get_const_from_twfy(@_);
74  
-}
75  
-
76  
-sub get_const_from_cache {
77  
-  debug "get_const_from_cache: @_";
78  
-
79  
-  return cache_get "C:$_[0]";
80  
-}
81  
-
82  
-sub get_const_from_db {
83  
-  debug "get_const_from_db: @_";
84  
-  return;
85  
-}
86  
-
87  
-sub get_const_from_twfy {
88  
-  debug "get_const_from_twfy: @_";
89  
-
90  
-  my ($con, $ret);
91  
-
92  
-  $ret = $twfy->query( 'getConstituency', {
93  
-    name => encode('iso-8859-1', $_[0]),
94  
-    postcode => '',
95  
-  });
96  
-
97  
-  # debug Dumper $ret;
98  
-
99  
-  if ($ret->{is_success}) {
100  
-    $con = from_json(encode('utf8', $ret->{results}));
101  
-    return if $con->{error};
102  
-    # debug "Setting cache key - 'C:$_[0]'";
103  
-    # debug 'Setting cache val - ' . Dumper(from_json(encode('utf8', $ret->{results})));
104  
-    cache_set "C:$_[0]", $con, 60*60*60;
105  
-  } else {
106  
-    return;
107  
-  }
108  
-
109  
-  # debug Dumper $con;
110  
-  
111  
-  $con->{db} = $con_rs->find_or_create({ name => $con->{name} });
112  
-  
113  
-  return $con;
114  
-}
115  
-
116  
-sub get_const_name_from_pc {
117  
-  my $ret = $twfy->query( 'getConstituency', {
118  
-    postcode => $_[0],
119  
-  });
120  
-    
121  
-  if ($ret->{is_success}) {
122  
-    my $constit = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
123  
-    # debug Dumper $constit;
124  
-    return if $constit->{error};
125  
-    cache_set 'C:' . $constit->{name}, $constit, 60*60*60;
126  
-    return $constit->{name};
127  
-  }
128  
-  
129  
-  return;
130  
-}
131  
-
132  
-sub get_mp {
133  
-  my $constit = shift;
134  
-  my $mp = get_mp_from_cache($constit->{name})
135  
-    || get_mp_from_db($constit->{name})
136  
-    || get_mp_from_twfy($constit->{name});
137  
-
138  
-  $mp->{db} = $constit->{db}->mp;
139  
-  return $mp;
140  
-}
141  
-
142  
-sub get_mp_from_cache {
143  
-  debug "get_mp_from_cache: @_";
144  
-
145  
-  return cache_get "M:$_[0]";
146  
-}
147  
-
148  
-sub get_mp_from_db {
149  
-  debug "get_mp_from_db: @_";
150  
-  return;
151  
-}
152  
-
153  
-sub get_mp_from_twfy {
154  
-  debug "get_mp_from_twfy: @_";
155  
-  my $mp;
156  
-  my $ret = $twfy->query( 'getMP', {
157  
-    constituency => encode('iso-8859-1', $_[0]),
158  
-  });
159  
-
160  
-  # debug Dumper $ret;
161  
-
162  
-  if ($ret->{is_success}) {
163  
-    $mp = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
164  
-    return if $mp->{error};
165  
-
166  
-    # debug Dumper $mp;
167  
-
168  
-    my $ret = $twfy->query( 'getMPInfo', {
169  
-      id => $mp->{person_id},
170  
-    });
171  
-    
172  
-    if ($ret->{is_success}) {
173  
-      $mp->{extra} = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));   
174  
-    }
175  
-
176  
-    cache_set "M:$_[0]", $mp, 60*60*60;
177  
-  } else {
178  
-    return;
179  
-  }
180  
-
181  
-  # debug "TWFY: MP is " . Dumper $mp;
182  
-
183  
-  $mp->{db} = schema->resultset('Mp')->find_or_create({
184  
-    mp_name => $mp->{full_name},
185  
-    twfy_id => $mp->{person_id},
186  
-  });
187  
-
188  
-  my ($con_db) = schema->resultset('Constituency')->find_or_create({
189  
-    name => $_[0],
190  
-  });
191  
-  $con_db->update({ mp => $mp->{db}->id });
192  
-
193  
-  return $mp;
194  
-}
195  
-
196 61
 true;
104  PoliticalWeb/lib/PoliticalWeb/Constituency.pm
... ...
@@ -0,0 +1,104 @@
  1
+package PoliticalWeb::Constituency;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+use Moose;
  8
+use WebService::TWFY::API;
  9
+use Dancer ':syntax';
  10
+use Dancer::Plugin::DBIC;
  11
+use Dancer::Plugin::Cache::CHI;
  12
+use Encode qw[encode decode];
  13
+
  14
+use PoliticalWeb::Schema::Result::Constituency;
  15
+use PoliticalWeb::Mp;
  16
+
  17
+my $twfy_query = WebService::TWFY::API->new({
  18
+  key => $ENV{TWFY_KEY}
  19
+}) or die $!;
  20
+
  21
+has twfy => (
  22
+  is   => 'ro',
  23
+  isa  => 'HashRef',
  24
+);
  25
+ 
  26
+has db => (
  27
+  is   => 'rw',
  28
+  isa  => 'PoliticalWeb::Schema::Result::Constituency',
  29
+);
  30
+
  31
+sub name_from_postcode {
  32
+  my $class = shift;
  33
+  my $postcode = shift;
  34
+
  35
+  my $ret = $twfy_query->query( 'getConstituency', {
  36
+    postcode => $postcode,
  37
+  });
  38
+    
  39
+  if ($ret->{is_success}) {
  40
+    my $constit = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
  41
+    # debug Dumper $constit;
  42
+    return if $constit->{error};
  43
+    cache_set 'C:' . $constit->{name}, $constit, 60*60*60;
  44
+    return $constit->{name};
  45
+  }
  46
+  
  47
+  return;
  48
+}
  49
+
  50
+sub new_from_name {
  51
+  my $class = shift;
  52
+  my $name  = shift;
  53
+
  54
+  if ($name =~ /Ynys M/) {
  55
+    $name = 'Ynys Mon';
  56
+  }
  57
+
  58
+  my $twfy = _get_from_cache($name) || _get_from_twfy($name);
  59
+  my $db   = _get_from_db($name);
  60
+
  61
+  return $class->new({ twfy => $twfy, db => $db });
  62
+}
  63
+
  64
+sub _get_from_cache {
  65
+  return cache_get "C:$_[0]";
  66
+}
  67
+
  68
+sub _get_from_twfy {
  69
+  my $name = shift;
  70
+
  71
+  my $ret = $twfy_query->query( 'getConstituency', {
  72
+    name => encode('iso-8859-1', $name),
  73
+    postcode => '',
  74
+  });
  75
+
  76
+  # debug Dumper $ret;
  77
+
  78
+  if ($ret->{is_success}) {
  79
+    my $con = from_json(encode('utf8', $ret->{results}));
  80
+    return if $con->{error};
  81
+    # debug "Setting cache key - 'C:$_[0]'";
  82
+    # debug 'Setting cache val - ' . Dumper(from_json(encode('utf8', $ret->{results})));
  83
+    cache_set "C:$name", $con, 60*60*60;
  84
+  } else {
  85
+    return;
  86
+  }
  87
+}
  88
+
  89
+sub _get_from_db {
  90
+  my $name = shift;
  91
+  return schema->resultset('Constituency')->find_or_create({ name => $name });
  92
+}
  93
+
  94
+sub get_mp {
  95
+  my $self = shift;
  96
+
  97
+  my $mp = PoliticalWeb::Mp->new_from_constituency_name($self->twfy->{name});
  98
+
  99
+  $self->db->update({ mp => $mp->db->id }) if $mp;
  100
+
  101
+  return $mp;  
  102
+}
  103
+
  104
+1;
82  PoliticalWeb/lib/PoliticalWeb/Mp.pm
... ...
@@ -0,0 +1,82 @@
  1
+package PoliticalWeb::Mp;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+use Moose;
  8
+use WebService::TWFY::API;
  9
+use Dancer ':syntax';
  10
+use Dancer::Plugin::DBIC;
  11
+use Dancer::Plugin::Cache::CHI;
  12
+use Encode qw[encode decode];
  13
+
  14
+my $twfy_query = WebService::TWFY::API->new({
  15
+  key => $ENV{TWFY_KEY}
  16
+}) or die $!;
  17
+
  18
+has twfy => (
  19
+  is   => 'ro',
  20
+  isa  => 'HashRef',
  21
+);
  22
+ 
  23
+has db => (
  24
+  is   => 'rw',
  25
+  isa  => 'PoliticalWeb::Schema::Result::Mp',
  26
+);
  27
+
  28
+sub new_from_constituency_name {
  29
+  my $class = shift;
  30
+  my $constit_name = shift;
  31
+  
  32
+  my $twfy = _get_from_cache($constit_name) || _get_from_twfy($constit_name);
  33
+  my $db   = _get_from_db($constit_name, $twfy);
  34
+
  35
+  return $class->new({ twfy => $twfy, db => $db });
  36
+}
  37
+
  38
+sub _get_from_cache {
  39
+  return cache_get "M:$_[0]";  
  40
+}
  41
+
  42
+sub _get_from_twfy {
  43
+  my $constit_name = shift;
  44
+
  45
+  my $mp;
  46
+  my $ret = $twfy_query->query( 'getMP', {
  47
+    constituency => encode('iso-8859-1', $constit_name),
  48
+  });
  49
+
  50
+  # debug Dumper $ret;
  51
+
  52
+  if ($ret->{is_success}) {
  53
+    $mp = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));
  54
+    return if $mp->{error};
  55
+
  56
+    # debug Dumper $mp;
  57
+
  58
+    my $ret = $twfy_query->query( 'getMPInfo', {
  59
+      id => $mp->{person_id},
  60
+    });
  61
+    
  62
+    if ($ret->{is_success}) {
  63
+      $mp->{extra} = from_json(encode('utf8', decode('iso-8859-1', $ret->{results})));   
  64
+    }
  65
+
  66
+    cache_set "M:$constit_name", $mp, 60*60*60;
  67
+  } else {
  68
+    return;
  69
+  }  
  70
+}
  71
+
  72
+sub _get_from_db {
  73
+  my $constit_name = shift;
  74
+  my $twfy = shift;
  75
+
  76
+  return schema->resultset('Mp')->find_or_create({
  77
+    mp_name => $twfy->{full_name},
  78
+    twfy_id => $twfy->{person_id},
  79
+  });  
  80
+}
  81
+
  82
+1;
24  PoliticalWeb/views/constituency.tt
@@ -13,14 +13,14 @@ $(document).ready(function () {
13 13
 </script>
14 14
 [% META title = 'Constituency' -%]
15 15
   <div class="span10 media">
16  
-[% IF mp.image %]
  16
+[% IF mp.twfy.image %]
17 17
     <a class="pull-left" href="#">
18  
-      <img class="media-object" src="http://www.theyworkforyou.com/[% mp.image %]" alt="[% mp.full_name %]" />
  18
+      <img class="media-object" src="http://www.theyworkforyou.com/[% mp.twfy.image %]" alt="[% mp.twfy.full_name %]" />
19 19
     </a>
20 20
 [% END %]
21 21
     <div class="media-body">
22  
-      <h2>[% constit.name %]</h2>
23  
-      <h3>[% mp.full_name %] ([% mp.party %])</h3>
  22
+      <h2>[% constit.twfy.name %]</h2>
  23
+      <h3>[% mp.twfy.full_name %] ([% mp.twfy.party %])</h3>
24 24
     </div>
25 25
   </div>
26 26
   <div class="span6">
@@ -38,20 +38,20 @@ $(document).ready(function () {
38 38
       <div class="tab-content">
39 39
         <div class="tab-pane active" id="links">
40 40
           <ul>
41  
-[% IF mp.extra.wikipedia_url %]
42  
-            <li><a href="[% mp.extra.wikipedia_url %]">Wikipedia entry</a></li>
  41
+[% IF mp.twfy.extra.wikipedia_url %]
  42
+            <li><a href="[% mp.twfy.extra.wikipedia_url %]">Wikipedia entry</a></li>
43 43
 [% END %]
44  
-            <li><a href="http://theyworkforyou.com/mp/?m=[% mp.member_id %]">They Work For You</a></li>
45  
-            <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>
46  
-            <li><a href="http://publicwhip.org.uk/mp.php?constituency=[% constit.name.replace(' ', '+') %]">Voting record from Public Whip</a></li>
47  
-            <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>
48  
-            <li><a href="http://www.guardian.co.uk/politics/constituency/[% constit.guardian_id %]">Guardian constituency page</a></li>
  44
+            <li><a href="http://theyworkforyou.com/mp/?m=[% mp.twfy.member_id %]">They Work For You</a></li>
  45
+            <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>
  46
+            <li><a href="http://publicwhip.org.uk/mp.php?constituency=[% constit.twfy.name.replace(' ', '+') %]">Voting record from Public Whip</a></li>
  47
+            <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>
  48
+            <li><a href="http://www.guardian.co.uk/politics/constituency/[% constit.twfy.guardian_id %]">Guardian constituency page</a></li>
49 49
 
50 50
 [% FOREACH link IN mp.db.mp_links -%]
51 51
             <li><a href="[% link.url %]">[% link.description %]</li>
52 52
 [% END -%]
53 53
 [% IF mp.guardian_aristotle_id %]
54  
-            <li><a href="http://www.guardian.co.uk/politics/person/[% mp.guardian_aristotle_id %]">Guardian MP page</a></li>
  54
+            <li><a href="http://www.guardian.co.uk/politics/person/[% mp.twfy.guardian_aristotle_id %]">Guardian MP page</a></li>
55 55
 [% END %]
56 56
 [% FOREACH link IN constit.db.constituency_links -%]
57 57
             <li><a href="[% link.url %]">[% link.description %]</li>

0 notes on commit 8931e52

Please sign in to comment.
Something went wrong with that request. Please try again.