gds-api-adapters 2.10.0 → 2.11.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.
@@ -60,8 +60,38 @@ class GdsApi::ContentApi < GdsApi::Base
60
60
  get_json!("#{base_url}/local_authorities.json?snac_code=#{CGI.escape(snac_code)}")
61
61
  end
62
62
 
63
+ def business_support_schemes(identifiers)
64
+ identifiers = identifiers.map {|i| CGI.escape(i) }
65
+ url_template = "#{base_url}/business_support_schemes.json?identifiers="
66
+ response = nil # assignment necessary for variable scoping
67
+
68
+ start_url = "#{url_template}#{identifiers.shift}"
69
+ last_batch_url = identifiers.inject(start_url) do |url, id|
70
+ new_url = [url, id].join(',')
71
+ if new_url.length >= 2000
72
+ # fetch a batch using the previous url, then return a new start URL with this id
73
+ response = get_batch(url, response)
74
+ "#{url_template}#{id}"
75
+ else
76
+ new_url
77
+ end
78
+ end
79
+ get_batch(last_batch_url, response)
80
+ end
81
+
63
82
  private
64
83
  def base_url
65
84
  endpoint
66
85
  end
86
+
87
+ def get_batch(batch_url, existing_response = nil)
88
+ batch_response = get_json!(batch_url)
89
+ if existing_response
90
+ existing_response.to_hash["total"] += batch_response["total"]
91
+ existing_response.to_hash["results"] += batch_response["results"]
92
+ existing_response
93
+ else
94
+ batch_response
95
+ end
96
+ end
67
97
  end
@@ -199,6 +199,24 @@ module GdsApi
199
199
  }
200
200
  end
201
201
 
202
+ def setup_content_api_business_support_schemes_stubs
203
+ @stubbed_content_api_business_support_schemes = []
204
+ stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).to_return do |request|
205
+ if request.uri.query_values and request.uri.query_values["identifiers"]
206
+ ids = request.uri.query_values["identifiers"].split(',')
207
+ results = @stubbed_content_api_business_support_schemes.select {|bs| ids.include? bs["details"]["business_support_identifier"] }
208
+ else
209
+ results = []
210
+ end
211
+ {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json}
212
+ end
213
+ end
214
+
215
+ def content_api_has_business_support_scheme(scheme)
216
+ raise "Need a licence identifier" if scheme["details"]["business_support_identifier"].nil?
217
+ @stubbed_content_api_business_support_schemes << scheme
218
+ end
219
+
202
220
  private
203
221
 
204
222
  def titleize_slug(slug)
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '2.10.0'
2
+ VERSION = '2.11.0'
3
3
  end
@@ -230,4 +230,130 @@ describe GdsApi::ContentApi do
230
230
  assert_requested :get, "#{@base_api_url}/local_authorities.json?snac_code=snacks%21"
231
231
  end
232
232
  end
233
+
234
+ describe "business support schemes" do
235
+ it "should query content_api for business_support_schemes" do
236
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
237
+ to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
238
+
239
+ response = @api.business_support_schemes(['foo', 'bar'])
240
+
241
+ assert_equal({"foo" => "bar"}, response.to_hash)
242
+ assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=foo,bar", :times => 1
243
+ end
244
+
245
+ it "should CGI escape identifiers" do
246
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
247
+ to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
248
+
249
+ response = @api.business_support_schemes(['foo bar', 'baz&bing'])
250
+
251
+ assert_equal({"foo" => "bar"}, response.to_hash)
252
+ assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=foo%20bar,baz%26bing", :times => 1
253
+ end
254
+
255
+ it "should not modify the given array" do
256
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
257
+ to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
258
+
259
+ ids = %w(foo bar baz)
260
+ @api.business_support_schemes(ids)
261
+
262
+ assert_equal %w(foo bar baz), ids
263
+ end
264
+
265
+ it "should raise an error if content_api returns 404" do
266
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
267
+ to_return(:status => 404, :body => "Not Found")
268
+
269
+ assert_raises GdsApi::HTTPNotFound do
270
+ @api.business_support_schemes(['foo', 'bar'])
271
+ end
272
+ end
273
+
274
+ it "should raise an error if content_api returns a 50x error" do
275
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
276
+ to_return(:status => 503, :body => "Gateway timeout")
277
+
278
+ assert_raises GdsApi::HTTPErrorResponse do
279
+ @api.business_support_schemes(['foo', 'bar'])
280
+ end
281
+ end
282
+
283
+ describe "handling requests that would have a URI in excess of 2000 chars" do
284
+ before :each do
285
+ stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes\.json}).
286
+ to_return(:status => 200, :body => api_response_for_results([{"foo" => "bar"}]).to_json)
287
+ end
288
+
289
+ it "should do the request in batches" do
290
+ ids = (1..300).map {|n| sprintf "%09d", n } # each id is 9 chars long
291
+
292
+ response = @api.business_support_schemes(ids)
293
+
294
+ assert_requested :get, %r{\A#{@base_api_url}/business_support_schemes\.json}, :times => 2
295
+
296
+ first_batch = ids[0..190]
297
+ assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=#{first_batch.join(',')}"
298
+ second_batch = ids[191..299]
299
+ assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=#{second_batch.join(',')}"
300
+ end
301
+
302
+ it "should merge the responses into a single GdsApi::Response" do
303
+ ids = (1..300).map {|n| sprintf "%09d", n } # each id is 9 chars long
304
+ first_batch = ids[0..190]
305
+ stub_request(:get, "#{@base_api_url}/business_support_schemes.json").
306
+ with(:query => {"identifiers" => first_batch.join(',')}).
307
+ to_return(:status => 200, :body => api_response_for_results(first_batch).to_json) # We're stubbing response that just return the requested ids
308
+ second_batch = ids[191..299]
309
+ stub_request(:get, "#{@base_api_url}/business_support_schemes.json").
310
+ with(:query => {"identifiers" => second_batch.join(',')}).
311
+ to_return(:status => 200, :body => api_response_for_results(second_batch).to_json)
312
+
313
+ response = @api.business_support_schemes(ids)
314
+
315
+ # Assert both Hash an OpenStruct access to ensure nothing's been memoized part-way through merging stuff
316
+ assert_equal 300, response["total"]
317
+ assert_equal ids, response["results"]
318
+
319
+ assert_equal 300, response.total
320
+ assert_equal ids, response.results
321
+ end
322
+ end
323
+
324
+ it "should do the request in batches if the request path would otherwise exceed 2000 chars" do
325
+
326
+ end
327
+
328
+ describe "test helpers" do
329
+ it "should have representative test helpers" do
330
+ setup_content_api_business_support_schemes_stubs
331
+
332
+ s1 = artefact_for_slug('scheme-1')
333
+ s1["details"].merge!("business_support_identifier" => "s1")
334
+ content_api_has_business_support_scheme(s1)
335
+ s2 = artefact_for_slug('scheme-2')
336
+ s2["details"].merge!("business_support_identifier" => "s2")
337
+ content_api_has_business_support_scheme(s2)
338
+ s3 = artefact_for_slug('scheme-3')
339
+ s3["details"].merge!("business_support_identifier" => "s3")
340
+ content_api_has_business_support_scheme(s3)
341
+
342
+ response = @api.business_support_schemes(['s1', 's3']).to_hash
343
+
344
+ assert_equal 2, response["total"]
345
+ assert_equal [s1, s3], response["results"]
346
+ end
347
+ end
348
+ end
349
+
350
+ def api_response_for_results(results)
351
+ {
352
+ "_response_info" => {
353
+ "status" => "ok",
354
+ },
355
+ "total" => results.size,
356
+ "results" => results,
357
+ }
358
+ end
233
359
  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: 2.10.0
5
+ version: 2.11.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-10-01 00:00:00 Z
13
+ date: 2012-10-02 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -154,44 +154,44 @@ extensions: []
154
154
  extra_rdoc_files: []
155
155
 
156
156
  files:
157
- - lib/gds_api/helpers.rb
158
- - lib/gds_api/base.rb
159
- - lib/gds_api/part_methods.rb
160
- - lib/gds_api/exceptions.rb
161
- - lib/gds_api/rummager.rb
157
+ - lib/gds_api/version.rb
158
+ - lib/gds_api/publisher.rb
159
+ - lib/gds_api/panopticon/registerer.rb
162
160
  - lib/gds_api/typhoeus_client.rb
161
+ - lib/gds_api/content_api.rb
163
162
  - lib/gds_api/imminence.rb
164
- - lib/gds_api/publisher.rb
165
163
  - lib/gds_api/contactotron.rb
166
- - lib/gds_api/panopticon.rb
167
- - lib/gds_api/version.rb
168
- - lib/gds_api/test_helpers/imminence.rb
169
164
  - lib/gds_api/test_helpers/publisher.rb
165
+ - lib/gds_api/test_helpers/content_api.rb
166
+ - lib/gds_api/test_helpers/imminence.rb
170
167
  - lib/gds_api/test_helpers/contactotron.rb
168
+ - lib/gds_api/test_helpers/licence_application.rb
171
169
  - lib/gds_api/test_helpers/panopticon.rb
172
170
  - lib/gds_api/test_helpers/json_client_helper.rb
173
- - lib/gds_api/test_helpers/licence_application.rb
174
- - lib/gds_api/test_helpers/content_api.rb
175
- - lib/gds_api/response.rb
176
- - lib/gds_api/panopticon/registerer.rb
177
- - lib/gds_api/needotron.rb
178
- - lib/gds_api/json_client.rb
179
171
  - lib/gds_api/licence_application.rb
172
+ - lib/gds_api/base.rb
173
+ - lib/gds_api/json_client.rb
174
+ - lib/gds_api/response.rb
175
+ - lib/gds_api/rummager.rb
176
+ - lib/gds_api/panopticon.rb
180
177
  - lib/gds_api/core-ext/openstruct.rb
181
- - lib/gds_api/content_api.rb
178
+ - lib/gds_api/part_methods.rb
179
+ - lib/gds_api/needotron.rb
180
+ - lib/gds_api/exceptions.rb
181
+ - lib/gds_api/helpers.rb
182
182
  - README.md
183
183
  - Rakefile
184
- - test/panopticon_registerer_test.rb
185
- - test/gds_api_base_test.rb
186
- - test/json_client_test.rb
187
- - test/test_helper.rb
188
- - test/licence_application_api_test.rb
189
- - test/publisher_api_test.rb
190
- - test/imminence_api_test.rb
191
- - test/panopticon_api_test.rb
192
184
  - test/contactotron_api_test.rb
185
+ - test/panopticon_api_test.rb
186
+ - test/publisher_api_test.rb
193
187
  - test/rummager_test.rb
188
+ - test/imminence_api_test.rb
189
+ - test/panopticon_registerer_test.rb
194
190
  - test/content_api_test.rb
191
+ - test/json_client_test.rb
192
+ - test/gds_api_base_test.rb
193
+ - test/licence_application_api_test.rb
194
+ - test/test_helper.rb
195
195
  homepage: http://github.com/alphagov/gds-api-adapters
196
196
  licenses: []
197
197
 
@@ -205,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - ">="
207
207
  - !ruby/object:Gem::Version
208
- hash: -2662590031199001168
208
+ hash: 1655582362177723413
209
209
  segments:
210
210
  - 0
211
211
  version: "0"
@@ -214,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
214
214
  requirements:
215
215
  - - ">="
216
216
  - !ruby/object:Gem::Version
217
- hash: -2662590031199001168
217
+ hash: 1655582362177723413
218
218
  segments:
219
219
  - 0
220
220
  version: "0"
@@ -226,14 +226,14 @@ signing_key:
226
226
  specification_version: 3
227
227
  summary: Adapters to work with GDS APIs
228
228
  test_files:
229
- - test/panopticon_registerer_test.rb
230
- - test/gds_api_base_test.rb
231
- - test/json_client_test.rb
232
- - test/test_helper.rb
233
- - test/licence_application_api_test.rb
234
- - test/publisher_api_test.rb
235
- - test/imminence_api_test.rb
236
- - test/panopticon_api_test.rb
237
229
  - test/contactotron_api_test.rb
230
+ - test/panopticon_api_test.rb
231
+ - test/publisher_api_test.rb
238
232
  - test/rummager_test.rb
233
+ - test/imminence_api_test.rb
234
+ - test/panopticon_registerer_test.rb
239
235
  - test/content_api_test.rb
236
+ - test/json_client_test.rb
237
+ - test/gds_api_base_test.rb
238
+ - test/licence_application_api_test.rb
239
+ - test/test_helper.rb