gds-api-adapters 7.25.0 → 8.0.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.
- data/lib/gds_api/content_api.rb +39 -11
- data/lib/gds_api/test_helpers/content_api.rb +66 -23
- data/lib/gds_api/version.rb +1 -1
- data/test/content_api_test.rb +107 -2
- metadata +4 -4
data/lib/gds_api/content_api.rb
CHANGED
@@ -16,31 +16,55 @@ class GdsApi::ContentApi < GdsApi::Base
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def sections
|
19
|
-
|
19
|
+
tags("section")
|
20
20
|
end
|
21
21
|
|
22
22
|
def root_sections
|
23
|
-
|
23
|
+
root_tags("section")
|
24
24
|
end
|
25
25
|
|
26
26
|
def sub_sections(parent_tag)
|
27
|
-
|
27
|
+
child_tags("section", parent_tag)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
30
|
+
def tags(tag_type)
|
31
|
+
get_list!("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}")
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
get_list!("#{base_url}/
|
34
|
+
def root_tags(tag_type)
|
35
|
+
get_list!("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&root_sections=true")
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
get_list("#{base_url}/
|
38
|
+
def child_tags(tag_type, parent_tag)
|
39
|
+
get_list!("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&parent_id=#{CGI.escape(parent_tag)}")
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
43
|
-
|
42
|
+
def tag(tag, tag_type=nil)
|
43
|
+
url = "#{base_url}/tags"
|
44
|
+
|
45
|
+
if tag_type
|
46
|
+
url << "/#{CGI.escape(tag_type)}"
|
47
|
+
end
|
48
|
+
|
49
|
+
get_json("#{url}/#{CGI.escape(tag)}.json")
|
50
|
+
end
|
51
|
+
|
52
|
+
def with_tag(tag, tag_type=nil)
|
53
|
+
tag_key = key_for_tag_type(tag_type)
|
54
|
+
|
55
|
+
get_list!("#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}&include_children=1")
|
56
|
+
end
|
57
|
+
|
58
|
+
def curated_list(tag, tag_type=nil)
|
59
|
+
tag_key = key_for_tag_type(tag_type)
|
60
|
+
|
61
|
+
get_list("#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}&sort=curated")
|
62
|
+
end
|
63
|
+
|
64
|
+
def sorted_by(tag, sort_by, tag_type=nil)
|
65
|
+
tag_key = key_for_tag_type(tag_type)
|
66
|
+
|
67
|
+
get_list!("#{base_url}/with_tag.json?#{tag_key}=#{CGI.escape(tag)}&sort=#{sort_by}")
|
44
68
|
end
|
45
69
|
|
46
70
|
def for_need(need_id)
|
@@ -141,4 +165,8 @@ class GdsApi::ContentApi < GdsApi::Base
|
|
141
165
|
batch_response
|
142
166
|
end
|
143
167
|
end
|
168
|
+
|
169
|
+
def key_for_tag_type(tag_type)
|
170
|
+
tag_type.nil? ? "tag" : CGI.escape(tag_type)
|
171
|
+
end
|
144
172
|
end
|
@@ -13,10 +13,22 @@ module GdsApi
|
|
13
13
|
# Takes an array of slugs, or hashes with section details (including a slug).
|
14
14
|
# Will stub out content_api calls for tags of type section to return these sections
|
15
15
|
def content_api_has_root_sections(slugs_or_sections)
|
16
|
+
content_api_has_root_tags("section", slugs_or_sections)
|
17
|
+
end
|
18
|
+
|
19
|
+
def content_api_has_section(slug_or_hash, parent_slug=nil)
|
20
|
+
content_api_has_tag("section", slug_or_hash, parent_slug)
|
21
|
+
end
|
22
|
+
|
23
|
+
def content_api_has_subsections(parent_slug_or_hash, subsection_slugs)
|
24
|
+
content_api_has_child_tags("section", parent_slug_or_hash, subsection_slugs)
|
25
|
+
end
|
26
|
+
|
27
|
+
def content_api_has_root_tags(tag_type, slugs_or_tags)
|
16
28
|
body = plural_response_base.merge(
|
17
|
-
"results" =>
|
29
|
+
"results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
|
18
30
|
)
|
19
|
-
urls = ["type
|
31
|
+
urls = ["type=#{tag_type}", "root_sections=true&type=#{tag_type}"].map { |q|
|
20
32
|
"#{CONTENT_API_ENDPOINT}/tags.json?#{q}"
|
21
33
|
}
|
22
34
|
urls.each do |url|
|
@@ -24,10 +36,39 @@ module GdsApi
|
|
24
36
|
end
|
25
37
|
end
|
26
38
|
|
27
|
-
def
|
28
|
-
section = tag_hash(slug_or_hash).merge(parent: parent_slug)
|
39
|
+
def content_api_has_tag(tag_type, slug_or_hash, parent_slug=nil)
|
40
|
+
section = tag_hash(slug_or_hash, tag_type).merge(parent: parent_slug)
|
29
41
|
body = tag_result(section)
|
30
|
-
|
42
|
+
|
43
|
+
urls = ["#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag_type)}/#{CGI.escape(section[:slug])}.json"]
|
44
|
+
|
45
|
+
if tag_type == "section"
|
46
|
+
urls << "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(section[:slug])}.json"
|
47
|
+
end
|
48
|
+
|
49
|
+
urls.each do |url|
|
50
|
+
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def content_api_has_tags(tag_type, slugs_or_tags)
|
55
|
+
body = plural_response_base.merge(
|
56
|
+
"results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
|
57
|
+
)
|
58
|
+
|
59
|
+
url = "#{CONTENT_API_ENDPOINT}/tags.json?type=#{tag_type}"
|
60
|
+
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
61
|
+
end
|
62
|
+
|
63
|
+
def content_api_has_child_tags(tag_type, parent_slug_or_hash, subsection_slugs)
|
64
|
+
parent_section = tag_hash(parent_slug_or_hash, tag_type)
|
65
|
+
subsections = subsection_slugs.map { |s|
|
66
|
+
tag_hash(s, tag_type).merge(parent: parent_section)
|
67
|
+
}
|
68
|
+
body = plural_response_base.merge(
|
69
|
+
"results" => subsections.map { |s| tag_result(s, tag_type) }
|
70
|
+
)
|
71
|
+
url = "#{CONTENT_API_ENDPOINT}/tags.json?type=#{tag_type}&parent_id=#{CGI.escape(parent_section[:slug])}"
|
31
72
|
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
32
73
|
end
|
33
74
|
|
@@ -44,18 +85,6 @@ module GdsApi
|
|
44
85
|
end
|
45
86
|
end
|
46
87
|
|
47
|
-
def content_api_has_subsections(parent_slug_or_hash, subsection_slugs)
|
48
|
-
parent_section = tag_hash(parent_slug_or_hash)
|
49
|
-
subsections = subsection_slugs.map { |s|
|
50
|
-
tag_hash(s).merge(parent: parent_section)
|
51
|
-
}
|
52
|
-
body = plural_response_base.merge(
|
53
|
-
"results" => subsections.map { |s| tag_result(s) }
|
54
|
-
)
|
55
|
-
url = "#{CONTENT_API_ENDPOINT}/tags.json?type=section&parent_id=#{CGI.escape(parent_section[:slug])}"
|
56
|
-
stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
|
57
|
-
end
|
58
|
-
|
59
88
|
def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
|
60
89
|
ArtefactStub.new(slug).with_response_body(body).stub
|
61
90
|
end
|
@@ -195,21 +224,22 @@ module GdsApi
|
|
195
224
|
end
|
196
225
|
|
197
226
|
# Construct a tag hash suitable for passing into tag_result
|
198
|
-
def tag_hash(slug_or_hash)
|
227
|
+
def tag_hash(slug_or_hash, tag_type = "section")
|
199
228
|
if slug_or_hash.is_a?(Hash)
|
200
229
|
slug_or_hash
|
201
230
|
else
|
202
|
-
{ slug: slug_or_hash, type:
|
231
|
+
{ slug: slug_or_hash, type: tag_type }
|
203
232
|
end
|
204
233
|
end
|
205
234
|
|
206
|
-
def tag_result(slug_or_hash)
|
207
|
-
tag = tag_hash(slug_or_hash)
|
235
|
+
def tag_result(slug_or_hash, tag_type = nil)
|
236
|
+
tag = tag_hash(slug_or_hash, tag_type)
|
208
237
|
|
209
238
|
parent = tag_result(tag[:parent]) if tag[:parent]
|
239
|
+
pluralized_tag_type = simple_tag_type_pluralizer(tag[:type])
|
210
240
|
|
211
241
|
{
|
212
|
-
"id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag[:slug])}.json",
|
242
|
+
"id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(pluralized_tag_type)}/#{CGI.escape(tag[:slug])}.json",
|
213
243
|
"web_url" => nil,
|
214
244
|
"title" => tag[:title] || titleize_slug(tag[:slug].split("/").last),
|
215
245
|
"details" => {
|
@@ -225,6 +255,19 @@ module GdsApi
|
|
225
255
|
}
|
226
256
|
end
|
227
257
|
|
258
|
+
# This is a nasty hack to get around the pluralized slugs in tag paths
|
259
|
+
# without having to require ActiveSupport
|
260
|
+
#
|
261
|
+
def simple_tag_type_pluralizer(s)
|
262
|
+
case s
|
263
|
+
when /o\Z/ then s.sub(/o\Z/, "es")
|
264
|
+
when /y\Z/ then s.sub(/y\Z/, "ies")
|
265
|
+
when /ss\Z/ then s.sub(/ss\Z/, "sses")
|
266
|
+
else
|
267
|
+
"#{s}s"
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
228
271
|
def setup_content_api_business_support_schemes_stubs
|
229
272
|
@stubbed_content_api_business_support_schemes = []
|
230
273
|
stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/business_support_schemes\.json}).to_return do |request|
|
@@ -281,7 +324,7 @@ module GdsApi
|
|
281
324
|
valid_licences = @stubbed_content_api_licences.select { |l| ids.include? l[:licence_identifier] }
|
282
325
|
{
|
283
326
|
:body => {
|
284
|
-
'results' => valid_licences.map { |licence|
|
327
|
+
'results' => valid_licences.map { |licence|
|
285
328
|
content_api_licence_hash(licence[:licence_identifier], licence)
|
286
329
|
}
|
287
330
|
}.to_json
|
data/lib/gds_api/version.rb
CHANGED
data/test/content_api_test.rb
CHANGED
@@ -81,11 +81,11 @@ describe GdsApi::ContentApi do
|
|
81
81
|
|
82
82
|
# Old-style dictionary access
|
83
83
|
first_section = response["results"][0]
|
84
|
-
assert_equal "#{@base_api_url}/tags/crime.json", first_section["id"]
|
84
|
+
assert_equal "#{@base_api_url}/tags/sections/crime.json", first_section["id"]
|
85
85
|
|
86
86
|
# Also check attribute access
|
87
87
|
first_section = response.first
|
88
|
-
assert_equal "#{@base_api_url}/tags/crime.json", first_section.id
|
88
|
+
assert_equal "#{@base_api_url}/tags/sections/crime.json", first_section.id
|
89
89
|
end
|
90
90
|
|
91
91
|
def section_page_url(page_parameter)
|
@@ -271,6 +271,45 @@ describe GdsApi::ContentApi do
|
|
271
271
|
end
|
272
272
|
|
273
273
|
describe "tags" do
|
274
|
+
it "returns a list of tags of a given type" do
|
275
|
+
content_api_has_tags("author", ["justin-thyme"])
|
276
|
+
response = @api.tags("author")
|
277
|
+
|
278
|
+
# Old-style dictionary access
|
279
|
+
first_section = response["results"][0]
|
280
|
+
assert_equal "#{@base_api_url}/tags/authors/justin-thyme.json", first_section["id"]
|
281
|
+
|
282
|
+
# Also check attribute access
|
283
|
+
first_section = response.first
|
284
|
+
assert_equal "#{@base_api_url}/tags/authors/justin-thyme.json", first_section.id
|
285
|
+
end
|
286
|
+
|
287
|
+
it "returns a list of root tags of a given type" do
|
288
|
+
content_api_has_root_tags("author", ["oliver-sudden", "percy-vere"])
|
289
|
+
response = @api.root_tags("author")
|
290
|
+
|
291
|
+
# Old-style dictionary access
|
292
|
+
first_section = response["results"][0]
|
293
|
+
assert_equal "#{@base_api_url}/tags/authors/oliver-sudden.json", first_section["id"]
|
294
|
+
|
295
|
+
# Also check attribute access
|
296
|
+
first_section = response.first
|
297
|
+
assert_equal "#{@base_api_url}/tags/authors/oliver-sudden.json", first_section.id
|
298
|
+
end
|
299
|
+
|
300
|
+
it "returns a list of child tags of a given type" do
|
301
|
+
content_api_has_child_tags("genre", "indie", ["indie/indie-rock"])
|
302
|
+
response = @api.child_tags("genre", "indie")
|
303
|
+
|
304
|
+
# Old-style dictionary access
|
305
|
+
first_section = response["results"][0]
|
306
|
+
assert_equal "#{@base_api_url}/tags/genres/indie%2Findie-rock.json", first_section["id"]
|
307
|
+
|
308
|
+
# Also check attribute access
|
309
|
+
first_section = response.first
|
310
|
+
assert_equal "#{@base_api_url}/tags/genres/indie%2Findie-rock.json", first_section.id
|
311
|
+
end
|
312
|
+
|
274
313
|
it "should produce an artefact with the provided tag" do
|
275
314
|
tag = "crime-and-justice"
|
276
315
|
api_url = "#{@base_api_url}/with_tag.json?tag=#{tag}&include_children=1"
|
@@ -288,6 +327,17 @@ describe GdsApi::ContentApi do
|
|
288
327
|
assert_equal "Complain about a claims company", response.first.title
|
289
328
|
end
|
290
329
|
|
330
|
+
it "returns artefacts given a tag and tag type" do
|
331
|
+
api_url = "#{@base_api_url}/with_tag.json?genre=reggae&include_children=1"
|
332
|
+
json = {
|
333
|
+
results: [{title: "Three Little Birds"}]
|
334
|
+
}
|
335
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
336
|
+
response = @api.with_tag("reggae", "genre")
|
337
|
+
|
338
|
+
assert_equal "Three Little Birds", response.first.title
|
339
|
+
end
|
340
|
+
|
291
341
|
it "should return tag tree for a specific tag" do
|
292
342
|
tag = "crime-and-justice"
|
293
343
|
api_url = "#{@base_api_url}/tags/#{tag}.json"
|
@@ -299,6 +349,61 @@ describe GdsApi::ContentApi do
|
|
299
349
|
title = response['title']
|
300
350
|
assert_equal json[:title], title
|
301
351
|
end
|
352
|
+
|
353
|
+
it "returns tag information given a tag and tag type" do
|
354
|
+
api_url = "#{@base_api_url}/tags/genre/reggae.json"
|
355
|
+
json = {
|
356
|
+
title: "Reggae"
|
357
|
+
}
|
358
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
359
|
+
response = @api.tag("reggae", "genre")
|
360
|
+
|
361
|
+
assert_equal "Reggae", response['title']
|
362
|
+
end
|
363
|
+
|
364
|
+
it "returns artefacts for a tag in curated list order" do
|
365
|
+
api_url = "#{@base_api_url}/with_tag.json?tag=crime-and-justice&sort=curated"
|
366
|
+
json = {
|
367
|
+
results: [{title: "Complain about a claims company"}]
|
368
|
+
}
|
369
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
370
|
+
response = @api.curated_list("crime-and-justice")
|
371
|
+
|
372
|
+
assert_equal "Complain about a claims company", response.first.title
|
373
|
+
end
|
374
|
+
|
375
|
+
it "returns artefacts in curated list order for a tag and tag type" do
|
376
|
+
api_url = "#{@base_api_url}/with_tag.json?genre=reggae&sort=curated"
|
377
|
+
json = {
|
378
|
+
results: [{title: "Buffalo Soldier"}]
|
379
|
+
}
|
380
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
381
|
+
response = @api.curated_list("reggae", "genre")
|
382
|
+
|
383
|
+
assert_equal "Buffalo Soldier", response.first.title
|
384
|
+
end
|
385
|
+
|
386
|
+
it "returns artefacts for a tag in a given sort order" do
|
387
|
+
api_url = "#{@base_api_url}/with_tag.json?tag=crime-and-justice&sort=foo"
|
388
|
+
json = {
|
389
|
+
results: [{title: "Complain about a claims company"}]
|
390
|
+
}
|
391
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
392
|
+
response = @api.sorted_by("crime-and-justice", "foo")
|
393
|
+
|
394
|
+
assert_equal "Complain about a claims company", response.first.title
|
395
|
+
end
|
396
|
+
|
397
|
+
it "returns artefacts in a given sort order for a tag and tag type" do
|
398
|
+
api_url = "#{@base_api_url}/with_tag.json?genre=reggae&sort=foo"
|
399
|
+
json = {
|
400
|
+
results: [{title: "Is This Love"}]
|
401
|
+
}
|
402
|
+
stub_request(:get, api_url).to_return(:status => 200, :body => json.to_json)
|
403
|
+
response = @api.sorted_by("reggae", "foo", "genre")
|
404
|
+
|
405
|
+
assert_equal "Is This Love", response.first.title
|
406
|
+
end
|
302
407
|
end
|
303
408
|
|
304
409
|
describe "licence" do
|
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:
|
4
|
+
version: 8.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-01-
|
12
|
+
date: 2014-01-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: plek
|
@@ -343,7 +343,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
343
343
|
version: '0'
|
344
344
|
segments:
|
345
345
|
- 0
|
346
|
-
hash: -
|
346
|
+
hash: -2067444062740093796
|
347
347
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
348
348
|
none: false
|
349
349
|
requirements:
|
@@ -352,7 +352,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
352
352
|
version: '0'
|
353
353
|
segments:
|
354
354
|
- 0
|
355
|
-
hash: -
|
355
|
+
hash: -2067444062740093796
|
356
356
|
requirements: []
|
357
357
|
rubyforge_project:
|
358
358
|
rubygems_version: 1.8.23
|