gds-api-adapters 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,18 +8,42 @@ class GdsApi::Imminence < GdsApi::Base
8
8
 
9
9
  def places(type, lat, lon, limit=5)
10
10
  places = get_json(api_url(type, lat, lon, limit)) || []
11
- places.map { |o|
12
- o['latitude'] = o['location'][0]
13
- o['longitude'] = o['location'][1]
14
- o['address'] = [
15
- o['address1'],
16
- o['address2']
17
- ].reject { |a| a.nil? or a == '' }.map(&:strip).join(', ')
18
- o
19
- }
11
+ places.map { |p| self.class.parse_place_hash(p) }
12
+ end
13
+
14
+ def self.parse_place_hash(place_hash)
15
+ location = self.extract_location_hash(place_hash["location"])
16
+ address = self.extract_address_hash(place_hash)
17
+
18
+ place_hash.merge(location).merge(address)
20
19
  end
21
20
 
22
21
  def places_kml(type)
23
22
  get_raw("#{@endpoint}/places/#{type}.kml")
24
23
  end
24
+
25
+ private
26
+ def self.extract_location_hash(location)
27
+ # Deal with all known location formats:
28
+ # Old style: [latitude, longitude]; empty array for no location
29
+ # New style: hash with keys "longitude", "latitude"; nil for no location
30
+ case location
31
+ when Array
32
+ {"latitude" => location[0], "longitude" => location[1]}
33
+ when Hash
34
+ location
35
+ when nil
36
+ {"latitude" => nil, "longitude" => nil}
37
+ end
38
+ end
39
+
40
+ def self.extract_address_hash(place_hash)
41
+ address_fields = [
42
+ place_hash["address1"],
43
+ place_hash["address2"]
44
+ ].reject { |a| a.nil? or a == "" }
45
+ {"address" => address_fields.map(&:strip).join(", ")}
46
+ end
47
+
48
+
25
49
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -0,0 +1,100 @@
1
+ require "test_helper"
2
+ require "gds_api/imminence"
3
+
4
+ class ImminenceApiTest < MiniTest::Unit::TestCase
5
+
6
+ ROOT = "https://imminence.test.alphagov.co.uk"
7
+ LATITUDE = 52.1327584352089
8
+ LONGITUDE = -0.4702813074674147
9
+
10
+ def api_client
11
+ GdsApi::Imminence.new('test')
12
+ end
13
+
14
+ def dummy_place
15
+ {
16
+ "access_notes" => nil,
17
+ "address1" => "Cauldwell Street",
18
+ "address2" => "Bedford",
19
+ "fax" => nil,
20
+ "general_notes" => nil,
21
+ "geocode_error" => nil,
22
+ "location" => [LATITUDE, LONGITUDE],
23
+ "name" => "Town Hall",
24
+ "phone" => nil,
25
+ "postcode" => "MK42 9AP",
26
+ "source_address" => "Town Hall, Cauldwell Street, Bedford",
27
+ "text_phone" => nil,
28
+ "town" => nil,
29
+ "url" => "http://www.bedford.gov.uk/advice_and_benefits/registration_service.aspx"
30
+ }
31
+ end
32
+
33
+ def test_no_second_address_line
34
+ c = api_client
35
+ url = "#{ROOT}/places/wibble.json?limit=5&lat=52&lng=0"
36
+ place_info = dummy_place.merge "address2" => nil
37
+ c.expects(:get_json).with(url).returns([place_info])
38
+ places = c.places("wibble", 52, 0)
39
+
40
+ assert_equal 1, places.size
41
+ assert_equal "Cauldwell Street", places[0]["address"]
42
+ end
43
+
44
+ def test_search_for_places
45
+ c = api_client
46
+ url = "#{ROOT}/places/wibble.json?limit=5&lat=52&lng=0"
47
+ c.expects(:get_json).with(url).returns([dummy_place])
48
+ places = c.places("wibble", 52, 0)
49
+
50
+ assert_equal 1, places.size
51
+ place = places[0]
52
+ assert_equal LATITUDE, place["latitude"]
53
+ assert_equal LONGITUDE, place["longitude"]
54
+ assert_equal "Cauldwell Street, Bedford", place["address"]
55
+ end
56
+
57
+ def test_empty_location
58
+ # Test behaviour when the location field is an empty array
59
+ c = api_client
60
+ url = "#{ROOT}/places/wibble.json?limit=5&lat=52&lng=0"
61
+ place_info = dummy_place.merge("location" => [])
62
+ c.expects(:get_json).with(url).returns([place_info])
63
+ places = c.places("wibble", 52, 0)
64
+
65
+ assert_equal 1, places.size
66
+ place = places[0]
67
+ assert_nil place["latitude"]
68
+ assert_nil place["longitude"]
69
+ end
70
+
71
+ def test_nil_location
72
+ # Test behaviour when the location field is nil
73
+ c = api_client
74
+ url = "#{ROOT}/places/wibble.json?limit=5&lat=52&lng=0"
75
+ place_info = dummy_place.merge("location" => nil)
76
+ c.expects(:get_json).with(url).returns([place_info])
77
+ places = c.places("wibble", 52, 0)
78
+
79
+ assert_equal 1, places.size
80
+ place = places[0]
81
+ assert_nil place["latitude"]
82
+ assert_nil place["longitude"]
83
+ end
84
+
85
+ def test_hash_location
86
+ # Test behaviour when the location field is a longitude/latitude hash
87
+ c = api_client
88
+ url = "#{ROOT}/places/wibble.json?limit=5&lat=52&lng=0"
89
+ place_info = dummy_place.merge(
90
+ "location" => {"longitude" => LONGITUDE, "latitude" => LATITUDE}
91
+ )
92
+ c.expects(:get_json).with(url).returns([place_info])
93
+ places = c.places("wibble", 52, 0)
94
+
95
+ assert_equal 1, places.size
96
+ place = places[0]
97
+ assert_equal LATITUDE, place["latitude"]
98
+ assert_equal LONGITUDE, place["longitude"]
99
+ end
100
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.0
5
+ version: 1.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - James Stewart
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-08-15 00:00:00 Z
13
+ date: 2012-08-23 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -170,6 +170,7 @@ files:
170
170
  - test/contactotron_api_test.rb
171
171
  - test/panopticon_api_test.rb
172
172
  - test/publisher_api_test.rb
173
+ - test/imminence_api_test.rb
173
174
  - test/json_client_test.rb
174
175
  - test/gds_api_base_test.rb
175
176
  - test/licence_application_api_test.rb
@@ -187,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
188
  requirements:
188
189
  - - ">="
189
190
  - !ruby/object:Gem::Version
190
- hash: -2151341373007717106
191
+ hash: -4250669656012288164
191
192
  segments:
192
193
  - 0
193
194
  version: "0"
@@ -196,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
197
  requirements:
197
198
  - - ">="
198
199
  - !ruby/object:Gem::Version
199
- hash: -2151341373007717106
200
+ hash: -4250669656012288164
200
201
  segments:
201
202
  - 0
202
203
  version: "0"
@@ -211,6 +212,7 @@ test_files:
211
212
  - test/contactotron_api_test.rb
212
213
  - test/panopticon_api_test.rb
213
214
  - test/publisher_api_test.rb
215
+ - test/imminence_api_test.rb
214
216
  - test/json_client_test.rb
215
217
  - test/gds_api_base_test.rb
216
218
  - test/licence_application_api_test.rb