| @@ -2,37 +2,46 @@ require 'rubygems' | |||||
| require 'sinatra' | require 'sinatra' | ||||
| require 'sinatra-helpers/haml/partials' | require 'sinatra-helpers/haml/partials' | ||||
| require 'haml' | require 'haml' | ||||
| # require 'pat' | |||||
| require 'lib/models' | require 'lib/models' | ||||
| get '/' do | get '/' do | ||||
| if params[:postcode] | |||||
| @postcode = params[:postcode].strip.upcase | |||||
| unless result = Postcode.finder(@postcode) | |||||
| # Invalid postcode | |||||
| redirect '/error' | |||||
| end | |||||
| # Postcode valid but not in LB Sutton | |||||
| if result.district_code != "00BF" | |||||
| redirect '/aliens' | |||||
| end | |||||
| # Postcode in LB Sutton | |||||
| @ward = Ward.first( :ons_id => result.ward_code ) | |||||
| redirect "/wards/#{@ward.slug}/postcode/#{@postcode}" | |||||
| end | |||||
| haml :home | haml :home | ||||
| end | end | ||||
| get '/wards/:id' do | |||||
| @ward = Ward.get(params[:id]) | |||||
| get '/wards/:slug/postcode/:postcode/?' do | |||||
| @ward = Ward.first(:slug => params[:slug]) | |||||
| @postcode = params[:postcode] | |||||
| haml :wards | haml :wards | ||||
| end | end | ||||
| get '/wards' do | |||||
| @postcode = params[:postcode].strip.upcase | |||||
| unless result = Postcode.finder(@postcode) | |||||
| # Invalid postcode | |||||
| redirect '/error' | |||||
| end | |||||
| # Postcode valid but not in LB Sutton | |||||
| if result.district_code != "00BF" | |||||
| redirect '/aliens' | |||||
| end | |||||
| # Postcode in LB Sutton | |||||
| @ward = Ward.first( :ons_id => result.ward_code ) | |||||
| get '/wards/:slug/?' do | |||||
| @ward = Ward.first(:slug => params[:slug]) | |||||
| haml :wards | haml :wards | ||||
| end | end | ||||
| get '/wards/?' do | |||||
| @wards = Ward.all | |||||
| haml :wardlist | |||||
| end | |||||
| get '/how-the-council-election-works' do | get '/how-the-council-election-works' do | ||||
| haml :election | haml :election | ||||
| end | end | ||||
| @@ -0,0 +1,19 @@ | |||||
| require 'rubygems' | |||||
| require 'csv' | |||||
| require 'dm-core' | |||||
| require 'dm-validations' | |||||
| require 'dm-timestamps' | |||||
| require 'lib/models' | |||||
| # Import wards | |||||
| CSV::Reader.parse(File.open('wards.csv', 'rb')) do |row| | |||||
| p row | |||||
| puts Ward.create!( | |||||
| 'ons_id' => row[0], | |||||
| 'name' => row[1], | |||||
| 'slug' => Ward.slugify(row[1]), | |||||
| 'constituency_id' => row[2] == 'Carshalton and Wallington' ? 1 : 2 | |||||
| ) | |||||
| end | |||||
| @@ -5,16 +5,6 @@ require 'dm-validations' | |||||
| require 'dm-timestamps' | require 'dm-timestamps' | ||||
| require 'lib/models' | require 'lib/models' | ||||
| # Import wards | |||||
| # | |||||
| # CSV::Reader.parse(File.open('wards.csv', 'rb')) do |row| | |||||
| # p row | |||||
| # Ward.create( | |||||
| # 'ons_id' => row[0], | |||||
| # 'name' => row[1] | |||||
| # ) | |||||
| # end | |||||
| # | |||||
| # Define parties | # Define parties | ||||
| # parties = [ | # parties = [ | ||||
| @@ -17,10 +17,6 @@ class Postcode | |||||
| property :ward_name, String, :required => true | property :ward_name, String, :required => true | ||||
| property :ward_code, String, :required => true | property :ward_code, String, :required => true | ||||
| def postcode=(postcode) | |||||
| attribute_set(:postcode, postcode.strip.upcase) | |||||
| end | |||||
| def self.finder(postcode) | def self.finder(postcode) | ||||
| postcode = postcode.strip.upcase | postcode = postcode.strip.upcase | ||||
| @@ -32,7 +28,7 @@ class Postcode | |||||
| unless result.code == 404 | unless result.code == 404 | ||||
| # cache API result | # cache API result | ||||
| return self.create( | |||||
| self.create( | |||||
| :postcode => postcode, | :postcode => postcode, | ||||
| :lat => result['geo']['lat'], | :lat => result['geo']['lat'], | ||||
| :lng => result['geo']['lng'], | :lng => result['geo']['lng'], | ||||
| @@ -53,6 +49,7 @@ class Ward | |||||
| include DataMapper::Resource | include DataMapper::Resource | ||||
| property :id, Serial | property :id, Serial | ||||
| property :slug, String, :required => true | |||||
| property :ons_id, String, :required => true | property :ons_id, String, :required => true | ||||
| property :name, String, :required => true | property :name, String, :required => true | ||||
| property :constituency_id, Integer, :required => true | property :constituency_id, Integer, :required => true | ||||
| @@ -16,7 +16,7 @@ | |||||
| %p Who you can vote for depends on where you live. You can find your local candidates and get more information on how the elections work here. | %p Who you can vote for depends on where you live. You can find your local candidates and get more information on how the elections work here. | ||||
| %form{ :method => 'get', :action => '/wards' } | |||||
| %form{ :method => 'get', :action => '/' } | |||||
| %label{ :for => "postcode" } My postcode is | %label{ :for => "postcode" } My postcode is | ||||
| %input{ :type => 'text', :name => 'postcode', :value => 'SM1 1EA', :size => 8, :maxlength => 8 } | %input{ :type => 'text', :name => 'postcode', :value => 'SM1 1EA', :size => 8, :maxlength => 8 } | ||||
| %input{ :type => 'submit', :value => "Find it" } | %input{ :type => 'submit', :value => "Find it" } | ||||
| @@ -21,6 +21,8 @@ | |||||
| %a{ :href => '/how-the-parliament-election-works' } How the parliament election works | %a{ :href => '/how-the-parliament-election-works' } How the parliament election works | ||||
| %p | %p | ||||
| %a{ :href => '/how-the-council-election-works' } How the council election works | %a{ :href => '/how-the-council-election-works' } How the council election works | ||||
| %p | |||||
| %a{ :href => '/wards' } Sutton Council wards | |||||
| %p | %p | ||||
| %a{ :href => '/about' } About this website | %a{ :href => '/about' } About this website | ||||
| <script type="text/javascript"> | <script type="text/javascript"> | ||||
| @@ -0,0 +1,8 @@ | |||||
| .grid_9 | |||||
| %h1 Sutton Council Wards | |||||
| - for ward in @wards | |||||
| %p.adr | |||||
| %a{ :href => "/wards/#{ward.slug}" } | |||||
| %span.locality | |||||
| = ward.name | |||||