gds-api-adapters 9.2.0 → 10.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,7 @@ app.
4
4
  Example usage:
5
5
 
6
6
  publisher_api = GdsApi::Publisher.new("environment")
7
- ostruct_publication = publisher.publication_for_slug('my-published-item')
7
+ ostruct_publication = publisher_api.publication_for_slug('my-published-item')
8
8
 
9
9
  panopticon_api = GdsApi::Panopticon.new("environment")
10
10
  ostruct_metadata = panopticon_api.artefact_for_slug('my-published-item')
@@ -108,23 +108,14 @@ class GdsApi::ContentApi < GdsApi::Base
108
108
  get_json("#{@endpoint}/licences.json?ids=#{ids}")
109
109
  end
110
110
 
111
- def business_support_schemes(identifiers)
112
- identifiers = identifiers.map {|i| CGI.escape(i) }
113
- url_template = "#{base_url}/business_support_schemes.json?identifiers="
114
- response = nil # assignment necessary for variable scoping
115
-
116
- start_url = "#{url_template}#{identifiers.shift}"
117
- last_batch_url = identifiers.inject(start_url) do |url, id|
118
- new_url = [url, id].join(',')
119
- if new_url.length >= 2000
120
- # fetch a batch using the previous url, then return a new start URL with this id
121
- response = get_batch(url, response)
122
- "#{url_template}#{id}"
123
- else
124
- new_url
125
- end
111
+ def business_support_schemes(facets)
112
+ url = "#{base_url}/business_support_schemes.json"
113
+ query = facets.map { |k,v| "#{k}=#{v}" }
114
+ if query.any?
115
+ url += "?#{query.join("&")}"
126
116
  end
127
- get_batch(last_batch_url, response)
117
+
118
+ get_json!(url)
128
119
  end
129
120
 
130
121
  def get_list!(url)
@@ -158,17 +149,6 @@ class GdsApi::ContentApi < GdsApi::Base
158
149
  endpoint
159
150
  end
160
151
 
161
- def get_batch(batch_url, existing_response = nil)
162
- batch_response = get_json!(batch_url)
163
- if existing_response
164
- existing_response.to_hash["total"] += batch_response["total"]
165
- existing_response.to_hash["results"] += batch_response["results"]
166
- existing_response
167
- else
168
- batch_response
169
- end
170
- end
171
-
172
152
  def key_for_tag_type(tag_type)
173
153
  tag_type.nil? ? "tag" : CGI.escape(tag_type)
174
154
  end
@@ -1,35 +1,20 @@
1
- require 'gds_api/test_helpers/json_client_helper'
2
- require 'cgi'
3
- require 'gds_api/test_helpers/common_responses'
1
+ require 'gds_api/test_helpers/business_support_helper'
4
2
 
5
3
  module GdsApi
6
4
  module TestHelpers
7
5
  module BusinessSupportApi
8
6
  include GdsApi::TestHelpers::CommonResponses
7
+ include GdsApi::TestHelpers::BusinessSupportHelper
9
8
  # Generally true. If you are initializing the client differently,
10
9
  # you could redefine/override the constant or stub directly.
11
10
  BUSINESS_SUPPORT_API_ENDPOINT = Plek.current.find('business-support-api')
12
11
 
13
12
  def setup_business_support_api_schemes_stubs
14
- @stubbed_business_support_schemes = {}
15
- stub_request(:get, %r{\A#{BUSINESS_SUPPORT_API_ENDPOINT}/business-support-schemes\.json}).to_return do |request|
16
- if request.uri.query_values
17
- key = facet_key(request.uri.query_values)
18
- results = @stubbed_business_support_schemes[key] || []
19
- else
20
- results = @stubbed_business_support_schemes['default']
21
- end
22
- {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json}
23
- end
24
-
13
+ setup_business_support_stubs(BUSINESS_SUPPORT_API_ENDPOINT, 'business-support-schemes')
25
14
  end
26
15
 
27
16
  def business_support_api_has_scheme(scheme, facets={})
28
- key = facet_key(facets)
29
- unless @stubbed_business_support_schemes.has_key?(key)
30
- @stubbed_business_support_schemes[key] = []
31
- end
32
- @stubbed_business_support_schemes[key] << scheme
17
+ api_has_business_support(scheme, facets)
33
18
  end
34
19
 
35
20
  def business_support_api_has_schemes(schemes, facets={})
@@ -44,14 +29,6 @@ module GdsApi
44
29
  {:body => response_base.merge(:format => 'business_support', :title => title, :details => scheme).to_json}
45
30
  end
46
31
  end
47
-
48
- private
49
-
50
- def facet_key(facets)
51
- key = 'default'
52
- key = facets.values.sort.hash.to_s if facets and !facets.empty?
53
- key
54
- end
55
32
  end
56
33
  end
57
34
  end
@@ -0,0 +1,40 @@
1
+ require 'gds_api/test_helpers/json_client_helper'
2
+ require 'cgi'
3
+ require 'gds_api/test_helpers/common_responses'
4
+
5
+
6
+ module GdsApi
7
+ module TestHelpers
8
+ module BusinessSupportHelper
9
+ def setup_business_support_stubs(endpoint, path)
10
+ @stubbed_business_supports = {}
11
+ stub_request(:get, %r{\A#{endpoint}/#{path}\.json}).to_return do |request|
12
+ if request.uri.query_values
13
+ key = facet_key(request.uri.query_values)
14
+ results = @stubbed_business_supports[key] || []
15
+ else
16
+ results = @stubbed_business_supports['default']
17
+ end
18
+ {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json}
19
+ end
20
+
21
+ end
22
+
23
+ def api_has_business_support(business_support, facets={})
24
+ key = facet_key(facets)
25
+ unless @stubbed_business_supports.has_key?(key)
26
+ @stubbed_business_supports[key] = []
27
+ end
28
+ @stubbed_business_supports[key] << business_support
29
+ end
30
+
31
+ private
32
+
33
+ def facet_key(facets)
34
+ key = 'default'
35
+ key = facets.values.flatten.sort.hash.to_s if facets and !facets.empty?
36
+ key
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,11 +1,13 @@
1
1
  require 'gds_api/test_helpers/json_client_helper'
2
2
  require 'cgi'
3
3
  require 'gds_api/test_helpers/common_responses'
4
+ require 'gds_api/test_helpers/business_support_helper'
4
5
 
5
6
  module GdsApi
6
7
  module TestHelpers
7
8
  module ContentApi
8
9
  include GdsApi::TestHelpers::CommonResponses
10
+ include GdsApi::TestHelpers::BusinessSupportHelper
9
11
  # Generally true. If you are initializing the client differently,
10
12
  # you could redefine/override the constant or stub directly.
11
13
  CONTENT_API_ENDPOINT = Plek.current.find('contentapi')
@@ -342,21 +344,23 @@ module GdsApi
342
344
  end
343
345
 
344
346
  def setup_content_api_business_support_schemes_stubs
345
- @stubbed_content_api_business_support_schemes = []
346
- stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).to_return do |request|
347
- if request.uri.query_values and request.uri.query_values["identifiers"]
348
- ids = request.uri.query_values["identifiers"].split(',')
349
- results = @stubbed_content_api_business_support_schemes.select {|bs| ids.include? bs["identifier"] }
350
- else
351
- results = []
352
- end
353
- {:body => plural_response_base.merge("results" => results, "total" => results.size).to_json}
354
- end
347
+ setup_business_support_stubs(CONTENT_API_ENDPOINT, 'business_support_schemes')
355
348
  end
356
349
 
357
- def content_api_has_business_support_scheme(scheme)
358
- raise "Need an identifier" if scheme["identifier"].nil?
359
- @stubbed_content_api_business_support_schemes << scheme
350
+ def content_api_has_business_support_scheme(scheme, facets)
351
+ api_has_business_support(scheme, facets)
352
+ end
353
+
354
+ def content_api_has_default_business_support_schemes
355
+ empty_results = {
356
+ "_response_info" => {"status" => "ok"},
357
+ "description" => "Business Support Schemes!",
358
+ "total" => 0, "startIndex" => 1, "pageSize" => 0, "currentPage" => 1, "pages" => 1,
359
+ "results" => []
360
+ }
361
+
362
+ stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).
363
+ to_return(:body => empty_results.to_json)
360
364
  end
361
365
 
362
366
  def content_api_licence_hash(licence_identifier, options = {})
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '9.2.0'
2
+ VERSION = '10.0.0'
3
3
  end
@@ -576,30 +576,10 @@ describe GdsApi::ContentApi do
576
576
  stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
577
577
  to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
578
578
 
579
- response = @api.business_support_schemes(['foo', 'bar'])
579
+ response = @api.business_support_schemes(:drink => "coffee")
580
580
 
581
581
  assert_equal({"foo" => "bar"}, response.to_hash)
582
- assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=foo,bar", :times => 1
583
- end
584
-
585
- it "should CGI escape identifiers" do
586
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
587
- to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
588
-
589
- response = @api.business_support_schemes(['foo bar', 'baz&bing'])
590
-
591
- assert_equal({"foo" => "bar"}, response.to_hash)
592
- assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=foo%20bar,baz%26bing", :times => 1
593
- end
594
-
595
- it "should not modify the given array" do
596
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes.json}).
597
- to_return(:status => 200, :body => {"foo" => "bar"}.to_json)
598
-
599
- ids = %w(foo bar baz)
600
- @api.business_support_schemes(ids)
601
-
602
- assert_equal %w(foo bar baz), ids
582
+ assert_requested :get, "#{@base_api_url}/business_support_schemes.json?drink=coffee", :times => 1
603
583
  end
604
584
 
605
585
  it "should raise an error if content_api returns 404" do
@@ -620,58 +600,17 @@ describe GdsApi::ContentApi do
620
600
  end
621
601
  end
622
602
 
623
- describe "handling requests that would have a URI in excess of 2000 chars" do
624
- before :each do
625
- stub_request(:get, %r{\A#{@base_api_url}/business_support_schemes\.json}).
626
- to_return(:status => 200, :body => api_response_for_results([{"foo" => "bar"}]).to_json)
627
- end
628
-
629
- it "should do the request in batches" do
630
- ids = (1..300).map {|n| sprintf "%09d", n } # each id is 9 chars long
631
-
632
- response = @api.business_support_schemes(ids)
633
-
634
- assert_requested :get, %r{\A#{@base_api_url}/business_support_schemes\.json}, :times => 2
635
-
636
- first_batch = ids[0..191]
637
- assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=#{first_batch.join(',')}"
638
- second_batch = ids[192..299]
639
- assert_requested :get, "#{@base_api_url}/business_support_schemes.json?identifiers=#{second_batch.join(',')}"
640
- end
641
-
642
- it "should merge the responses into a single GdsApi::Response" do
643
- ids = (1..300).map {|n| sprintf "%09d", n } # each id is 9 chars long
644
- first_batch = ids[0..191]
645
- stub_request(:get, "#{@base_api_url}/business_support_schemes.json").
646
- with(:query => {"identifiers" => first_batch.join(',')}).
647
- to_return(:status => 200, :body => api_response_for_results(first_batch).to_json) # We're stubbing response that just return the requested ids
648
- second_batch = ids[192..299]
649
- stub_request(:get, "#{@base_api_url}/business_support_schemes.json").
650
- with(:query => {"identifiers" => second_batch.join(',')}).
651
- to_return(:status => 200, :body => api_response_for_results(second_batch).to_json)
652
-
653
- response = @api.business_support_schemes(ids)
654
-
655
- # Assert both Hash an OpenStruct access to ensure nothing's been memoized part-way through merging stuff
656
- assert_equal 300, response["total"]
657
- assert_equal ids, response["results"]
658
-
659
- assert_equal 300, response.total
660
- assert_equal ids, response.results
661
- end
662
- end
663
-
664
603
  describe "test helpers" do
665
604
  it "should have representative test helpers" do
666
605
  setup_content_api_business_support_schemes_stubs
667
- s1 = { "title" => "Scheme 1", "identifier" => "s1", "format" => "business_support" }
668
- content_api_has_business_support_scheme(s1)
669
- s2 = { "title" => "Scheme 2", "identifier" => "s2", "format" => "business_support" }
670
- content_api_has_business_support_scheme(s2)
671
- s3 = { "title" => "Scheme 3", "identifier" => "s3", "format" => "business_support" }
672
- content_api_has_business_support_scheme(s3)
673
-
674
- response = @api.business_support_schemes(['s1', 's3']).to_hash
606
+ s1 = { "title" => "Scheme 1", "format" => "business_support" }
607
+ content_api_has_business_support_scheme(s1, :locations => "england", :sectors => "farming")
608
+ s2 = { "title" => "Scheme 2", "format" => "business_support" }
609
+ content_api_has_business_support_scheme(s2, :sectors => "farming")
610
+ s3 = { "title" => "Scheme 3", "format" => "business_support" }
611
+ content_api_has_business_support_scheme(s3, :locations => "england", :sectors => "farming")
612
+
613
+ response = @api.business_support_schemes(:locations => "england", :sectors => "farming").to_hash
675
614
 
676
615
  assert_equal 2, response["total"]
677
616
  assert_equal [s1, s3], response["results"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.2.0
4
+ version: 10.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-28 00:00:00.000000000 Z
12
+ date: 2014-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -294,6 +294,7 @@ files:
294
294
  - lib/gds_api/test_helpers/performance_platform/data_in.rb
295
295
  - lib/gds_api/test_helpers/licence_application.rb
296
296
  - lib/gds_api/test_helpers/organisations.rb
297
+ - lib/gds_api/test_helpers/business_support_helper.rb
297
298
  - lib/gds_api/test_helpers/imminence.rb
298
299
  - lib/gds_api/test_helpers/worldwide.rb
299
300
  - lib/gds_api/test_helpers/mapit.rb
@@ -367,7 +368,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
367
368
  version: '0'
368
369
  segments:
369
370
  - 0
370
- hash: 3249051588083426737
371
+ hash: 21857638136048876
371
372
  required_rubygems_version: !ruby/object:Gem::Requirement
372
373
  none: false
373
374
  requirements:
@@ -376,7 +377,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
376
377
  version: '0'
377
378
  segments:
378
379
  - 0
379
- hash: 3249051588083426737
380
+ hash: 21857638136048876
380
381
  requirements: []
381
382
  rubyforge_project:
382
383
  rubygems_version: 1.8.23