geogov 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -20,9 +20,9 @@ module Geogov
20
20
  raise ArgumentError.new("#{instance.class} doesn't respond to #{method}")
21
21
  end
22
22
 
23
- caching_instance = SimpleCache.new(instance)
23
+ # caching_instance = SimpleCache.new(instance)
24
24
  @@methods ||= {}
25
- @@methods[method] = caching_instance
25
+ @@methods[method] = instance
26
26
 
27
27
  unless self.methods().include?(method)
28
28
  dispatcher = <<-EOS
@@ -1,8 +1,8 @@
1
1
  module Geogov
2
2
  class GeoStack
3
3
 
4
- attr_accessor :ward, :council, :nation, :country, :wmc, :lat, :lon, :friendly_name
5
- attr_reader :postcode, :fuzzy_point
4
+ attr_accessor :ward, :council, :nation, :country, :region, :lat, :lon, :authorities, :fuzzy_point
5
+ attr_reader :postcode
6
6
 
7
7
  def initialize(&block)
8
8
  if block_given?
@@ -58,13 +58,10 @@ module Geogov
58
58
  {
59
59
  :fuzzy_point => self.fuzzy_point.to_hash,
60
60
  :postcode => self.postcode,
61
- :ward => self.ward,
62
61
  :council => self.council,
63
- :nation => self.nation,
64
- :country => self.country,
65
- :wmc => self.wmc,
62
+ :ward => self.ward,
66
63
  :friendly_name => self.friendly_name
67
- }.select {|k,v| !(v.nil?) }
64
+ }#.select {|k,v| !(v.nil?) }
68
65
  end
69
66
 
70
67
  def update(hash)
@@ -79,6 +76,52 @@ module Geogov
79
76
  empty.fuzzy_point = empty.calculate_fuzzy_point
80
77
  end
81
78
  end
79
+
80
+ def friendly_name
81
+ @friendly_name ||= build_locality
82
+ end
83
+
84
+ def has_authority?( type )
85
+ get_authority(type) ? true : false
86
+ end
87
+
88
+ def get_authority( type )
89
+ return false if self.authorities[type.upcase.to_sym] == true
90
+ self.authorities.nil? or self.authorities[type.upcase.to_sym].nil? ? false : self.authorities[type.upcase.to_sym]
91
+ end
92
+
93
+ def formatted_authority_name( type )
94
+ return false unless has_authority?(type)
95
+ name = get_authority(type)['name'].dup
96
+
97
+ name.sub!(/ *((District Council|Borough Council|Community|County Council|City Council|Council) ?)+/,'')
98
+ name.sub!(/ (North|East|South|West|Central)$/,'')
99
+ name.sub!(/Mid /,'')
100
+
101
+ name
102
+ end
103
+
104
+ def build_locality
105
+ return false unless self.authorities
106
+
107
+ case
108
+ when has_authority?('DIS') && has_authority?('CTY')
109
+ locality = ['DIS','CTY']
110
+ when has_authority?('LBO')
111
+ locality = ['LBO','London']
112
+ when has_authority?('UTA') && has_authority?('CPC') # for cornwall civil parishes
113
+ locality = ['CPC','UTA']
114
+ when has_authority?('UTA') && has_authority?('UTE')
115
+ locality = ['UTE','UTA']
116
+ when has_authority?('UTA') && has_authority?('UTW')
117
+ locality = ['UTW','UTA']
118
+ when has_authority?('MTW') && has_authority?('MTD')
119
+ locality = ['MTW','MTD']
120
+ else
121
+ return false
122
+ end
123
+ locality.map {|t| formatted_authority_name(t) || t }.uniq.join(', ')
124
+ end
82
125
 
83
126
  def has_valid_lat_lon(hash)
84
127
  return (hash['lon'] and hash['lat'] and hash['lon'] != "" and hash['lat'] != "")
@@ -90,16 +133,12 @@ module Geogov
90
133
  fields = Geogov.areas_for_stack_from_postcode(postcode)
91
134
  if fields
92
135
  lat_lon = fields[:point]
93
- if lat_lon
94
- self.friendly_name = Geogov.nearest_place_name(lat_lon['lat'],lat_lon['lon'])
95
- end
96
136
  set_fields(fields.select {|k,v| k != :point})
97
137
  end
98
138
  end
99
139
  end
100
140
 
101
141
  def fetch_missing_fields_for_coords(lat, lon)
102
- self.friendly_name = Geogov.nearest_place_name(lat, lon)
103
142
  fields = Geogov.areas_for_stack_from_coords(lat, lon)
104
143
  if ['England', 'Scotland', 'Northern Ireland', 'Wales'].include?(fields[:nation])
105
144
  self.country = 'UK'
@@ -115,7 +154,9 @@ module Geogov
115
154
  self.send(setter,value)
116
155
  end
117
156
  else
118
- raise ArgumentError, "geo type '#{geo}' is not a valid geo type"
157
+ self.authorities ||= { }
158
+ self.authorities[geo] = value
159
+ # raise ArgumentError, "geo type '#{geo}' is not a valid geo type"
119
160
  end
120
161
  end
121
162
  self
@@ -55,6 +55,8 @@ module Geogov
55
55
  return 'council' # county
56
56
  elsif ['DIS', 'LBO'].include?(area_type)
57
57
  return 'council' # district
58
+ elsif area_type == 'EUR'
59
+ return 'region'
58
60
  elsif area_type == 'WMC' # XXX Also maybe 'EUR', 'NIE', 'SPC', 'SPE', 'WAC', 'WAE', 'OLF', 'OLG', 'OMF', 'OMG')
59
61
  return 'WMC'
60
62
  end
@@ -63,8 +65,12 @@ module Geogov
63
65
  def areas_for_stack_from_coords(lat, lon)
64
66
  query = self.point("4326", [lon, lat])
65
67
  results = {:point => {'lat' => lat, 'lon' => lon}}
68
+ councils = { }
69
+
66
70
  query.each do |id, area_info|
71
+ type = area_info['type'].upcase.to_sym
67
72
  level = translate_area_type_to_shortcut(area_info['type'])
73
+
68
74
  if level
69
75
  level = level.downcase.to_sym
70
76
  results[level] = [] unless results[level]
@@ -73,15 +79,24 @@ module Geogov
73
79
  results[level] << level_info
74
80
  results[:nation] = area_info['country_name'] if results[:nation].nil?
75
81
  end
82
+
83
+ councils[type] = { 'name' => area_info['name'], 'type' => area_info['type'], 'id' => area_info['id'] }
76
84
  end
77
- return results
85
+
86
+ return councils.merge results
78
87
  end
79
88
 
80
89
  def areas_for_stack_from_postcode(postcode)
81
90
  query = self.postcode(postcode)
82
91
  results = {}
83
92
 
84
- if query && query['shortcuts'] && query['areas']
93
+ if query && query['areas']
94
+
95
+ query['areas'].each do |i, area|
96
+ type = area['type'].to_sym
97
+ results[type] = area
98
+ end
99
+
85
100
  query['shortcuts'].each do |typ, i|
86
101
  if i.is_a? Hash
87
102
  ids = i.values()
@@ -3,7 +3,6 @@ require 'test_helper'
3
3
  class GovspeakTest < Test::Unit::TestCase
4
4
 
5
5
  test "IP-located stack should have country" do
6
-
7
6
  Geogov.configure do |g|
8
7
  g.provider_for :centre_of_country, stub(:centre_of_country => {"lat"=>37, "lon"=>-96})
9
8
  g.provider_for :remote_location, stub(:remote_location => {'country' => 'US'})
@@ -74,4 +73,52 @@ class GovspeakTest < Test::Unit::TestCase
74
73
  assert_equal :country, stack.fuzzy_point.accuracy
75
74
  end
76
75
 
76
+ test "stack with postcode returns correct locality" do
77
+ stack = Geogov::GeoStack.new
78
+ stack = stack.update( 'postcode' => "SW1A 1AA" )
79
+
80
+ assert_equal "Westminster, London", stack.friendly_name
81
+ end
82
+
83
+ test "stack with coordinates returns correct locality" do
84
+ stack = Geogov::GeoStack.new
85
+ stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
86
+
87
+ assert_equal "Westminster, London", stack.friendly_name
88
+ end
89
+
90
+ test "stack with postcode returns authorities" do
91
+ stack = Geogov::GeoStack.new
92
+ stack = stack.update( 'postcode' => "SW1A 1AA" )
93
+
94
+ assert stack.authorities.any?
95
+ end
96
+
97
+ test "stack with coordinates returns authorities" do
98
+ stack = Geogov::GeoStack.new
99
+ stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
100
+
101
+ assert stack.authorities.any?
102
+ end
103
+
104
+ test "stack with postcode returns correct locality, ward and council" do
105
+ stack = Geogov::GeoStack.new
106
+ stack = stack.update( 'postcode' => "SW1A 1AA" )
107
+
108
+ assert_equal "Westminster, London", stack.friendly_name
109
+ assert stack.ward and stack.ward.any?
110
+ assert stack.council and stack.council.any?
111
+ assert_equal "Westminster City Council", stack.council.first['name']
112
+ end
113
+
114
+ test "stack with coordinates returns correct locality, ward and council" do
115
+ stack = Geogov::GeoStack.new
116
+ stack = stack.update( 'lat' => "51.501009", "lon" => "-0.1415870" )
117
+
118
+ assert_equal "Westminster, London", stack.friendly_name
119
+ assert stack.ward and stack.ward.any?
120
+ assert stack.council and stack.council.any?
121
+ assert_equal "Westminster City Council", stack.council.first['name']
122
+ end
123
+
77
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geogov
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-01-06 00:00:00.000000000Z
13
+ date: 2012-01-20 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70264753872460 !ruby/object:Gem::Requirement
17
+ requirement: &70258242218540 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 0.9.0
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *70264753872460
25
+ version_requirements: *70258242218540
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mocha
28
- requirement: &70264753871900 !ruby/object:Gem::Requirement
28
+ requirement: &70258242217200 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 0.9.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70264753871900
36
+ version_requirements: *70258242217200
37
37
  description: Geolocation and utilities for UK Government single domain
38
38
  email:
39
39
  - ben@alphagov.co.uk