| @@ -2,7 +2,7 @@ require 'rubygems' | |||
| require 'sinatra' | |||
| require 'sinatra-helpers/haml/partials' | |||
| require 'haml' | |||
| require 'pat' | |||
| # require 'pat' | |||
| require 'lib/models' | |||
| get '/' do | |||
| @@ -16,20 +16,19 @@ end | |||
| get '/wards' do | |||
| @postcode = params[:postcode].strip.upcase | |||
| result = Pat.get(@postcode) | |||
| # Invalid postcode | |||
| if result.code == 404 | |||
| unless result = Postcode.finder(@postcode) | |||
| # Invalid postcode | |||
| redirect '/error' | |||
| end | |||
| # Postcode valid but not in LB Sutton | |||
| if result['administrative']['district']['title'] != "Sutton London Borough Council" | |||
| if result.district_code != "00BF" | |||
| redirect '/aliens' | |||
| end | |||
| # Postcode in LB Sutton | |||
| @ward = Ward.first( :name => result['administrative']['ward']['title'] ) | |||
| @ward = Ward.first( :ons_id => result.ward_code ) | |||
| haml :wards | |||
| end | |||
| @@ -56,4 +55,4 @@ end | |||
| get '/aliens' do | |||
| haml :aliens | |||
| end | |||
| end | |||
| @@ -1,19 +1,52 @@ | |||
| require 'dm-core' | |||
| require 'dm-validations' | |||
| require 'dm-timestamps' | |||
| require 'pat' | |||
| class Postcode | |||
| 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 :lng, Float, :required => true | |||
| property :district_name, String, :required => true | |||
| property :district_code, String, :required => true | |||
| property :ward_name, 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 | |||
| class Ward | |||
| @@ -25,11 +25,6 @@ input.postcode | |||
| text-transform: uppercase; | |||
| } | |||
| #header | |||
| { | |||
| margin-bottom: 0px; | |||
| } | |||
| #main | |||
| { | |||
| margin: 30px 0; | |||
| @@ -43,9 +38,9 @@ input.postcode | |||
| margin: 40px 0 40px 0; | |||
| } | |||
| a, a:visited | |||
| a | |||
| { | |||
| background-color: #d9ec96; | |||
| background-color: #dce9b0; | |||
| padding: 1px 4px; | |||
| color: #111; | |||
| text-decoration: none; | |||
| @@ -65,20 +60,6 @@ a:hover | |||
| 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 | |||
| { | |||
| margin-top: 20px; | |||