gds-api-adapters 4.4.0 → 4.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -80,7 +80,7 @@ class GdsApi::ContentApi < GdsApi::Base
80
80
 
81
81
  private
82
82
  def link_header
83
- @link_header ||= LinkHeader.parse @net_http_response["Link"]
83
+ @link_header ||= LinkHeader.parse @http_response.headers[:link]
84
84
  end
85
85
 
86
86
  def page_link(rel)
@@ -109,7 +109,7 @@ module GdsApi
109
109
 
110
110
  # If no custom response is given, just instantiate Response
111
111
  create_response ||= Proc.new { |r| Response.new(r) }
112
- create_response.call(response.net_http_res)
112
+ create_response.call(response)
113
113
  end
114
114
 
115
115
  # Take a hash of parameters for Request#execute; return a hash of
@@ -0,0 +1,43 @@
1
+ require_relative 'base'
2
+ require_relative 'exceptions'
3
+
4
+ class GdsApi::Mapit < GdsApi::Base
5
+ include GdsApi::ExceptionHandling
6
+
7
+ def location_for_postcode(postcode)
8
+ response = get_json("#{base_url}/postcode/#{CGI.escape postcode}.json")
9
+ return Location.new(response) unless response.nil?
10
+ rescue GdsApi::HTTPErrorResponse => e
11
+ # allow 400 errors, as they can be invalid postcodes people have entered
12
+ raise GdsApi::HTTPErrorResponse.new(e.code) unless e.code == 400
13
+ end
14
+
15
+ class Location
16
+ attr_reader :response
17
+
18
+ def initialize(response)
19
+ @response = response
20
+ end
21
+
22
+ def lat
23
+ @response['wgs84_lat']
24
+ end
25
+
26
+ def lon
27
+ @response['wgs84_lon']
28
+ end
29
+
30
+ def areas
31
+ @response['areas'].map {|i, area| OpenStruct.new(area) }
32
+ end
33
+
34
+ def postcode
35
+ @response['postcode']
36
+ end
37
+ end
38
+
39
+ private
40
+ def base_url
41
+ endpoint
42
+ end
43
+ end
@@ -9,21 +9,21 @@ module GdsApi
9
9
 
10
10
  def_delegators :to_hash, :[], :"<=>", :each
11
11
 
12
- def initialize(net_http_response)
13
- @net_http_response = net_http_response
12
+ def initialize(http_response)
13
+ @http_response = http_response
14
14
  end
15
15
 
16
16
  def raw_response_body
17
- @net_http_response.body
17
+ @http_response.body
18
18
  end
19
19
 
20
20
  def to_hash
21
- @parsed ||= JSON.parse(@net_http_response.body)
21
+ @parsed ||= JSON.parse(@http_response.body)
22
22
  end
23
23
 
24
24
  def code
25
25
  # Return an integer code for consistency with HTTPErrorResponse
26
- @net_http_response.code.to_i
26
+ @http_response.code
27
27
  end
28
28
 
29
29
  def to_ostruct
@@ -58,4 +58,4 @@ module GdsApi
58
58
  end
59
59
  end
60
60
  end
61
- end
61
+ end
@@ -0,0 +1,56 @@
1
+ module GdsApi
2
+ module TestHelpers
3
+ module Mapit
4
+
5
+ MAPIT_ENDPOINT = Plek.current.find('mapit')
6
+
7
+ def mapit_has_a_postcode(postcode, coords)
8
+ response = {
9
+ "wgs84_lat" => coords.first,
10
+ "wgs84_lon" => coords.last,
11
+ "postcode" => postcode
12
+ }
13
+
14
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/" + postcode.sub(' ','+') + ".json")
15
+ .to_return(:body => response.to_json, :status => 200)
16
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/partial/" + postcode.split(' ').first + ".json")
17
+ .to_return(:body => response.to_json, :status => 200)
18
+ end
19
+
20
+ def mapit_has_a_postcode_and_areas(postcode, coords, areas)
21
+ response = {
22
+ "wgs84_lat" => coords.first,
23
+ "wgs84_lon" => coords.last,
24
+ "postcode" => postcode
25
+ }
26
+
27
+ area_response = Hash[areas.map.with_index {|area, i|
28
+ [i, {
29
+ 'codes' => {
30
+ 'ons' => area['ons'],
31
+ 'gss' => area['gss']
32
+ },
33
+ 'name' => area['name'],
34
+ 'type' => area['type']
35
+ }]
36
+ }]
37
+
38
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/" + postcode.sub(' ','+') + ".json")
39
+ .to_return(:body => response.merge({'areas' => area_response}).to_json, :status => 200)
40
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/partial/" + postcode.split(' ').first + ".json")
41
+ .to_return(:body => response.to_json, :status => 200)
42
+ end
43
+
44
+ def mapit_does_not_have_a_postcode(postcode)
45
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/" + postcode.sub(' ','+') + ".json")
46
+ .to_return(:body => { "code" => 404, "error" => "No Postcode matches the given query." }.to_json, :status => 404)
47
+ end
48
+
49
+ def mapit_does_not_have_a_bad_postcode(postcode)
50
+ stub_request(:get, "#{MAPIT_ENDPOINT}/postcode/" + postcode.sub(' ','+') + ".json")
51
+ .to_return(:body => { "code" => 400, "error" => "Postcode '#{postcode}' is not valid." }.to_json, :status => 400)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '4.4.0'
2
+ VERSION = '4.5.0'
3
3
  end
@@ -408,4 +408,13 @@ class JsonClientTest < MiniTest::Spec
408
408
  response = client.put_json("http://some.other.endpoint/some.json", {})
409
409
  assert_equal 2, response.a
410
410
  end
411
+
412
+ def test_client_can_decompress_gzip_responses
413
+ url = "http://some.endpoint/some.json"
414
+ # {"test": "hello"}
415
+ stub_request(:get, url).to_return(:body => "\u001F\x8B\b\u0000Q\u000F\u0019Q\u0000\u0003\xABVP*I-.Q\xB2RP\xCAH\xCD\xC9\xC9WR\xA8\u0005\u0000\xD1C\u0018\xFE\u0013\u0000\u0000\u0000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' })
416
+ response = @client.get_json(url)
417
+
418
+ assert_equal "hello", response["test"]
419
+ end
411
420
  end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+ require 'gds_api/mapit'
3
+ require 'gds_api/test_helpers/mapit'
4
+
5
+ describe GdsApi::Mapit do
6
+ include GdsApi::TestHelpers::Mapit
7
+
8
+ before do
9
+ @base_api_url = Plek.current.find("mapit")
10
+ @api = GdsApi::Mapit.new(@base_api_url)
11
+ end
12
+
13
+ describe "postcodes" do
14
+ it "should return the coordinates" do
15
+ mapit_has_a_postcode("SW1A 1AA", [ 51.5010096, -0.1415870 ])
16
+
17
+ response = @api.location_for_postcode("SW1A 1AA")
18
+ assert_equal 51.5010096, response.lat
19
+ assert_equal -0.1415870, response.lon
20
+ end
21
+
22
+ it "should return the postcode" do
23
+ mapit_has_a_postcode("SW1A 1AA", [ 51.5010096, -0.1415870 ])
24
+
25
+ response = @api.location_for_postcode("SW1A 1AA")
26
+ assert_equal "SW1A 1AA", response.postcode
27
+ end
28
+
29
+ it "should return areas" do
30
+ mapit_has_a_postcode_and_areas("SW1A 1AA", [ 51.5010096, -0.1415870 ], [
31
+ { 'name' => 'Lancashire County Council', 'type' => 'CTY', 'ons' => '30', 'gss' => 'E10000017' },
32
+ { 'name' => 'South Ribble Borough Council', 'type' => 'DIS', 'ons' => '30UN', 'gss' => 'E07000126' }
33
+ ])
34
+
35
+ response = @api.location_for_postcode("SW1A 1AA")
36
+ assert_equal 2, response.areas.length
37
+
38
+ assert_equal "Lancashire County Council", response.areas.first.name
39
+ assert_equal "South Ribble Borough Council", response.areas.last.name
40
+
41
+ assert_equal "CTY", response.areas.first.type
42
+ assert_equal "DIS", response.areas.last.type
43
+
44
+ assert_equal "30", response.areas.first.codes['ons']
45
+ assert_equal "30UN", response.areas.last.codes['ons']
46
+ end
47
+
48
+ it "should return nil if a postcode doesn't exist" do
49
+ mapit_does_not_have_a_postcode("SW1A 1AA")
50
+
51
+ assert_nil @api.location_for_postcode("SW1A 1AA")
52
+ end
53
+
54
+ it "should return nil for an invalid postcode" do
55
+ mapit_does_not_have_a_bad_postcode("B4DP05TC0D3")
56
+
57
+ assert_nil @api.location_for_postcode("B4DP05TC0D3")
58
+ end
59
+ end
60
+ 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: 4.4.0
5
+ version: 4.5.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: 2013-02-04 00:00:00 Z
13
+ date: 2013-02-12 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -195,6 +195,7 @@ files:
195
195
  - lib/gds_api/imminence.rb
196
196
  - lib/gds_api/rummager.rb
197
197
  - lib/gds_api/content_api.rb
198
+ - lib/gds_api/mapit.rb
198
199
  - lib/gds_api/exceptions.rb
199
200
  - lib/gds_api/json_client.rb
200
201
  - lib/gds_api/core-ext/openstruct.rb
@@ -204,6 +205,7 @@ files:
204
205
  - lib/gds_api/test_helpers/licence_application.rb
205
206
  - lib/gds_api/test_helpers/imminence.rb
206
207
  - lib/gds_api/test_helpers/content_api.rb
208
+ - lib/gds_api/test_helpers/mapit.rb
207
209
  - lib/gds_api/test_helpers/content_api/artefact_stub.rb
208
210
  - lib/gds_api/test_helpers/publisher.rb
209
211
  - lib/gds_api/test_helpers/panopticon.rb
@@ -216,6 +218,7 @@ files:
216
218
  - test/rummager_test.rb
217
219
  - test/publisher_api_test.rb
218
220
  - test/licence_application_api_test.rb
221
+ - test/mapit_test.rb
219
222
  - test/panopticon_api_test.rb
220
223
  - test/imminence_api_test.rb
221
224
  - test/json_client_test.rb
@@ -236,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
236
239
  requirements:
237
240
  - - ">="
238
241
  - !ruby/object:Gem::Version
239
- hash: 355718895235244126
242
+ hash: -3906045177861584672
240
243
  segments:
241
244
  - 0
242
245
  version: "0"
@@ -245,7 +248,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
245
248
  requirements:
246
249
  - - ">="
247
250
  - !ruby/object:Gem::Version
248
- hash: 355718895235244126
251
+ hash: -3906045177861584672
249
252
  segments:
250
253
  - 0
251
254
  version: "0"
@@ -260,6 +263,7 @@ test_files:
260
263
  - test/rummager_test.rb
261
264
  - test/publisher_api_test.rb
262
265
  - test/licence_application_api_test.rb
266
+ - test/mapit_test.rb
263
267
  - test/panopticon_api_test.rb
264
268
  - test/imminence_api_test.rb
265
269
  - test/json_client_test.rb