gds-api-adapters 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -60,6 +60,11 @@ 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 licences_for_ids(ids)
64
+ ids = ids.map(&:to_s).sort.join(',')
65
+ get_json("#{@endpoint}/licences.json?ids=#{ids}")
66
+ end
67
+
63
68
  def business_support_schemes(identifiers)
64
69
  identifiers = identifiers.map {|i| CGI.escape(i) }
65
70
  url_template = "#{base_url}/business_support_schemes.json?identifiers="
@@ -38,12 +38,6 @@ class GdsApi::Publisher < GdsApi::Base
38
38
  json.to_hash if json
39
39
  end
40
40
 
41
- def licences_for_ids(ids)
42
- ids = ids.map(&:to_s).sort.join(',')
43
- response = get_json("#{@endpoint}/licences.json?ids=#{ids}")
44
- response.to_ostruct if response
45
- end
46
-
47
41
  private
48
42
  def convert_updated_date(container)
49
43
  if container.updated_at && container.updated_at.class == String
@@ -228,6 +228,60 @@ module GdsApi
228
228
  @stubbed_content_api_business_support_schemes << scheme
229
229
  end
230
230
 
231
+ def content_api_licence_hash(licence_identifier, options = {})
232
+ details = {
233
+ title: "Publisher title",
234
+ slug: 'licence-slug',
235
+ licence_short_description: "Short description of licence"
236
+ }
237
+ details.merge!(options)
238
+
239
+ {
240
+ "title" => details[:title],
241
+ "id" => "http://example.org/#{details[:slug]}.json",
242
+ "web_url" => "http://www.test.gov.uk/#{details[:slug]}",
243
+ "format" => "licence",
244
+ "details" => {
245
+ "need_id" => nil,
246
+ "business_proposition" => false,
247
+ "alternative_title" => nil,
248
+ "overview" => nil,
249
+ "will_continue_on" => nil,
250
+ "continuation_link" => nil,
251
+ "licence_identifier" => licence_identifier,
252
+ "licence_short_description" => details[:licence_short_description],
253
+ "licence_overview" => nil,
254
+ "updated_at" => "2012-10-06T12:00:05+01:00"
255
+ },
256
+ "tags" => [],
257
+ "related" => []
258
+ }
259
+ end
260
+
261
+ def setup_content_api_licences_stubs
262
+ @stubbed_content_api_licences = []
263
+ stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/licences}).to_return do |request|
264
+ if request.uri.query_values and request.uri.query_values["ids"]
265
+ ids = request.uri.query_values["ids"].split(',')
266
+ valid_licences = @stubbed_content_api_licences.select { |l| ids.include? l[:licence_identifier] }
267
+ {
268
+ :body => {
269
+ 'results' => valid_licences.map { |licence|
270
+ content_api_licence_hash(licence[:licence_identifier], licence)
271
+ }
272
+ }.to_json
273
+ }
274
+ else
275
+ {:body => {'results' => []}.to_json}
276
+ end
277
+ end
278
+ end
279
+
280
+ def content_api_has_licence(details)
281
+ raise "Need a licence identifier" if details[:licence_identifier].nil?
282
+ @stubbed_content_api_licences << details
283
+ end
284
+
231
285
  private
232
286
 
233
287
  def titleize_slug(slug)
@@ -42,23 +42,6 @@ module GdsApi
42
42
  uri = "#{PUBLISHER_ENDPOINT}/local_transactions/#{slug}.json"
43
43
  stub_request(:post, uri).to_return(:body => "", :status => 404)
44
44
  end
45
-
46
- def setup_publisher_licences_stubs
47
- @stubbed_publisher_licences = []
48
- stub_request(:get, %r{\A#{PUBLISHER_ENDPOINT}/licences}).to_return do |request|
49
- if request.uri.query_values and request.uri.query_values["ids"]
50
- ids = request.uri.query_values["ids"].split(',')
51
- {:body => @stubbed_publisher_licences.select {|l| ids.include? l[:licence_identifier] }.to_json}
52
- else
53
- {:body => [].to_json}
54
- end
55
- end
56
- end
57
-
58
- def publisher_has_licence(details)
59
- raise "Need a licence identifier" if details[:licence_identifier].nil?
60
- @stubbed_publisher_licences << details
61
- end
62
45
  end
63
46
  end
64
47
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '3.2.0'
2
+ VERSION = '3.3.0'
3
3
  end
@@ -99,7 +99,7 @@ describe GdsApi::ContentApi do
99
99
 
100
100
  it "should return an unpublished artefact with a snac code" do
101
101
  body = artefact_for_slug('licence-example')
102
- url = "#{CONTENT_API_ENDPOINT}/licence-example.json?snac=1234&edition=1"
102
+ url = "#{@base_api_url}/licence-example.json?snac=1234&edition=1"
103
103
  stub_request(:get, url).to_return(status: 200, body: body.to_json)
104
104
 
105
105
  api = GdsApi::ContentApi.new('test', { bearer_token: 'MY_BEARER_TOKEN' })
@@ -358,6 +358,42 @@ describe GdsApi::ContentApi do
358
358
  end
359
359
  end
360
360
 
361
+ describe "getting licence details" do
362
+ it "should get licence details" do
363
+ setup_content_api_licences_stubs
364
+
365
+ content_api_has_licence :licence_identifier => "1234", :title => 'Test Licence 1', :slug => 'test-licence-1',
366
+ :licence_short_description => 'A short description'
367
+ content_api_has_licence :licence_identifier => "1235", :title => 'Test Licence 2', :slug => 'test-licence-2',
368
+ :licence_short_description => 'A short description'
369
+ content_api_has_licence :licence_identifier => "AB1234", :title => 'Test Licence 3', :slug => 'test-licence-3',
370
+ :licence_short_description => 'A short description'
371
+
372
+ results = @api.licences_for_ids([1234, 'AB1234', 'something']).to_ostruct.results
373
+ assert_equal 2, results.size
374
+ assert_equal ['1234', 'AB1234'], results.map { |r| r.details.licence_identifier }
375
+ assert_equal ['Test Licence 1', 'Test Licence 3'], results.map(&:title).sort
376
+ assert_equal ['http://www.test.gov.uk/test-licence-1', 'http://www.test.gov.uk/test-licence-3'], results.map(&:web_url).sort
377
+ assert_equal 'A short description', results[0].details.licence_short_description
378
+ assert_equal 'A short description', results[1].details.licence_short_description
379
+ end
380
+
381
+ it "should return empty array with no licences" do
382
+ setup_content_api_licences_stubs
383
+
384
+ assert_equal [], @api.licences_for_ids([123,124]).to_ostruct.results
385
+ end
386
+
387
+ it "should raise an error if publisher returns an error" do
388
+ stub_request(:get, %r[\A#{@base_api_url}/licences]).
389
+ to_return(:status => [503, "Service temporarily unabailable"])
390
+
391
+ assert_raises GdsApi::HTTPErrorResponse do
392
+ @api.licences_for_ids([123,124])
393
+ end
394
+ end
395
+ end
396
+
361
397
  def api_response_for_results(results)
362
398
  {
363
399
  "_response_info" => {
@@ -106,83 +106,4 @@ describe GdsApi::Publisher do
106
106
  to_return(:status => 200, :body => '{"snac": "12345"}', :headers => {})
107
107
  assert_equal '12345', api.council_for_slug('fake-transaction', [12345])
108
108
  end
109
-
110
- describe "getting licence details from publisher" do
111
- it "should get licence details from publisher" do
112
- setup_publisher_licences_stubs
113
-
114
- publisher_has_licence :licence_identifier => "1234", :title => 'Test Licence 1', :slug => 'test-licence-1',
115
- :licence_short_description => 'A short description'
116
- publisher_has_licence :licence_identifier => "1235", :title => 'Test Licence 2', :slug => 'test-licence-2',
117
- :licence_short_description => 'A short description'
118
- publisher_has_licence :licence_identifier => "AB1234", :title => 'Test Licence 3', :slug => 'test-licence-3',
119
- :licence_short_description => 'A short description'
120
-
121
- results = api.licences_for_ids([1234, 'AB1234', 'something'])
122
- assert_equal 2, results.size
123
- assert_equal ['1234', 'AB1234'], results.map(&:licence_identifier)
124
- assert_equal ['Test Licence 1', 'Test Licence 3'], results.map(&:title).sort
125
- assert_equal ['test-licence-1', 'test-licence-3'], results.map(&:slug).sort
126
- assert_equal 'A short description', results[0].licence_short_description
127
- assert_equal 'A short description', results[1].licence_short_description
128
- end
129
-
130
- it "should return empty array with no licences" do
131
- setup_publisher_licences_stubs
132
-
133
- assert_equal [], api.licences_for_ids([123,124])
134
- end
135
-
136
- it "should raise an error if publisher returns an error" do
137
- stub_request(:get, %r[\A#{PUBLISHER_ENDPOINT}/licences]).
138
- to_return(:status => [503, "Service temporarily unabailable"])
139
-
140
- assert_raises GdsApi::HTTPErrorResponse do
141
- api.licences_for_ids([123,124])
142
- end
143
- end
144
-
145
- it "should return nil if a council snac code is not found" do
146
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_snac?snac=bloop").
147
- with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
148
- to_return(:status => 404, :body => " ", :headers => {})
149
-
150
- assert_equal nil, api.council_for_snac_code("bloop")
151
- end
152
-
153
- it "should return a council hash for a snac code" do
154
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_snac?snac=AA00").
155
- with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
156
- to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
157
-
158
- expected = {"name" => "Some Council", "snac" => "AA00"}
159
- assert_equal expected, api.council_for_snac_code("AA00")
160
- end
161
-
162
- it "should return nil if a council name is not found" do
163
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=bloop").
164
- with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
165
- to_return(:status => 404, :body => " ", :headers => {})
166
-
167
- assert_equal nil, api.council_for_name("bloop")
168
- end
169
-
170
- it "should return a council hash for a mixed case council name" do
171
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=Some%20Council").
172
- with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
173
- to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
174
-
175
- expected = {"name" => "Some Council", "snac" => "AA00"}
176
- assert_equal expected, api.council_for_name("Some Council")
177
- end
178
-
179
- it "should return a council hash for a lowercase council name" do
180
- stub_request(:get, "#{PUBLISHER_ENDPOINT}/local_transactions/find_by_council_name?name=some%20council").
181
- with(:headers => GdsApi::JsonClient::DEFAULT_REQUEST_HEADERS).
182
- to_return(:status => 200, :body => '{"name": "Some Council", "snac": "AA00"}', :headers => {})
183
-
184
- expected = {"name" => "Some Council", "snac" => "AA00"}
185
- assert_equal expected, api.council_for_name("some council")
186
- end
187
- end
188
109
  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: 3.2.0
5
+ version: 3.3.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-05 00:00:00 Z
13
+ date: 2012-10-08 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -205,7 +205,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
205
205
  requirements:
206
206
  - - ">="
207
207
  - !ruby/object:Gem::Version
208
- hash: 827207644748547205
208
+ hash: 604582119140206169
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: 827207644748547205
217
+ hash: 604582119140206169
218
218
  segments:
219
219
  - 0
220
220
  version: "0"