| @@ -2,7 +2,7 @@ require 'rubygems' | |||||
| require 'sinatra' | require 'sinatra' | ||||
| require 'sinatra-helpers/haml/partials' | require 'sinatra-helpers/haml/partials' | ||||
| require 'haml' | require 'haml' | ||||
| require 'pat' | |||||
| # require 'pat' | |||||
| require 'lib/models' | require 'lib/models' | ||||
| get '/' do | get '/' do | ||||
| @@ -16,20 +16,19 @@ end | |||||
| get '/wards' do | get '/wards' do | ||||
| @postcode = params[:postcode].strip.upcase | @postcode = params[:postcode].strip.upcase | ||||
| result = Pat.get(@postcode) | |||||
| # Invalid postcode | |||||
| if result.code == 404 | |||||
| unless result = Postcode.finder(@postcode) | |||||
| # Invalid postcode | |||||
| redirect '/error' | redirect '/error' | ||||
| end | end | ||||
| # Postcode valid but not in LB Sutton | # Postcode valid but not in LB Sutton | ||||
| if result['administrative']['district']['title'] != "Sutton London Borough Council" | |||||
| if result.district_code != "00BF" | |||||
| redirect '/aliens' | redirect '/aliens' | ||||
| end | end | ||||
| # Postcode in LB Sutton | # Postcode in LB Sutton | ||||
| @ward = Ward.first( :name => result['administrative']['ward']['title'] ) | |||||
| @ward = Ward.first( :ons_id => result.ward_code ) | |||||
| haml :wards | haml :wards | ||||
| end | end | ||||
| @@ -56,4 +55,4 @@ end | |||||
| get '/aliens' do | get '/aliens' do | ||||
| haml :aliens | haml :aliens | ||||
| end | |||||
| end | |||||
| @@ -1,19 +1,52 @@ | |||||
| require 'dm-core' | require 'dm-core' | ||||
| require 'dm-validations' | require 'dm-validations' | ||||
| require 'dm-timestamps' | require 'dm-timestamps' | ||||
| require 'pat' | |||||
| class Postcode | class Postcode | ||||
| include DataMapper::Resource | include DataMapper::Resource | ||||
| property :id, Serial | |||||
| property :postcode, String, :required => true | |||||
| property :created_at, DateTime, :required => true | |||||
| # Postcode natural key, uppercase with space, eg. "SM1 1EA" | |||||
| property :postcode, String, :key => true | |||||
| property :created_at, DateTime | |||||
| property :updated_at, DateTime | |||||
| property :lat, Float, :required => true | property :lat, Float, :required => true | ||||
| property :lng, Float, :required => true | property :lng, Float, :required => true | ||||
| property :district_name, String, :required => true | property :district_name, String, :required => true | ||||
| property :district_code, String, :required => true | property :district_code, String, :required => true | ||||
| 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) | |||||
| postcode = postcode.strip.upcase | |||||
| if o = self.get(postcode) | |||||
| return o | |||||
| end | |||||
| result = Pat.get(postcode) | |||||
| unless result.code == 404 | |||||
| # cache API result | |||||
| return self.create( | |||||
| :postcode => postcode, | |||||
| :lat => result['geo']['lat'], | |||||
| :lng => result['geo']['lng'], | |||||
| :district_name => result['administrative']['district']['title'], | |||||
| :district_code => result['administrative']['district']['uri'].match(/.+\/(.+)$/)[1], | |||||
| :ward_name => result['administrative']['ward']['title'], | |||||
| :ward_code => result['administrative']['ward']['uri'].match(/.+\/(.+)$/)[1] | |||||
| ) | |||||
| else | |||||
| # invalid postcode | |||||
| nil | |||||
| end | |||||
| end | |||||
| end | end | ||||
| class Ward | class Ward | ||||
| @@ -25,11 +25,6 @@ input.postcode | |||||
| text-transform: uppercase; | text-transform: uppercase; | ||||
| } | } | ||||
| #header | |||||
| { | |||||
| margin-bottom: 0px; | |||||
| } | |||||
| #main | #main | ||||
| { | { | ||||
| margin: 30px 0; | margin: 30px 0; | ||||
| @@ -43,9 +38,9 @@ input.postcode | |||||
| margin: 40px 0 40px 0; | margin: 40px 0 40px 0; | ||||
| } | } | ||||
| a, a:visited | |||||
| a | |||||
| { | { | ||||
| background-color: #d9ec96; | |||||
| background-color: #dce9b0; | |||||
| padding: 1px 4px; | padding: 1px 4px; | ||||
| color: #111; | color: #111; | ||||
| text-decoration: none; | text-decoration: none; | ||||
| @@ -65,20 +60,6 @@ a:hover | |||||
| color: #fff; | color: #fff; | ||||
| } | } | ||||
| a.date | |||||
| { | |||||
| font-size: 610%; | |||||
| font-weight: bold; | |||||
| padding: 30px 30px; | |||||
| background-color: red; | |||||
| display: none; | |||||
| position: absolute; | |||||
| left: 0; | |||||
| top: 1000; | |||||
| -webkit-transform: rotate(-90deg); | |||||
| -moz-transform: rotate(-90deg); | |||||
| } | |||||
| h1 | h1 | ||||
| { | { | ||||
| margin-top: 20px; | margin-top: 20px; | ||||