govuk_taxonomy_helpers 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 145e8e7f48e099c7854d619770ef6d05183c44ed
4
- data.tar.gz: 71505d56dc561f034407a03eea998e8d2f7b375b
3
+ metadata.gz: f5914eebd6bd1d3dea7a0d464950ad850baf9c6e
4
+ data.tar.gz: 222098a1b3402e73f9d2cb6d4ab96310812fdaa4
5
5
  SHA512:
6
- metadata.gz: d8200cd1c85991d6f978183c593ff2812c5c3db909c29228993047eb7235759e0ee4d182779fe0d8040794ec9fab6289d3f225b562fb0590d9781d85b88515f6
7
- data.tar.gz: ecbd204b86e4a97f47d5580c98a17b721ca7a262d37e2521153a59bb26c502086157c0f8e44ecbfa3740da9a9cc69434fc13056ac7ee3e1a6b83e771392b6500
6
+ metadata.gz: 253c283e3e433a2f8d6d154ef2535d04989737c2f0ec7b40a2448503838a61952efd9e8c0dacac864c8d1fbd0004acbb94ed10ef6f9e0921f0c0bfbc6624897f
7
+ data.tar.gz: 6d53adbc9c998dc3fa7333b689bc3c218f7b53e0eaba71df8787be7f01e53bfd56113fcf8f91f04d759e79ab9aea82ef12330012a3647f16d55a5b47eee8ebbc
@@ -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
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env groovy
2
2
 
3
+ library("govuk")
4
+
3
5
  REPOSITORY = 'govuk_taxonomy_helpers'
4
6
 
5
7
  node {
6
- def govuk = load '/var/lib/jenkins/groovy_scripts/govuk_jenkinslib.groovy'
7
8
 
8
9
  try {
9
10
  stage('Checkout') {
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 request the content from the [publishing api](https://github.com/alphagov/publishing-api), then parse it to get a `LinkedContentItem` object.
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
- content_item = Services.publishing_api.get_content("c75c541-403f-4cb1-9b34-4ddde816a80d")
31
- expanded_links = Services.publishing_api.get_expanded_links("c75c541-403f-4cb1-9b34-4ddde816a80d")
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
- GovukTaxonomyHelpers::LinkedContentItem.from_publishing_api(
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
- def initialize(content_item:, expanded_links:)
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 !child_taxons.nil?
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 !parent_taxons.nil?
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 !taxons.nil?
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)
@@ -1,3 +1,3 @@
1
1
  module GovukTaxonomyHelpers
2
- VERSION = "0.1.1".freeze
2
+ VERSION = "1.0.0".freeze
3
3
  end
@@ -13,30 +13,42 @@ RSpec.describe GovukTaxonomyHelpers::PublishingApiResponse do
13
13
  }
14
14
  end
15
15
 
16
- describe '#from_content_id - simple one child case' do
17
- let(:expanded_links) do
18
- child = {
19
- "content_id" => "74aadc14-9bca-40d9-abb4-4f21f9792a05",
20
- "base_path" => "/child",
21
- "title" => "Child",
22
- "details" => {
23
- "internal_name" => "C",
24
- },
25
- "links" => {}
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
- "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
30
- "expanded_links" => {
31
- "child_taxons" => [child]
32
- }
27
+ {
28
+ "content_id" => "64aadc14-9bca-40d9-abb4-4f21f9792a05",
29
+ "expanded_links" => {
30
+ "child_taxons" => [child]
33
31
  }
34
- end
35
- before :each do
36
- @publishing_api = double('publishing_api')
37
- allow(@publishing_api).to receive(:get_content).with('64aadc14-9bca-40d9-abb4-4f21f9792a05').and_return(content_item)
38
- allow(@publishing_api).to receive(:get_expanded_links).with('64aadc14-9bca-40d9-abb4-4f21f9792a05').and_return(expanded_links)
39
- end
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
- let(:minimal_taxon) do
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
- GovukTaxonomyHelpers::LinkedContentItem.from_publishing_api(
422
- content_item: content_item,
423
- expanded_links: expanded_links
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.1.1
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: 2017-07-31 00:00:00.000000000 Z
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler