govuk_taxonomy_helpers 0.1.1 → 1.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/Jenkinsfile +2 -1
- data/README.md +4 -7
- data/lib/govuk_taxonomy_helpers/publishing_api_response.rb +20 -23
- data/lib/govuk_taxonomy_helpers/version.rb +1 -1
- data/spec/publishing_api_response_spec.rb +102 -35
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5914eebd6bd1d3dea7a0d464950ad850baf9c6e
|
4
|
+
data.tar.gz: 222098a1b3402e73f9d2cb6d4ab96310812fdaa4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 253c283e3e433a2f8d6d154ef2535d04989737c2f0ec7b40a2448503838a61952efd9e8c0dacac864c8d1fbd0004acbb94ed10ef6f9e0921f0c0bfbc6624897f
|
7
|
+
data.tar.gz: 6d53adbc9c998dc3fa7333b689bc3c218f7b53e0eaba71df8787be7f01e53bfd56113fcf8f91f04d759e79ab9aea82ef12330012a3647f16d55a5b47eee8ebbc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
## 1.0.0
|
2
|
+
|
3
|
+
* Merges from_content_id and from_publishing_api into one builder method contained
|
4
|
+
in LinkedContentItem. The homepage can now have it's links expanded through
|
5
|
+
it's level_one_taxons by using the content_id of the homepage to form a nested tree from
|
6
|
+
the root.
|
7
|
+
|
1
8
|
## 0.1.1
|
2
9
|
|
3
10
|
* Adds the from_content_id builder method to LinkedContentItem
|
data/Jenkinsfile
CHANGED
data/README.md
CHANGED
@@ -22,17 +22,14 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
The API is provisional and is likely to change for versions < 1.0.0.
|
24
24
|
|
25
|
-
To access the taxonomy, first
|
25
|
+
To access the taxonomy, first get the content_id of the piece of content you want the taxonomy for, then parse it to get a `LinkedContentItem` object.
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
require 'govuk_taxonomy_helpers'
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
taxonomy = GovukTaxonomyHelpers::LinkedContentItem.from_publishing_api(
|
34
|
-
content_item: content_item,
|
35
|
-
expanded_links: expanded_links
|
30
|
+
taxonomy = GovukTaxonomyHelpers::LinkedContentItem.from_content_id(
|
31
|
+
content_id: "c75c541-403f-4cb1-9b34-4ddde816a80d",
|
32
|
+
publishing_api: Services.publishing_api
|
36
33
|
)
|
37
34
|
```
|
38
35
|
|
@@ -1,29 +1,16 @@
|
|
1
1
|
module GovukTaxonomyHelpers
|
2
2
|
class LinkedContentItem
|
3
|
-
# Extract a LinkedContentItem from publishing api response data.
|
4
|
-
#
|
5
|
-
# @param content_item [Hash] Publishing API `get_content` response hash
|
6
|
-
# @param expanded_links [Hash] Publishing API `get_expanded_links` response hash
|
7
|
-
# @return [LinkedContentItem]
|
8
|
-
# @see http://www.rubydoc.info/gems/gds-api-adapters/GdsApi/PublishingApiV2#get_content-instance_method
|
9
|
-
# @see http://www.rubydoc.info/gems/gds-api-adapters/GdsApi%2FPublishingApiV2:get_expanded_links
|
10
|
-
def self.from_publishing_api(content_item:, expanded_links:)
|
11
|
-
PublishingApiResponse.new(
|
12
|
-
content_item: content_item,
|
13
|
-
expanded_links: expanded_links,
|
14
|
-
).linked_content_item
|
15
|
-
end
|
16
|
-
|
17
3
|
# Use the publishing API service to fetch and extract a LinkedContentItem
|
18
4
|
#
|
19
5
|
# @param content_id [String] id of the content
|
20
6
|
# @param publishing_api [PublishingApiV2] Publishing API service
|
21
7
|
# @return [LinkedContentItem]
|
22
8
|
def self.from_content_id(content_id:, publishing_api:)
|
23
|
-
|
9
|
+
PublishingApiResponse.new(
|
24
10
|
content_item: publishing_api.get_content(content_id).to_h,
|
25
|
-
expanded_links: publishing_api.get_expanded_links(content_id).to_h
|
26
|
-
|
11
|
+
expanded_links: publishing_api.get_expanded_links(content_id).to_h,
|
12
|
+
publishing_api: publishing_api
|
13
|
+
).linked_content_item
|
27
14
|
end
|
28
15
|
end
|
29
16
|
|
@@ -32,7 +19,8 @@ module GovukTaxonomyHelpers
|
|
32
19
|
|
33
20
|
# @param content_item [Hash] Publishing API `get_content` response hash
|
34
21
|
# @param expanded_links [Hash] Publishing API `get_expanded_links` response hash
|
35
|
-
|
22
|
+
# @param publishing_api [PublishingApiV2] Publishing API service
|
23
|
+
def initialize(content_item:, expanded_links:, publishing_api:)
|
36
24
|
details = content_item["details"] || {}
|
37
25
|
|
38
26
|
@linked_content_item = LinkedContentItem.new(
|
@@ -42,30 +30,39 @@ module GovukTaxonomyHelpers
|
|
42
30
|
base_path: content_item["base_path"]
|
43
31
|
)
|
44
32
|
|
45
|
-
add_expanded_links(expanded_links)
|
33
|
+
add_expanded_links(expanded_links, publishing_api)
|
46
34
|
end
|
47
35
|
|
48
36
|
private
|
49
37
|
|
50
|
-
def add_expanded_links(expanded_links_response)
|
38
|
+
def add_expanded_links(expanded_links_response, publishing_api)
|
39
|
+
level_one_taxons = expanded_links_response["expanded_links"]["level_one_taxons"]
|
51
40
|
child_taxons = expanded_links_response["expanded_links"]["child_taxons"]
|
52
41
|
parent_taxons = expanded_links_response["expanded_links"]["parent_taxons"]
|
53
42
|
taxons = expanded_links_response["expanded_links"]["taxons"]
|
54
43
|
|
55
|
-
if
|
44
|
+
if level_one_taxons
|
45
|
+
level_one_taxons.each do |taxon|
|
46
|
+
expanded = publishing_api.get_expanded_links(taxon['content_id'])
|
47
|
+
taxon['links'] = expanded['expanded_links']
|
48
|
+
linked_content_item << parse_nested_child(taxon)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
if child_taxons
|
56
53
|
child_taxons.each do |child|
|
57
54
|
linked_content_item << parse_nested_child(child)
|
58
55
|
end
|
59
56
|
end
|
60
57
|
|
61
|
-
if
|
58
|
+
if parent_taxons
|
62
59
|
# Assume no taxon has multiple parents
|
63
60
|
single_parent = parent_taxons.first
|
64
61
|
|
65
62
|
parse_nested_parent(single_parent) << linked_content_item
|
66
63
|
end
|
67
64
|
|
68
|
-
if
|
65
|
+
if taxons
|
69
66
|
taxons.each do |taxon|
|
70
67
|
taxon_node = parse_nested_parent(taxon)
|
71
68
|
linked_content_item.add_taxon(taxon_node)
|
@@ -13,30 +13,42 @@ RSpec.describe GovukTaxonomyHelpers::PublishingApiResponse do
|
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
"
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
}
|
16
|
+
let(:expanded_links) do
|
17
|
+
child = {
|
18
|
+
"content_id" => "74aadc14-9bca-40d9-abb4-4f21f9792a05",
|
19
|
+
"base_path" => "/child",
|
20
|
+
"title" => "Child",
|
21
|
+
"details" => {
|
22
|
+
"internal_name" => "C",
|
23
|
+
},
|
24
|
+
"links" => {}
|
25
|
+
}
|
27
26
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
27
|
+
{
|
28
|
+
"content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
|
29
|
+
"expanded_links" => {
|
30
|
+
"child_taxons" => [child]
|
33
31
|
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
let(:publishing_api) do
|
36
|
+
double('publishing_api')
|
37
|
+
end
|
38
|
+
|
39
|
+
before do
|
40
|
+
allow(publishing_api).to receive(:get_content).with('64aadc14-9bca-40d9-abb4-4f21f9792a05').and_return(content_item)
|
41
|
+
allow(publishing_api).to receive(:get_expanded_links).with('64aadc14-9bca-40d9-abb4-4f21f9792a05').and_return(expanded_links)
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:linked_content_item) do
|
45
|
+
GovukTaxonomyHelpers::LinkedContentItem.from_content_id(
|
46
|
+
content_id: content_item['content_id'],
|
47
|
+
publishing_api: publishing_api
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#from_content_id - simple one child case' do
|
40
52
|
it 'loads the taxon' do
|
41
53
|
expect(linked_content_item.title).to eq("Taxon")
|
42
54
|
expect(linked_content_item.children.map(&:title)).to eq(["Child"])
|
@@ -44,13 +56,6 @@ RSpec.describe GovukTaxonomyHelpers::PublishingApiResponse do
|
|
44
56
|
end
|
45
57
|
end
|
46
58
|
|
47
|
-
let(:linked_content_item) do
|
48
|
-
GovukTaxonomyHelpers::LinkedContentItem.from_publishing_api(
|
49
|
-
content_item: content_item,
|
50
|
-
expanded_links: expanded_links
|
51
|
-
)
|
52
|
-
end
|
53
|
-
|
54
59
|
context "content item with multiple levels of descendants" do
|
55
60
|
let(:expanded_links) do
|
56
61
|
grandchild_1 = {
|
@@ -372,8 +377,69 @@ RSpec.describe GovukTaxonomyHelpers::PublishingApiResponse do
|
|
372
377
|
end
|
373
378
|
end
|
374
379
|
|
380
|
+
context "homepage content item with a level_one_taxon and a child" do
|
381
|
+
it "parses each level of taxons from home page" do
|
382
|
+
root_taxon = {
|
383
|
+
"content_id" => "f3bbdec2-0e62-4520-a7fd-6ffd5d36e03a",
|
384
|
+
"base_path" => "/",
|
385
|
+
"title" => "GOV.UK homepage",
|
386
|
+
"details" => {}
|
387
|
+
}
|
388
|
+
|
389
|
+
child_for_level_one_taxon = {
|
390
|
+
"content_id" => "84aadc14-9bca-40d9-abb4-4f21f9792a05",
|
391
|
+
"base_path" => "/transport_child",
|
392
|
+
"title" => "Transport child",
|
393
|
+
"details" => {
|
394
|
+
"internal_name" => "TC 1",
|
395
|
+
},
|
396
|
+
"links" => {}
|
397
|
+
}
|
398
|
+
|
399
|
+
level_one_taxon = {
|
400
|
+
"content_id" => "a4038b29-b332-4f13-98b1-1c9709e216bc",
|
401
|
+
"base_path" => "/transport/all",
|
402
|
+
"title" => "Transport",
|
403
|
+
"details" => {
|
404
|
+
"internal_name" => "Transport",
|
405
|
+
},
|
406
|
+
"links" => {
|
407
|
+
"child_taxons" => [child_for_level_one_taxon],
|
408
|
+
"root_taxon" => [root_taxon]
|
409
|
+
}
|
410
|
+
}
|
411
|
+
|
412
|
+
expanded_links = {
|
413
|
+
"expanded_links" => {
|
414
|
+
"level_one_taxons" => [level_one_taxon],
|
415
|
+
}
|
416
|
+
}
|
417
|
+
|
418
|
+
expanded_links_2 = {
|
419
|
+
"expanded_links" => {
|
420
|
+
"child_taxons" => [child_for_level_one_taxon]
|
421
|
+
}
|
422
|
+
}
|
423
|
+
|
424
|
+
allow(publishing_api).to receive(:get_content).with('f3bbdec2-0e62-4520-a7fd-6ffd5d36e03a').and_return(root_taxon)
|
425
|
+
allow(publishing_api).to receive(:get_expanded_links).with('f3bbdec2-0e62-4520-a7fd-6ffd5d36e03a').and_return(expanded_links)
|
426
|
+
allow(publishing_api).to receive(:get_expanded_links).with('a4038b29-b332-4f13-98b1-1c9709e216bc').and_return(expanded_links_2)
|
427
|
+
|
428
|
+
homepage_taxon = GovukTaxonomyHelpers::LinkedContentItem.from_content_id(
|
429
|
+
content_id: root_taxon['content_id'],
|
430
|
+
publishing_api: publishing_api
|
431
|
+
)
|
432
|
+
|
433
|
+
expect(homepage_taxon.title).to eq("GOV.UK homepage")
|
434
|
+
expect(homepage_taxon.parent).to eq(nil)
|
435
|
+
expect(homepage_taxon.children.map(&:title)).to eq(["Transport"])
|
436
|
+
expect(homepage_taxon.descendants.map(&:title)).to eq(["Transport", "Transport child"])
|
437
|
+
expect(homepage_taxon.children.first.children.first.title).to eq("Transport child")
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
375
441
|
context "minimal responses with missing links and details hashes" do
|
376
|
-
|
442
|
+
it "parses taxons with nil internal names" do
|
377
443
|
grandchild_1 = {
|
378
444
|
"content_id" => "84aadc14-9bca-40d9-abb4-4f21f9792a05",
|
379
445
|
"base_path" => "/grandchild-1",
|
@@ -418,13 +484,14 @@ RSpec.describe GovukTaxonomyHelpers::PublishingApiResponse do
|
|
418
484
|
}
|
419
485
|
}
|
420
486
|
|
421
|
-
|
422
|
-
|
423
|
-
|
487
|
+
allow(publishing_api).to receive(:get_content).with('aaaaaa14-9bca-40d9-abb4-4f21f9792a05').and_return(content_item)
|
488
|
+
allow(publishing_api).to receive(:get_expanded_links).with('aaaaaa14-9bca-40d9-abb4-4f21f9792a05').and_return(expanded_links)
|
489
|
+
|
490
|
+
minimal_taxon = GovukTaxonomyHelpers::LinkedContentItem.from_content_id(
|
491
|
+
content_id: content_item['content_id'],
|
492
|
+
publishing_api: publishing_api
|
424
493
|
)
|
425
|
-
end
|
426
494
|
|
427
|
-
it "parses taxons with nil internal names" do
|
428
495
|
expect(minimal_taxon.title).to eq("Minimal Taxon")
|
429
496
|
expect(minimal_taxon.internal_name).to be_nil
|
430
497
|
expect(minimal_taxon.parent.title).to eq("Parent Taxon")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_taxonomy_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Government Digital Service
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|