Skip to content

Commit

Permalink
postcode search using police maps api
Browse files Browse the repository at this point in the history
  • Loading branch information
robmckinnon committed Jun 2, 2010
1 parent 7d18d7f commit 6e2a97a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
47 changes: 30 additions & 17 deletions app/controllers/application_controller.rb
Expand Up @@ -2,6 +2,8 @@
# Likewise, all the methods added will be available for all controllers.
require 'uri'
require 'morph'
require 'hpricot'
require 'rest_client'

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
Expand All @@ -21,24 +23,35 @@ def home

def search
postcode = params[:postcode]
url = "http://policeapi.rkh.co.uk/api/geocode-crime-area?key=rewiredcrime&q="+postcode
url = URI.encode(url)
response = RestClient.get url
xml = response.to_s
data = Hash.from_xml(xml)
m = Morph.from_hash data
area_id = m.response.areas.areas[1].area_id

url = "http://policeapi.rkh.co.uk/api/crime-area?key=rewiredcrime&force=metropolitan&area="+area_id
url = URI.encode(url)
response = RestClient.get url
xml = response.to_s
data = Hash.from_xml(xml)
m = Morph.from_hash data
name = m.response.name
url = "http://maps.met.police.uk/php/lookup.php?pcode="+postcode
point = morph_request(url).centre_points.first

group = PoliceGroup.find_by_name(name)
url = "http://maps.met.police.uk/php/lookup.php?minlat=#{point.lat}&minlng=#{point.lng}&maxlat=#{point.lat}&maxlng=#{point.lng}&level=4&CTID=8"
area_code = morph_request(url).areas.first.area_code

url = "http://maps.met.police.uk/php/dataview.php?area=#{area_code}&ct=8"
html = get_request(url)
doc = Hpricot html
borough = doc.at('ul.bread-crumb/li[3]/a/text()').to_s

group = PoliceGroup.find_by_name(borough)

redirect_to :controller => 'police_group', :action => 'show', :id => group.id
# redirect_to :controller => 'police_group', :action => 'show', :id => group.id
redirect_to group
end

private

def get_request url
url = URI.encode(url)
RestClient.get(url)
end

def morph_request url
response = get_request url
json = response.to_s
hash = eval(json.gsub("':","' => ").sub("'type'","'request_type'"))
Morph.from_hash(hash)
end

end
2 changes: 1 addition & 1 deletion app/controllers/police_groups_controller.rb
Expand Up @@ -6,7 +6,7 @@ class PoliceGroupsController < ResourceController::Base
def ensure_current_url
begin
group = PoliceGroup.find(params[:id])
redirect_to group, :status => :moved_permanently if group.has_better_id?
redirect_to(group, :status => :moved_permanently) if group.has_better_id?
rescue
render_not_found
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/police_group.rb
@@ -1,6 +1,6 @@
class PoliceGroup < ActiveRecord::Base

has_friendly_id :name, :use_slug => true, :strip_diacritics => true
has_friendly_id :name, :use_slug => true #, :strip_diacritics => true

def rank
groups = PoliceGroup.all.sort_by(&:grand_total)
Expand Down
2 changes: 1 addition & 1 deletion app/views/application/home.html.erb
@@ -1,4 +1,4 @@
<form id="Login" title="Met Complaints" class="panel" selected="true" action="/police_groups" method="post" redirect="true">
<form id="Search" title="Met Complaints" class="panel" selected="true" action="/search" method="post" redirect="true">
<fieldset>
<div class="row">
<label>Enter Postcode</label>
Expand Down
1 change: 1 addition & 0 deletions app/views/police_groups/show.html.erb
Expand Up @@ -15,6 +15,7 @@
level public complaints Dec 2008 to Nov 2009,
<br />BSMI Report</p>

<p><a href="">Link to <%=h @police_group.name %>'s Police Consultative Group</a></p>
<!--
<p>
<b>Discriminatory behaviour:</b>
Expand Down
8 changes: 4 additions & 4 deletions config/routes.rb
@@ -1,10 +1,10 @@
ActionController::Routing::Routes.draw do |map|
map.resources :police_groups
map.resources :police_groups, :only => :show

# The priority is based upon order of creation: first created -> highest priority.

# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
map.connect 'search', :controller => 'application', :action => 'search'
# Keep in mind you can assign values other than :controller and :action

# Sample of named route:
Expand Down Expand Up @@ -40,6 +40,6 @@
# Install the default routes as the lowest priority.
# Note: These default routes make all actions in every controller accessible via GET requests. You should
# consider removing or commenting them out if you're using named routes and resources.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
# map.connect ':controller/:action/:id'
# map.connect ':controller/:action/:id.:format'
end

0 comments on commit 6e2a97a

Please sign in to comment.