浏览代码

Added caching for postcode API calls to 'postcodes' DB table

tags/last-sinatra-version
Adrian Short 14 年前
父节点
当前提交
4a9d89bf2c
共有 3 个文件被更改,包括 45 次插入32 次删除
  1. +7
    -8
      app.rb
  2. +36
    -3
      lib/models.rb
  3. +2
    -21
      public/style.css

+ 7
- 8
app.rb 查看文件

@@ -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

+ 36
- 3
lib/models.rb 查看文件

@@ -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


+ 2
- 21
public/style.css 查看文件

@@ -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;


正在加载...
取消
保存