Skip to content

Commit

Permalink
cache responses
Browse files Browse the repository at this point in the history
  • Loading branch information
stefl committed Oct 6, 2009
1 parent c16297f commit 6856110
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/controllers/postcode_to_wards_controller.rb
@@ -0,0 +1,2 @@
class PostcodeToWardsController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/helpers/postcode_to_wards_helper.rb
@@ -0,0 +1,2 @@
module PostcodeToWardsHelper
end
3 changes: 3 additions & 0 deletions app/models/postcode_to_ward.rb
@@ -0,0 +1,3 @@
class PostcodeToWard < ActiveRecord::Base
belongs_to :ward
end
20 changes: 17 additions & 3 deletions app/models/ward.rb
Expand Up @@ -2,7 +2,7 @@
require 'open-uri'

class Ward < ActiveRecord::Base
acts_as_solr :fields=>["name"]
#acts_as_solr :fields=>["name"]
belongs_to :constituency

has_permalink :name
Expand All @@ -18,7 +18,13 @@ def openly_local_ward
end

def self.get_by_postcode postcode

postcode = postcode.downcase
# cache the previous results of the lookup
previous_result = PostcodeToWard.find_by_postcode(postcode)
debugger
unless previous_result.blank?
return previous_result.ward
else
lookup_url = "http://www.neighbourhood.statistics.gov.uk/dissemination/LeadAreaSearch.do?a=7&c=#{CGI::escape postcode }&d=14&r=0&i=1001&m=0&enc=1&areaSearchText=#{CGI::escape postcode}&areaSearchType=14&extendedList=false&searchAreas="

result = ''
Expand All @@ -42,10 +48,18 @@ def self.get_by_postcode postcode
end

unless result.blank?
return Ward.find_by_permalink(result.parameterize)
#write the results to the lookup cache

the_ward = Ward.find_by_permalink(result.parameterize)

p = PostcodeToWard.new(:postcode=>postcode, :ward=>the_ward)
p.save

return the_ward
else
return nil
end
end
end
end

2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :postcode_to_wards

map.resources :restyles, :collection=>{:revert => :put}, :member => {:apply => :put}

map.resources :jobs
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20091006160550_create_postcode_to_wards.rb
@@ -0,0 +1,13 @@
class CreatePostcodeToWards < ActiveRecord::Migration
def self.up
create_table :postcode_to_wards do |t|
t.string :postcode
t.integer :ward_id
t.timestamps
end
end

def self.down
drop_table :postcode_to_wards
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20091003185125) do
ActiveRecord::Schema.define(:version => 20091006160550) do

create_table "brain_busters", :force => true do |t|
t.string "question"
Expand Down Expand Up @@ -159,6 +159,13 @@
t.integer "ward_id"
end

create_table "postcode_to_wards", :force => true do |t|
t.string "postcode"
t.integer "ward_id"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "restyles", :force => true do |t|
t.string "title"
t.string "description"
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/postcode_to_wards.yml
@@ -0,0 +1,7 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

# one:
# column: value
#
# two:
# column: value
8 changes: 8 additions & 0 deletions test/functional/postcode_to_wards_controller_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class PostcodeToWardsControllerTest < ActionController::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/postcode_to_wards_helper_test.rb
@@ -0,0 +1,4 @@
require 'test_helper'

class PostcodeToWardsHelperTest < ActionView::TestCase
end
8 changes: 8 additions & 0 deletions test/unit/postcode_to_ward_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class PostcodeToWardTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit 6856110

Please sign in to comment.