gds-api-adapters 32.2.1 → 32.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ffef8739d56c013b7ddc653f8d1626407623ec50
4
- data.tar.gz: 7675818d84eb277c3792bfb9e8b544932c77e71e
3
+ metadata.gz: 9aeead33f16d7971b151925cf306e1cc3777e94f
4
+ data.tar.gz: 7d34c0f7607e66742bd58749526b11b0c0c62e4b
5
5
  SHA512:
6
- metadata.gz: 2729d34b9eb08f0ec00fcf82ffcc546661df3fc9314ecb2115f81898294c5a6a48e425507dd21aff1107a2f39343ba05ab5b89f73924950a92eb83328c428993
7
- data.tar.gz: bb927139c619e469bfb6deea4bf68b724179bc828f0f8981c8ae2ce66f62ab3dd5c6ad638459b5d2b87c739909e83282fcd6f75ecb9f123066a1585c746f4585
6
+ metadata.gz: 4e956ff4cf182f461d7f9fcaad9ffd848506193ec3d5f91bd623f5485860969e728f1b00a3b87bb1d4cf810ea20bc0de3696add35d743b9da5ba8d7634c3e8a9
7
+ data.tar.gz: 5e46864dfc8b5d777abf26592d141c246c031fb3450f9ec775b0e5bd8134ed8cdd47c48dc7d32998b5a474659d4f89b677663f0562d25f3a7060ac96223915f2
data/README.md CHANGED
@@ -16,6 +16,22 @@ Example adapters for frequently used applications:
16
16
  - [Content Store](lib/gds_api/content_store.rb) ([docs](http://www.rubydoc.info/github/alphagov/gds-api-adapters/master/GdsApi/ContentStore), [test helper code](https://github.com/alphagov/gds-api-adapters/blob/master/lib/gds_api/test_helpers/content_store.rb), [test helper docs](http://www.rubydoc.info/github/alphagov/gds-api-adapters/master/GdsApi/TestHelpers/ContentStore))
17
17
  - [Rummager](lib/gds_api/rummager.rb) ([docs](http://www.rubydoc.info/github/alphagov/gds-api-adapters/master/GdsApi/Rummager), [test helper code](https://github.com/alphagov/gds-api-adapters/blob/master/lib/gds_api/test_helpers/rummager.rb), [test helper docs](http://www.rubydoc.info/github/alphagov/gds-api-adapters/master/GdsApi/TestHelpers/Rummager))
18
18
 
19
+ ## Configuration
20
+
21
+ We're currently deprecating some behaviour of this gem. You can opt-in to the
22
+ new behaviour now by adding configuration like this:
23
+
24
+ ```ruby
25
+ # config/initializers/gds_api_adapters.rb
26
+ GdsApi.configure do |config|
27
+ # Never return nil when a server responds with 404 or 410.
28
+ config.always_raise_for_not_found = true
29
+
30
+ # Return a hash, not an OpenStruct from a request.
31
+ config.hash_response_for_requests = true
32
+ end
33
+ ```
34
+
19
35
  ## Logging
20
36
 
21
37
  Each HTTP request can be logged as JSON. Example:
@@ -1,2 +1,3 @@
1
1
  require 'gds_api/railtie' if defined?(Rails)
2
2
  require 'gds_api/exceptions'
3
+ require 'gds_api/config'
@@ -0,0 +1,29 @@
1
+ module GdsApi
2
+ def self.configure
3
+ yield(config)
4
+ end
5
+
6
+ def self.config
7
+ @config ||= Config.new
8
+ end
9
+
10
+ class Config
11
+ # Always raise a `HTTPNotFound` exception when the server returns 404 or
12
+ # 410. This avoids nil-errors in your code and makes debugging easier.
13
+ #
14
+ # Currently defaults to false.
15
+ #
16
+ # This configuration allows some time to upgrade - you should opt-in to this
17
+ # behaviour now. We'll change this to default to true on October 1st, 2016
18
+ # and remove the option entirely on December 1st, 2016.
19
+ attr_accessor :always_raise_for_not_found
20
+
21
+ # Set to true to make `GdsApi::Response` behave like a simple hash, instead
22
+ # of an OpenStruct. This will prevent nil-errors.
23
+ #
24
+ # This configuration allows some time to upgrade - you should opt-in to this
25
+ # behaviour now. We'll change this to default to true on October 1st, 2016
26
+ # and remove the option entirely on December 1st, 2016.
27
+ attr_accessor :hash_response_for_requests
28
+ end
29
+ end
@@ -82,8 +82,22 @@ module GdsApi
82
82
  [:get, :post, :put, :patch, :delete].each do |http_method|
83
83
  method_name = "#{http_method}_json"
84
84
  define_method method_name do |url, *args, &block|
85
- ignoring_missing do
85
+ if GdsApi.config.always_raise_for_not_found
86
86
  send (method_name + "!"), url, *args, &block
87
+ else
88
+ warn <<-doc
89
+ DEPRECATION NOTICE: You are making requests that will potentially
90
+ return nil. Please set `GdsApi.config.always_raise_for_not_found = true`
91
+ to make sure all responses with 404 or 410 raise an exception.
92
+
93
+ Raising exceptions will be the default behaviour from October 1st, 2016.
94
+
95
+ Called from: #{caller[2]}
96
+ doc
97
+
98
+ ignoring_missing do
99
+ send (method_name + "!"), url, *args, &block
100
+ end
87
101
  end
88
102
  end
89
103
  end
@@ -6,4 +6,9 @@ class GdsApi::LocalLinksManager < GdsApi::Base
6
6
  url += "&lgil=#{lgil}" if lgil
7
7
  get_json(url)
8
8
  end
9
+
10
+ def local_authority(authority_slug)
11
+ url = "#{endpoint}/api/local-authority?authority_slug=#{authority_slug}"
12
+ get_json(url)
13
+ end
9
14
  end
@@ -75,14 +75,17 @@ module GdsApi
75
75
  end
76
76
 
77
77
  def to_ostruct
78
+ raise NoMethodError if GdsApi.config.hash_response_for_requests
78
79
  @ostruct ||= self.class.build_ostruct_recursively(to_hash)
79
80
  end
80
81
 
81
82
  def method_missing(method_sym, *arguments, &block)
83
+ super if GdsApi.config.hash_response_for_requests
82
84
  to_ostruct.public_send(method_sym, *arguments, &block)
83
85
  end
84
86
 
85
87
  def respond_to_missing?(method, include_private)
88
+ super if GdsApi.config.hash_response_for_requests
86
89
  to_ostruct.respond_to?(method, include_private)
87
90
  end
88
91
 
@@ -108,6 +108,71 @@ module GdsApi
108
108
  .with(query: params)
109
109
  .to_return(body: {}.to_json, status: 404)
110
110
  end
111
+
112
+ def local_links_manager_has_a_local_authority(authority_slug)
113
+ response = {
114
+ "local_authorities" => [
115
+ {
116
+ "name" => authority_slug.capitalize,
117
+ "homepage_url" => "http://#{authority_slug}.example.com",
118
+ "tier" => "unitary"
119
+ }
120
+ ]
121
+ }
122
+
123
+ stub_request(:get, "#{LOCAL_LINKS_MANAGER_ENDPOINT}/api/local-authority")
124
+ .with(query: {authority_slug: authority_slug})
125
+ .to_return(body: response.to_json, status: 200)
126
+ end
127
+
128
+ def local_links_manager_has_a_district_and_county_local_authority(district_slug, county_slug)
129
+ response = {
130
+ "local_authorities" => [
131
+ {
132
+ "name" => district_slug.capitalize,
133
+ "homepage_url" => "http://#{district_slug}.example.com",
134
+ "tier" => "district"
135
+ },
136
+ {
137
+ "name" => county_slug.capitalize,
138
+ "homepage_url" => "http://#{county_slug}.example.com",
139
+ "tier" => "county"
140
+ }
141
+ ]
142
+ }
143
+
144
+ stub_request(:get, "#{LOCAL_LINKS_MANAGER_ENDPOINT}/api/local-authority")
145
+ .with(query: {authority_slug: district_slug})
146
+ .to_return(body: response.to_json, status: 200)
147
+ end
148
+
149
+ def local_links_manager_request_without_local_authority_slug
150
+ stub_request(:get, "#{LOCAL_LINKS_MANAGER_ENDPOINT}/api/local-authority")
151
+ .with(query: {authority_slug: ''})
152
+ .to_return(body: {}.to_json, status: 400)
153
+ end
154
+
155
+ def local_links_manager_does_not_have_an_authority(authority_slug)
156
+ stub_request(:get, "#{LOCAL_LINKS_MANAGER_ENDPOINT}/api/local-authority")
157
+ .with(query: {authority_slug: authority_slug})
158
+ .to_return(body: {}.to_json, status: 404)
159
+ end
160
+
161
+ def local_links_manager_has_a_local_authority_without_homepage(authority_slug)
162
+ response = {
163
+ "local_authorities" => [
164
+ {
165
+ "name" => authority_slug.capitalize,
166
+ "homepage_url" => "",
167
+ "tier" => "unitary"
168
+ }
169
+ ]
170
+ }
171
+
172
+ stub_request(:get, "#{LOCAL_LINKS_MANAGER_ENDPOINT}/api/local-authority")
173
+ .with(query: {authority_slug: authority_slug})
174
+ .to_return(body: response.to_json, status: 200)
175
+ end
111
176
  end
112
177
  end
113
178
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '32.2.1'
2
+ VERSION = '32.3.0'
3
3
  end
@@ -25,6 +25,22 @@ describe GdsApi::ContentStore do
25
25
 
26
26
  assert_nil @api.content_item("/non-existent")
27
27
  end
28
+
29
+ it "raises if the item doesn't exist and `always_raise_for_not_found` enabled" do
30
+ GdsApi.configure do |config|
31
+ config.always_raise_for_not_found = true
32
+ end
33
+
34
+ content_store_does_not_have_item("/non-existent")
35
+
36
+ assert_raises GdsApi::HTTPNotFound do
37
+ @api.content_item("/non-existent")
38
+ end
39
+
40
+ GdsApi.configure do |config|
41
+ config.always_raise_for_not_found = false
42
+ end
43
+ end
28
44
  end
29
45
 
30
46
  describe "#content_item!" do
@@ -608,6 +608,25 @@ class JsonClientTest < MiniTest::Spec
608
608
  assert_equal 2, response.a.b
609
609
  end
610
610
 
611
+ def test_cant_access_attributes_of_response_directly_if_hash_only
612
+ url = "http://some.endpoint/some.json"
613
+ payload = {a: 1}
614
+ stub_request(:put, url).with(body: payload.to_json).to_return(:body => '{"a":{"b":2}}', :status => 200)
615
+ response = @client.put_json(url, payload)
616
+
617
+ GdsApi.configure do |config|
618
+ config.hash_response_for_requests = true
619
+ end
620
+
621
+ assert_raises NoMethodError do
622
+ response.a.b
623
+ end
624
+
625
+ GdsApi.configure do |config|
626
+ config.hash_response_for_requests = false
627
+ end
628
+ end
629
+
611
630
  def test_accessing_non_existent_attribute_of_response_returns_nil
612
631
  url = "http://some.endpoint/some.json"
613
632
  stub_request(:put, url).to_return(:body => '{"a":1}', :status => 200)
@@ -1,7 +1,6 @@
1
1
  require "test_helper"
2
2
  require "gds_api/local_links_manager"
3
3
  require "gds_api/test_helpers/local_links_manager"
4
- require 'pry'
5
4
 
6
5
  describe GdsApi::LocalLinksManager do
7
6
  include GdsApi::TestHelpers::LocalLinksManager
@@ -168,4 +167,68 @@ describe GdsApi::LocalLinksManager do
168
167
  end
169
168
  end
170
169
  end
170
+
171
+ describe '#local_authority' do
172
+ describe 'when making a request for a local authority with a parent' do
173
+ it 'should return the local authority and its parent' do
174
+ local_links_manager_has_a_district_and_county_local_authority('blackburn', 'rochester')
175
+
176
+ expected_response = {
177
+ "local_authorities" => [
178
+ {
179
+ "name" => 'Blackburn',
180
+ "homepage_url" => "http://blackburn.example.com",
181
+ "tier" => "district"
182
+ },
183
+ {
184
+ "name" => 'Rochester',
185
+ "homepage_url" => "http://rochester.example.com",
186
+ "tier" => "county"
187
+ }
188
+ ]
189
+ }
190
+
191
+ response = @api.local_authority('blackburn')
192
+ assert_equal expected_response, response.to_hash
193
+ end
194
+ end
195
+
196
+ describe 'when making a request for a local authority without a parent' do
197
+ it 'should return the local authority' do
198
+ local_links_manager_has_a_local_authority('blackburn')
199
+
200
+ expected_response = {
201
+ "local_authorities" => [
202
+ {
203
+ "name" => 'Blackburn',
204
+ "homepage_url" => "http://blackburn.example.com",
205
+ "tier" => "unitary"
206
+ }
207
+ ]
208
+ }
209
+
210
+ response = @api.local_authority('blackburn')
211
+ assert_equal expected_response, response.to_hash
212
+ end
213
+ end
214
+
215
+ describe 'when making a request without the required parameters' do
216
+ it "raises HTTPClientError when authority_slug is missing" do
217
+ local_links_manager_request_without_local_authority_slug
218
+
219
+ assert_raises GdsApi::HTTPClientError do
220
+ @api.local_authority(nil)
221
+ end
222
+ end
223
+ end
224
+
225
+ describe 'when making a request with invalid required parameters' do
226
+ it "returns nil when authority_slug is invalid" do
227
+ local_links_manager_does_not_have_an_authority("hogwarts")
228
+
229
+ response = @api.local_authority("hogwarts")
230
+ assert_equal nil, response
231
+ end
232
+ end
233
+ end
171
234
  end
@@ -13,6 +13,7 @@ require 'simplecov'
13
13
  require 'simplecov-rcov'
14
14
  require 'mocha/mini_test'
15
15
  require 'timecop'
16
+ require 'gds-api-adapters'
16
17
 
17
18
  SimpleCov.start do
18
19
  add_filter "/test/"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 32.2.1
4
+ version: 32.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stewart
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2016-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: plek
@@ -331,6 +331,7 @@ files:
331
331
  - lib/gds_api/asset_manager.rb
332
332
  - lib/gds_api/base.rb
333
333
  - lib/gds_api/business_support_api.rb
334
+ - lib/gds_api/config.rb
334
335
  - lib/gds_api/content_api.rb
335
336
  - lib/gds_api/content_store.rb
336
337
  - lib/gds_api/core-ext/openstruct.rb