gds-api-adapters 1.9.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,6 +16,18 @@ class GdsApi::ContentApi < GdsApi::Base
16
16
  get_json!("#{base_url}/#{slug}.json")
17
17
  end
18
18
 
19
+ def local_authority(snac_code)
20
+ get_json("#{base_url}/local_authorities/#{CGI.escape(snac_code)}.json")
21
+ end
22
+
23
+ def local_authorities_by_name(name)
24
+ get_json!("#{base_url}/local_authorities.json?name=#{CGI.escape(name)}")
25
+ end
26
+
27
+ def local_authorities_by_snac_code(snac_code)
28
+ get_json!("#{base_url}/local_authorities.json?snac_code=#{CGI.escape(snac_code)}")
29
+ end
30
+
19
31
  private
20
32
  def base_url
21
33
  endpoint
@@ -16,6 +16,9 @@ module GdsApi
16
16
  end
17
17
  end
18
18
 
19
+ class HTTPNotFound < HTTPErrorResponse
20
+ end
21
+
19
22
  module ExceptionHandling
20
23
  def ignoring(exception, &block)
21
24
  yield
@@ -39,7 +39,7 @@ module GdsApi
39
39
  end
40
40
 
41
41
  def get_json(url)
42
- ignoring GdsApi::HTTPErrorResponse do
42
+ ignoring GdsApi::HTTPNotFound do
43
43
  get_json! url
44
44
  end
45
45
  end
@@ -49,7 +49,7 @@ module GdsApi
49
49
  end
50
50
 
51
51
  def post_json(url, params)
52
- ignoring GdsApi::HTTPErrorResponse do
52
+ ignoring GdsApi::HTTPNotFound do
53
53
  post_json! url, params
54
54
  end
55
55
  end
@@ -59,7 +59,7 @@ module GdsApi
59
59
  end
60
60
 
61
61
  def put_json(url, params)
62
- ignoring GdsApi::HTTPErrorResponse do
62
+ ignoring GdsApi::HTTPNotFound do
63
63
  put_json! url, params
64
64
  end
65
65
  end
@@ -84,6 +84,8 @@ module GdsApi
84
84
  if response.is_a?(Net::HTTPSuccess)
85
85
  logger.info loggable.merge(status: 'success', end_time: Time.now.to_f).to_json
86
86
  Response.new(response)
87
+ elsif response.is_a?(Net::HTTPNotFound)
88
+ raise GdsApi::HTTPNotFound.new(response.code.to_i)
87
89
  else
88
90
  body = begin
89
91
  JSON.parse(response.body.to_s)
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '1.9.2'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -2,29 +2,167 @@ require 'test_helper'
2
2
  require 'gds_api/content_api'
3
3
  require 'gds_api/test_helpers/content_api'
4
4
 
5
- class ContentApiTest < MiniTest::Unit::TestCase
5
+ describe GdsApi::ContentApi do
6
6
  include GdsApi::TestHelpers::ContentApi
7
7
 
8
- def api
9
- GdsApi::ContentApi.new('test')
8
+ before do
9
+ @base_api_url = "https://contentapi.test.alphagov.co.uk"
10
+ @api = GdsApi::ContentApi.new('test')
10
11
  end
11
12
 
12
- def test_sections
13
- content_api_has_root_sections(["crime"])
14
- response = api.sections
15
- first_section = response["results"][0]
16
- assert_equal "http://contentapi.test.gov.uk/tags/crime.json", first_section["id"]
13
+ describe "sections" do
14
+ it "should show a list of sections" do
15
+ content_api_has_root_sections(["crime"])
16
+ response = @api.sections
17
+ first_section = response["results"][0]
18
+ assert_equal "http://contentapi.test.gov.uk/tags/crime.json", first_section["id"]
19
+ end
17
20
  end
18
21
 
19
- def test_with_tag
20
- tag = "crime-and-justice"
21
- api_url = "https://contentapi.test.alphagov.co.uk/with_tag.json?tag=#{tag}&include_children=1"
22
- json = {
23
- results: [{title: "Complain about a claims company"}]
24
- }.to_json
25
- stub_request(:get, api_url).to_return(:status => 200, :body => json)
26
- response = api.with_tag("crime-and-justice")
27
- subsection = response["results"][0]
28
- assert_equal "Complain about a claims company", subsection["title"]
22
+ describe "tags" do
23
+ it "should produce an artefact with the provided tag" do
24
+ tag = "crime-and-justice"
25
+ api_url = "#{@base_api_url}/with_tag.json?tag=#{tag}&include_children=1"
26
+ json = {
27
+ results: [{title: "Complain about a claims company"}]
28
+ }.to_json
29
+ stub_request(:get, api_url).to_return(:status => 200, :body => json)
30
+ response = @api.with_tag("crime-and-justice")
31
+ subsection = response["results"][0]
32
+ assert_equal "Complain about a claims company", subsection["title"]
33
+ end
29
34
  end
30
- end
35
+
36
+ describe "local authorities" do
37
+ it "should return nil if no local authority found" do
38
+ stub_request(:get, "#{@base_api_url}/local_authorities/does-not-exist.json").
39
+ with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
40
+ to_return(:status => 404,
41
+ :body => {"_response_info" => {"status" => "ok"}}.to_json,
42
+ :headers => {})
43
+
44
+ assert_nil @api.local_authority("does-not-exist")
45
+ end
46
+
47
+ it "should produce a LocalAuthority hash for an existing snac code" do
48
+ body_response = {
49
+ "name" => "Solihull Metropolitan Borough Council",
50
+ "snac_code" => "00CT",
51
+ "id" => "#{@base_api_url}/local_authorities/00CT.json",
52
+ "_response_info" => {"status" => "ok"}
53
+ }
54
+
55
+ stub_request(:get, "#{@base_api_url}/local_authorities/00CT.json").
56
+ with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
57
+ to_return(:status => 200,
58
+ :body => body_response.to_json,
59
+ :headers => {})
60
+
61
+ response = @api.local_authority("00CT").to_hash
62
+
63
+ assert_equal body_response, response
64
+ end
65
+
66
+ it "should return an empty result set if name not found" do
67
+ body_response = {
68
+ "_response_info" => {"status" => "ok"},
69
+ "description" => "Local Authorities",
70
+ "total" => 0,
71
+ "results" => []
72
+ }.to_json
73
+
74
+ stub_request(:get, "#{@base_api_url}/local_authorities.json?name=Swansalona").
75
+ with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
76
+ to_return(:status => 200,
77
+ :body => body_response,
78
+ :headers => {})
79
+
80
+ response = @api.local_authorities_by_name("Swansalona")
81
+
82
+ assert_equal 0, response["total"]
83
+ assert_equal [], response["results"]
84
+ end
85
+
86
+ it "should return an empty result set if snac code not found" do
87
+ body_response = {
88
+ "_response_info" => {"status" => "ok"},
89
+ "description" => "Local Authorities",
90
+ "total" => 0,
91
+ "results" => []
92
+ }.to_json
93
+
94
+ stub_request(:get, "#{@base_api_url}/local_authorities.json?snac_code=SNACKS").
95
+ with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
96
+ to_return(:status => 200,
97
+ :body => body_response,
98
+ :headers => {})
99
+
100
+ response = @api.local_authorities_by_snac_code("SNACKS")
101
+
102
+ assert_equal 0, response["total"]
103
+ assert_equal [], response["results"]
104
+ end
105
+
106
+ it "should have an array of results for a name search" do
107
+ body_response = {
108
+ "_response_info" => {"status" => "ok"},
109
+ "description" => "Local Authorities",
110
+ "total" => 2,
111
+ "results" => [{
112
+ "name" => "Swansalona Council",
113
+ "snac_code" => "00VT",
114
+ "id" => "#{@base_api_url}/local_authorities/00VT.json"
115
+ },
116
+ {
117
+ "name" => "Swansea Council",
118
+ "snac_code" => "00CT",
119
+ "id" => "#{@base_api_url}/local_authorities/00VT.json"
120
+ }]
121
+ }.to_json
122
+
123
+ stub_request(:get, "#{@base_api_url}/local_authorities.json?name=Swans").
124
+ with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
125
+ to_return(:status => 200,
126
+ :body => body_response,
127
+ :headers => {})
128
+
129
+ response = @api.local_authorities_by_name("Swans")
130
+
131
+ assert_equal 2, response["total"]
132
+ assert_equal "Swansalona Council", response["results"][0]["name"]
133
+ end
134
+
135
+ it "should escape snac code when calling unique a local authority" do
136
+ stub_request(:get, "#{@base_api_url}/local_authorities/escape%21.json").
137
+ to_return(:status => 200,
138
+ :body => {"test" => "ing"}.to_json,
139
+ :headers => {})
140
+
141
+ @api.local_authority("escape!")
142
+
143
+ assert_requested :get, "#{@base_api_url}/local_authorities/escape%21.json"
144
+ end
145
+
146
+ it "should escape name when searching for local authorities" do
147
+ stub_request(:get, "#{@base_api_url}/local_authorities.json?name=name%21").
148
+ to_return(:status => 200,
149
+ :body => {"test" => "ing"}.to_json,
150
+ :headers => {})
151
+
152
+ @api.local_authorities_by_name("name!")
153
+
154
+ assert_requested :get, "#{@base_api_url}/local_authorities.json?name=name%21"
155
+ end
156
+
157
+ it "should escape snac code when searching for local authorities" do
158
+ stub_request(:get, "#{@base_api_url}/local_authorities.json?snac_code=snacks%21").
159
+ to_return(:status => 200,
160
+ :body => {"test" => "ing"}.to_json,
161
+ :headers => {})
162
+
163
+ @api.local_authorities_by_snac_code("snacks!")
164
+
165
+ assert_requested :get, "#{@base_api_url}/local_authorities.json?snac_code=snacks%21"
166
+ end
167
+ end
168
+ end
@@ -65,12 +65,56 @@ class JsonClientTest < MiniTest::Spec
65
65
  assert_equal response_a.object_id, response_b.object_id
66
66
  end
67
67
 
68
- def test_should_return_nil_if_404_returned_from_endpoint
68
+ def test_should_raise_http_not_found_if_404_returned_from_endpoint
69
+ url = "http://some.endpoint/some.json"
70
+ stub_request(:get, url).to_return(:body => "{}", :status => 404)
71
+ assert_raises GdsApi::HTTPNotFound do
72
+ @client.get_json!(url)
73
+ end
74
+ end
75
+
76
+ def test_get_should_be_nil_if_404_returned_from_endpoint
69
77
  url = "http://some.endpoint/some.json"
70
78
  stub_request(:get, url).to_return(:body => "{}", :status => 404)
71
79
  assert_nil @client.get_json(url)
72
80
  end
73
81
 
82
+ def test_get_should_raise_error_if_non_404_error_code_returned_from_endpoint
83
+ url = "http://some.endpoint/some.json"
84
+ stub_request(:get, url).to_return(:body => "{}", :status => 500)
85
+ assert_raises GdsApi::HTTPErrorResponse do
86
+ @client.get_json(url)
87
+ end
88
+ end
89
+
90
+ def test_post_should_be_nil_if_404_returned_from_endpoint
91
+ url = "http://some.endpoint/some.json"
92
+ stub_request(:post, url).to_return(:body => "{}", :status => 404)
93
+ assert_nil @client.post_json(url, {})
94
+ end
95
+
96
+ def test_post_should_raise_error_if_non_404_error_code_returned_from_endpoint
97
+ url = "http://some.endpoint/some.json"
98
+ stub_request(:post, url).to_return(:body => "{}", :status => 500)
99
+ assert_raises GdsApi::HTTPErrorResponse do
100
+ @client.post_json(url, {})
101
+ end
102
+ end
103
+
104
+ def test_put_should_be_nil_if_404_returned_from_endpoint
105
+ url = "http://some.endpoint/some.json"
106
+ stub_request(:put, url).to_return(:body => "{}", :status => 404)
107
+ assert_nil @client.put_json(url, {})
108
+ end
109
+
110
+ def test_put_should_raise_error_if_non_404_error_code_returned_from_endpoint
111
+ url = "http://some.endpoint/some.json"
112
+ stub_request(:put, url).to_return(:body => "{}", :status => 500)
113
+ assert_raises GdsApi::HTTPErrorResponse do
114
+ @client.put_json(url, {})
115
+ end
116
+ end
117
+
74
118
  def empty_response
75
119
  net_http_response = stub(:body => '{}')
76
120
  GdsApi::Response.new(net_http_response)
@@ -133,11 +133,13 @@ describe GdsApi::Publisher do
133
133
  assert_equal [], api.licences_for_ids([123,124])
134
134
  end
135
135
 
136
- it "should return nil if publisher returns an error" do
136
+ it "should raise an error if publisher returns an error" do
137
137
  stub_request(:get, %r[\A#{PUBLISHER_ENDPOINT}/licences]).
138
138
  to_return(:status => [503, "Service temporarily unabailable"])
139
139
 
140
- assert_equal nil, api.licences_for_ids([123,124])
140
+ assert_raises GdsApi::HTTPErrorResponse do
141
+ api.licences_for_ids([123,124])
142
+ end
141
143
  end
142
144
 
143
145
  it "should return nil if a council snac code is not found" do
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.9.2
5
+ version: 2.0.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-09-13 00:00:00 Z
13
+ date: 2012-09-14 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -202,7 +202,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
202
202
  requirements:
203
203
  - - ">="
204
204
  - !ruby/object:Gem::Version
205
- hash: 2434091861084396613
205
+ hash: 3104708599933595868
206
206
  segments:
207
207
  - 0
208
208
  version: "0"
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  requirements:
212
212
  - - ">="
213
213
  - !ruby/object:Gem::Version
214
- hash: 2434091861084396613
214
+ hash: 3104708599933595868
215
215
  segments:
216
216
  - 0
217
217
  version: "0"