gds-api-adapters 46.0.0 → 47.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: 161ffa498afd13048d7be1e30938b3ad067f3acb
4
- data.tar.gz: b8f8ae98f0f20ef2cf53631fef4ced003453ffdf
3
+ metadata.gz: 99be45080ed4287d5b398d1ef0040dc61a74d822
4
+ data.tar.gz: 13e88395ee04b4d40edd6f76912a946fbcd162c4
5
5
  SHA512:
6
- metadata.gz: a9e1596dd63611d1928818f2ad3106831909346915252f449f35c2ff0c8df8368eefce31d91c01567595012cfd51efe51f82cffe4dbed8c4955f6f1d5ffcda90
7
- data.tar.gz: cf34fcefdbb3e5666c0e66949cb5058f70d9b6e919d8cf94803ea696301d97a436054b2e1115c5d648f0b8e05e201388cf11cfea0cd92d8402ecb245088a1f1b
6
+ metadata.gz: 3d56fdf001acca6e91cb36e0eb9ff850c1183b6e6d774ede515aa1482769ab9a05fad70cf225265442dd247b83790c10e7d34d0e246ecb64ef543437f6663f70
7
+ data.tar.gz: 19920ce300008797ce38efcea67ce18897fbd2505216135f225c59e3cbccd78840b09f33ff89a6f80f6c6270fdcbc5933885dac7d3bb8a48d7c771a721cbb3e4
@@ -1,5 +1,4 @@
1
1
  require 'gds_api/asset_manager'
2
- require 'gds_api/content_api'
3
2
  require 'gds_api/content_store'
4
3
  require 'gds_api/imminence'
5
4
  require 'gds_api/licence_application'
@@ -13,10 +12,6 @@ module GdsApi
13
12
  @asset_manager_api ||= GdsApi::AssetManager.new(Plek.current.find('asset-manager'), options)
14
13
  end
15
14
 
16
- def content_api(options = {})
17
- @content_api ||= GdsApi::ContentApi.new(Plek.current.find("contentapi"), options)
18
- end
19
-
20
15
  def content_store(options = {})
21
16
  @content_store ||= GdsApi::ContentStore.new(Plek.current.find("content-store"), options)
22
17
  end
@@ -43,7 +38,7 @@ module GdsApi
43
38
 
44
39
  def self.included(klass)
45
40
  if klass.respond_to?(:helper_method)
46
- klass.helper_method :imminence_api, :content_api, :licence_application_api
41
+ klass.helper_method :imminence_api, :licence_application_api
47
42
  end
48
43
  end
49
44
  end
@@ -21,10 +21,7 @@ module GdsApi
21
21
  def_delegators :results, :each, :to_ary
22
22
 
23
23
  def results
24
- # support group_by results from the content api by checking if there is a
25
- # grouped_results key present first. if it's not, then fallback to the
26
- # results key
27
- to_hash["grouped_results"] || to_hash["results"]
24
+ to_hash["results"]
28
25
  end
29
26
 
30
27
  def has_next_page?
@@ -43,6 +43,89 @@ module GdsApi
43
43
  def service_feedback(transaction_page_slug)
44
44
  get_json("#{endpoint}/data/#{transaction_page_slug}/customer-satisfaction")
45
45
  end
46
+
47
+ # Fetching statistics data from the performance platform for a given page slug
48
+ #
49
+ # Makes a +GET+ request.
50
+ #
51
+ # @param slug [String] Points to the page for which we are requesting
52
+ # statistics.
53
+ # @param is_multipart [Boolean] Flag that marks whether the slug is multipart
54
+ # or not:
55
+ #
56
+ # - simple: `/european-health-insurance-card`
57
+ # - multipart: `/european-health-insurance-card/123`
58
+ #
59
+ # # @examples
60
+ #
61
+ # 1. Without multipart filtering:
62
+ #
63
+ # performance_platform_data_out.search_terms('/european-health-insurance-card')
64
+ #
65
+ # 2. With multipart filtering:
66
+ #
67
+ # performance_platform_data_out.searches('/european-health-insurance-card', true)
68
+ # performance_platform_data_out.page_views('/european-health-insurance-card', true)
69
+ # performance_platform_data_out.problem_reports('/european-health-insurance-card', true)
70
+
71
+ def search_terms(slug)
72
+ options = {
73
+ slug: slug,
74
+ transaction: 'searchTerms',
75
+ group_by: 'searchKeyword',
76
+ collect: 'searchUniques:sum'
77
+ }
78
+ statistics(options)
79
+ end
80
+
81
+ def searches(slug, is_multipart)
82
+ options = {
83
+ slug: slug,
84
+ transaction: 'searchTerms',
85
+ group_by: 'pagePath',
86
+ collect: 'searchUniques:sum'
87
+ }
88
+ statistics(options, is_multipart)
89
+ end
90
+
91
+ def page_views(slug, is_multipart)
92
+ options = {
93
+ slug: slug,
94
+ transaction: 'page-statistics',
95
+ group_by: 'pagePath',
96
+ collect: 'uniquePageviews:sum'
97
+ }
98
+ statistics(options, is_multipart)
99
+ end
100
+
101
+ def problem_reports(slug, is_multipart)
102
+ options = {
103
+ slug: slug,
104
+ transaction: 'page-contacts',
105
+ group_by: 'pagePath',
106
+ collect: 'total:sum'
107
+ }
108
+ statistics(options, is_multipart)
109
+ end
110
+
111
+ # This can be used as a free form call to the performance platform.
112
+ # The performance platform uses Backdrop and its query language for
113
+ # storing and querying data.
114
+ # Backdrop can be found here: https://github.com/alphagov/backdrop
115
+ def statistics(options, is_multipart = false)
116
+ params = {
117
+ group_by: options[:group_by],
118
+ collect: options[:collect],
119
+ duration: 42,
120
+ period: "day",
121
+ end_at: Date.today.to_time.getutc.iso8601
122
+ }
123
+
124
+ filter_param = is_multipart ? :filter_by_prefix : :filter_by
125
+ params[filter_param] = "pagePath:" + options[:slug]
126
+
127
+ get_json("#{endpoint}/data/govuk-info/#{options[:transaction]}#{query_string(params)}")
128
+ end
46
129
  end
47
130
  end
48
131
  end
@@ -17,6 +17,81 @@ module GdsApi
17
17
  def stub_service_not_available
18
18
  stub_request(:any, /#{PP_DATA_OUT_ENDPOINT}\/.*/).to_return(status: 503)
19
19
  end
20
+
21
+ def stub_search_terms(slug, response_body = {})
22
+ options = {
23
+ slug: slug,
24
+ transaction: 'searchTerms',
25
+ group_by: 'searchKeyword',
26
+ collect: 'searchUniques:sum'
27
+ }
28
+ stub_statistics(options, false, response_body)
29
+ end
30
+
31
+ def stub_searches(slug, is_multipart, response_body = {})
32
+ options = {
33
+ slug: slug,
34
+ transaction: 'searchTerms',
35
+ group_by: 'pagePath',
36
+ collect: 'searchUniques:sum'
37
+ }
38
+ stub_statistics(options, is_multipart, response_body)
39
+ end
40
+
41
+ def stub_page_views(slug, is_multipart, response_body = {})
42
+ options = {
43
+ slug: slug,
44
+ transaction: 'page-statistics',
45
+ group_by: 'pagePath',
46
+ collect: 'uniquePageviews:sum'
47
+ }
48
+ stub_statistics(options, is_multipart, response_body)
49
+ end
50
+
51
+ def stub_problem_reports(slug, is_multipart, response_body = {})
52
+ options = {
53
+ slug: slug,
54
+ transaction: 'page-contacts',
55
+ group_by: 'pagePath',
56
+ collect: 'total:sum'
57
+ }
58
+ stub_statistics(options, is_multipart, response_body)
59
+ end
60
+
61
+ def stub_statistics(options, is_multipart, response_body = {})
62
+ params = {
63
+ group_by: options[:group_by],
64
+ collect: options[:collect],
65
+ duration: 42,
66
+ period: "day",
67
+ end_at: Date.today.to_time.getutc.iso8601
68
+ }
69
+
70
+ filter_param = is_multipart ? :filter_by_prefix : :filter_by
71
+ params[filter_param] = "pagePath:" + options[:slug]
72
+
73
+ stub_http_request(:get, "#{PP_DATA_OUT_ENDPOINT}/data/govuk-info/#{options[:transaction]}")
74
+ .with(query: params)
75
+ .to_return(status: 200, body: response_body.to_json)
76
+ end
77
+
78
+ def stub_search_404(slug)
79
+ stub_request(:get, "#{PP_DATA_OUT_ENDPOINT}/data/govuk-info/searchTerms").
80
+ with(query: hash_including(filter_by: slug)).
81
+ to_return(status: 404, headers: { content_type: "application/json" })
82
+ end
83
+
84
+ def stub_page_views_404(slug)
85
+ stub_request(:get, "#{PP_DATA_OUT_ENDPOINT}/data/govuk-info/page-statistics").
86
+ with(query: hash_including(filter_by: slug)).
87
+ to_return(status: 404, headers: { content_type: "application/json" })
88
+ end
89
+
90
+ def stub_problem_reports_404(slug)
91
+ stub_request(:get, "#{PP_DATA_OUT_ENDPOINT}/data/govuk-info/page-contacts").
92
+ with(query: hash_including(filter_by: slug)).
93
+ to_return(status: 404, headers: { content_type: "application/json" })
94
+ end
20
95
  end
21
96
  end
22
97
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '46.0.0'.freeze
2
+ VERSION = '47.0.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 46.0.0
4
+ version: 47.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Stewart
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-18 00:00:00.000000000 Z
11
+ date: 2017-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: plek
@@ -359,7 +359,6 @@ files:
359
359
  - lib/gds_api/asset_manager.rb
360
360
  - lib/gds_api/base.rb
361
361
  - lib/gds_api/calendars.rb
362
- - lib/gds_api/content_api.rb
363
362
  - lib/gds_api/content_store.rb
364
363
  - lib/gds_api/email_alert_api.rb
365
364
  - lib/gds_api/exceptions.rb
@@ -392,8 +391,6 @@ files:
392
391
  - lib/gds_api/test_helpers/asset_manager.rb
393
392
  - lib/gds_api/test_helpers/calendars.rb
394
393
  - lib/gds_api/test_helpers/common_responses.rb
395
- - lib/gds_api/test_helpers/content_api.rb
396
- - lib/gds_api/test_helpers/content_api/artefact_stub.rb
397
394
  - lib/gds_api/test_helpers/content_item_helpers.rb
398
395
  - lib/gds_api/test_helpers/content_store.rb
399
396
  - lib/gds_api/test_helpers/email_alert_api.rb
@@ -1,125 +0,0 @@
1
- require_relative 'base'
2
- require_relative 'exceptions'
3
- require_relative 'list_response'
4
-
5
- class GdsApi::ContentApi < GdsApi::Base
6
- def initialize(endpoint_url, options = {})
7
- # If the `web_urls_relative_to` option is given, the adapter will convert
8
- # any `web_url` values to relative URLs if they are from the same host.
9
- #
10
- # For example: "https://www.gov.uk"
11
-
12
- @web_urls_relative_to = options.delete(:web_urls_relative_to)
13
- super
14
- end
15
-
16
- def sections
17
- tags("section")
18
- end
19
-
20
- def root_sections
21
- root_tags("section")
22
- end
23
-
24
- def sub_sections(parent_tag)
25
- child_tags("section", parent_tag)
26
- end
27
-
28
- def tags(tag_type, options = {})
29
- params = [
30
- "type=#{CGI.escape(tag_type)}"
31
- ]
32
- params << "sort=#{options[:sort]}" if options.has_key?(:sort)
33
- params << "draft=true" if options[:draft]
34
- params << "cachebust=#{Time.now.utc.to_i}#{rand(1000)}" if options[:bust_cache]
35
-
36
- get_list("#{base_url}/tags.json?#{params.join('&')}")
37
- end
38
-
39
- def root_tags(tag_type)
40
- get_list("#{base_url}/tags.json?type=#{CGI.escape(tag_type)}&root_sections=true")
41
- end
42
-
43
- def child_tags(tag_type, parent_tag, options = {})
44
- params = [
45
- "type=#{CGI.escape(tag_type)}",
46
- "parent_id=#{CGI.escape(parent_tag)}",
47
- ]
48
- params << "sort=#{options[:sort]}" if options.has_key?(:sort)
49
-
50
- get_list("#{base_url}/tags.json?#{params.join('&')}")
51
- end
52
-
53
- def tag(tag, tag_type = nil)
54
- if tag_type.nil?
55
- raise "Requests for tags without a tag_type are no longer supported. You probably want a tag_type of 'section'. See https://github.com/alphagov/govuk_content_api/blob/f4c0102a1ae4970be6a440707b89798442f768b9/govuk_content_api.rb#L241-L250"
56
- end
57
-
58
- url = [base_url, "tags", CGI.escape(tag_type), CGI.escape(tag)].join("/") + ".json"
59
- get_json(url)
60
- end
61
-
62
- def for_need(need_id)
63
- get_list("#{base_url}/for_need/#{CGI.escape(need_id.to_s)}.json")
64
- end
65
-
66
- def artefact(slug, params = {})
67
- get_json(artefact_url(slug, params))
68
- end
69
-
70
- def artefact!(slug, params = {})
71
- get_json(artefact_url(slug, params))
72
- end
73
-
74
- def artefacts
75
- get_list("#{base_url}/artefacts.json")
76
- end
77
-
78
- def local_authority(snac_code)
79
- get_json("#{base_url}/local_authorities/#{CGI.escape(snac_code)}.json")
80
- end
81
-
82
- def local_authorities_by_name(name)
83
- get_json("#{base_url}/local_authorities.json?name=#{CGI.escape(name)}")
84
- end
85
-
86
- def local_authorities_by_snac_code(snac_code)
87
- get_json("#{base_url}/local_authorities.json?snac_code=#{CGI.escape(snac_code)}")
88
- end
89
-
90
- def get_list(url)
91
- get_json(url) { |r|
92
- GdsApi::ListResponse.new(r, self, web_urls_relative_to: @web_urls_relative_to)
93
- }
94
- end
95
-
96
- def get_json(url, &create_response)
97
- create_response = create_response || Proc.new { |r|
98
- GdsApi::Response.new(r, web_urls_relative_to: @web_urls_relative_to)
99
- }
100
- super(url, &create_response)
101
- end
102
-
103
- private
104
-
105
- def base_url
106
- endpoint
107
- end
108
-
109
- def key_for_tag_type(tag_type)
110
- tag_type.nil? ? "tag" : CGI.escape(tag_type)
111
- end
112
-
113
- def artefact_url(slug, params)
114
- url = "#{base_url}/#{CGI.escape(slug)}.json"
115
- query = params.map { |k, v| "#{k}=#{v}" }
116
- if query.any?
117
- url += "?#{query.join('&')}"
118
- end
119
-
120
- if params[:edition] && ! options.include?(:bearer_token)
121
- raise GdsApi::NoBearerToken
122
- end
123
- url
124
- end
125
- end
@@ -1,358 +0,0 @@
1
- require 'gds_api/test_helpers/json_client_helper'
2
- require 'cgi'
3
- require 'gds_api/test_helpers/common_responses'
4
-
5
- module GdsApi
6
- module TestHelpers
7
- module ContentApi
8
- include GdsApi::TestHelpers::CommonResponses
9
- # Generally true. If you are initializing the client differently,
10
- # you could redefine/override the constant or stub directly.
11
- CONTENT_API_ENDPOINT = Plek.current.find('contentapi')
12
-
13
- # Legacy section test helpers
14
- #
15
- # Use of these should be retired in favour of the other test helpers in this
16
- # module which work with any tag type.
17
-
18
- def content_api_has_root_sections(slugs_or_sections)
19
- content_api_has_root_tags("section", slugs_or_sections)
20
- end
21
-
22
- def content_api_has_section(slug_or_hash, parent_slug = nil)
23
- content_api_has_tag("section", slug_or_hash, parent_slug)
24
- end
25
-
26
- def content_api_has_subsections(parent_slug_or_hash, subsection_slugs)
27
- content_api_has_child_tags("section", parent_slug_or_hash, subsection_slugs)
28
- end
29
-
30
- def artefact_for_slug_in_a_section(slug, section_slug)
31
- artefact_for_slug_with_a_tag("section", slug, section_slug)
32
- end
33
-
34
- def artefact_for_slug_in_a_subsection(slug, subsection_slug)
35
- artefact_for_slug_with_a_child_tag("section", slug, subsection_slug)
36
- end
37
-
38
- # Takes an array of slugs, or hashes with section details (including a slug).
39
- # Will stub out content_api calls for tags of type section to return these sections
40
- def content_api_has_root_tags(tag_type, slugs_or_tags)
41
- body = plural_response_base.merge(
42
- "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
43
- )
44
- urls = ["type=#{tag_type}", "root_sections=true&type=#{tag_type}"].map { |q|
45
- "#{CONTENT_API_ENDPOINT}/tags.json?#{q}"
46
- }
47
- urls.each do |url|
48
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
49
- end
50
- end
51
-
52
- def content_api_has_tag(tag_type, slug_or_hash, parent_tag_id = nil)
53
- tag = tag_hash(slug_or_hash, tag_type).merge(parent: parent_tag_id)
54
- body = tag_result(tag)
55
-
56
- urls = ["#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag_type)}/#{CGI.escape(tag[:slug])}.json"]
57
-
58
- urls.each do |url|
59
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
60
- end
61
- end
62
-
63
- def content_api_does_not_have_tag(tag_type, slug)
64
- body = {
65
- "_response_info" => {
66
- "status" => "not found"
67
- }
68
- }
69
-
70
- urls = ["#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(tag_type)}/#{CGI.escape(slug)}.json"]
71
-
72
- urls.each do |url|
73
- stub_request(:get, url).to_return(status: 404, body: body.to_json, headers: {})
74
- end
75
- end
76
-
77
- def content_api_has_draft_and_live_tags(options = {})
78
- type = options.fetch(:type)
79
- live_tags = options.fetch(:live).map { |tag| tag_result(tag, type, state: 'live') }
80
- draft_tags = options.fetch(:draft).map { |tag| tag_result(tag, type, state: 'draft') }
81
-
82
- body = plural_response_base.merge("results" => live_tags)
83
- stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
84
- .with(query: hash_including("type" => type))
85
- .to_return(status: 200, body: body.to_json, headers: {})
86
-
87
- body = plural_response_base.merge("results" => (live_tags + draft_tags))
88
- stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
89
- .with(query: hash_including("type" => type, "draft" => "true"))
90
- .to_return(status: 200, body: body.to_json, headers: {})
91
- end
92
-
93
- def content_api_does_not_have_tags(tag_type, _slugs)
94
- body = {
95
- "_response_info" => {
96
- "status" => "not found"
97
- }
98
- }
99
-
100
- stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
101
- .with(query: hash_including("type" => tag_type))
102
- .to_return(status: 404, body: body.to_json, headers: {})
103
- end
104
-
105
- def content_api_has_tags(tag_type, slugs_or_tags)
106
- body = plural_response_base.merge(
107
- "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
108
- )
109
-
110
- stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
111
- .with(query: hash_including("type" => tag_type))
112
- .to_return(status: 200, body: body.to_json, headers: {})
113
- end
114
-
115
- def content_api_has_sorted_tags(tag_type, sort_order, slugs_or_tags)
116
- body = plural_response_base.merge(
117
- "results" => slugs_or_tags.map { |tag| tag_result(tag, tag_type) }
118
- )
119
-
120
- stub_request(:get, "#{CONTENT_API_ENDPOINT}/tags.json")
121
- .with(query: hash_including("type" => tag_type, "sort" => sort_order))
122
- .to_return(status: 200, body: body.to_json, headers: {})
123
- end
124
-
125
- def content_api_has_child_tags(tag_type, parent_slug_or_hash, child_tag_ids)
126
- parent_tag = tag_hash(parent_slug_or_hash, tag_type)
127
- child_tags = child_tag_ids.map { |id|
128
- tag_hash(id, tag_type).merge(parent: parent_tag)
129
- }
130
- body = plural_response_base.merge(
131
- "results" => child_tags.map { |s| tag_result(s, tag_type) }
132
- )
133
- url = "#{CONTENT_API_ENDPOINT}/tags.json?type=#{tag_type}&parent_id=#{CGI.escape(parent_tag[:slug])}"
134
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
135
- end
136
-
137
- def content_api_has_sorted_child_tags(tag_type, parent_slug_or_hash, sort_order, child_tag_ids)
138
- parent_tag = tag_hash(parent_slug_or_hash, tag_type)
139
- child_tags = child_tag_ids.map { |id|
140
- tag_hash(id, tag_type).merge(parent: parent_tag)
141
- }
142
- body = plural_response_base.merge(
143
- "results" => child_tags.map { |s| tag_result(s, tag_type) }
144
- )
145
-
146
- url = "#{CONTENT_API_ENDPOINT}/tags.json?parent_id=#{CGI.escape(parent_tag[:slug])}&sort=#{sort_order}&type=#{tag_type}"
147
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
148
- end
149
-
150
- def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
151
- ArtefactStub.new(slug).with_response_body(body).stub
152
- end
153
-
154
- def content_api_has_unpublished_artefact(slug, edition, body = artefact_for_slug(slug))
155
- ArtefactStub.new(slug)
156
- .with_response_body(body)
157
- .with_query_parameters(edition: edition)
158
- .stub
159
- end
160
-
161
- def content_api_has_an_artefact_with_snac_code(slug, snac, body = artefact_for_slug(slug))
162
- ArtefactStub.new(slug)
163
- .with_response_body(body)
164
- .with_query_parameters(snac: snac)
165
- .stub
166
- end
167
-
168
- def content_api_does_not_have_an_artefact(slug)
169
- body = {
170
- "_response_info" => {
171
- "status" => "not found"
172
- }
173
- }
174
- ArtefactStub.new(slug)
175
- .with_response_body(body)
176
- .with_response_status(404)
177
- .stub
178
- end
179
-
180
- def content_api_has_an_archived_artefact(slug)
181
- body = {
182
- "_response_info" => {
183
- "status" => "gone",
184
- "status_message" => "This item is no longer available"
185
- }
186
- }
187
- ArtefactStub.new(slug)
188
- .with_response_body(body)
189
- .with_response_status(410)
190
- .stub
191
- end
192
-
193
- # Stub requests, and then dynamically generate a response based on the slug in the request
194
- def stub_content_api_default_artefact
195
- stub_request(:get, %r{\A#{CONTENT_API_ENDPOINT}/[a-z0-9-]+\.json}).to_return { |request|
196
- slug = request.uri.path.split('/').last.chomp('.json')
197
- { body: artefact_for_slug(slug).to_json }
198
- }
199
- end
200
-
201
- def artefact_for_slug(slug, options = {})
202
- singular_response_base.merge(
203
- "title" => titleize_slug(slug),
204
- "format" => options.fetch(:format, "guide"),
205
- "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape(slug)}.json",
206
- "web_url" => "http://frontend.test.gov.uk/#{slug}",
207
- "details" => {
208
- "need_ids" => ["100001"],
209
- "business_proposition" => false, # To be removed and replaced with proposition tags
210
- "format" => options.fetch(:format, "guide"),
211
- "alternative_title" => "",
212
- "overview" => "This is an overview",
213
- "video_summary" => "",
214
- "video_url" => "",
215
- "parts" => [
216
- {
217
- "id" => "overview",
218
- "order" => 1,
219
- "title" => "Overview",
220
- "body" => "<p>Some content</p>"
221
- },
222
- {
223
- "id" => "#{slug}-part-2",
224
- "order" => 2,
225
- "title" => "How to make a nomination",
226
- "body" => "<p>Some more content</p>"
227
- }
228
- ]
229
- },
230
- "tags" => [],
231
- "related" => []
232
- )
233
- end
234
-
235
- def artefact_for_slug_with_a_tag(tag_type, slug, tag_id)
236
- artefact = artefact_for_slug(slug)
237
- artefact["tags"] << tag_for_slug(tag_id, tag_type)
238
- artefact
239
- end
240
-
241
- def artefact_for_slug_with_a_child_tag(tag_type, slug, child_tag_id)
242
- artefact_for_slug_with_a_child_tags(tag_type, slug, [child_tag_id])
243
- end
244
-
245
- def artefact_for_slug_with_a_child_tags(tag_type, slug, child_tag_ids)
246
- artefact = artefact_for_slug(slug)
247
-
248
- child_tag_ids.each do |child_tag_id|
249
- # for each "part" of the path, we want to reduce across the
250
- # list and build up a tree of nested tags.
251
- # This will turn "thing1/thing2" into:
252
- # Tag{ thing2, parent: Tag{ thing1 } }
253
-
254
- tag_tree = nil
255
- child_tag_id.split('/').inject(nil) do |parent_tag, child_tag|
256
- child_tag = [parent_tag, child_tag].join('/') if parent_tag
257
- next_level_tag = tag_for_slug(child_tag, tag_type)
258
- tag_tree = if tag_tree
259
- # Because tags are nested within one another, this makes
260
- # the current part the top, and the rest we've seen the
261
- # ancestors
262
- next_level_tag.merge("parent" => tag_tree)
263
- else
264
- next_level_tag
265
- end
266
-
267
- # This becomes the parent tag in the next iteration of the block
268
- child_tag
269
- end
270
- artefact["tags"] << tag_tree
271
- end
272
-
273
- artefact
274
- end
275
-
276
- def artefact_for_slug_with_related_artefacts(slug, related_artefact_slugs)
277
- artefact = artefact_for_slug(slug)
278
- artefact["related"] = related_artefact_slugs.map do |related_slug|
279
- {
280
- "title" => titleize_slug(related_slug),
281
- "id" => "#{CONTENT_API_ENDPOINT}/#{CGI.escape(related_slug)}.json",
282
- "web_url" => "https://www.test.gov.uk/#{related_slug}",
283
- "details" => {}
284
- }
285
- end
286
- artefact
287
- end
288
-
289
- def tag_for_slug(slug, tag_type, parent_slug = nil)
290
- if parent_slug
291
- parent = tag_for_slug(parent_slug, tag_type)
292
- end
293
-
294
- tag_result(slug: slug, type: tag_type, parent: parent)
295
- end
296
-
297
- # Construct a tag hash suitable for passing into tag_result
298
- def tag_hash(slug_or_hash, tag_type = "section")
299
- if slug_or_hash.is_a?(Hash)
300
- slug_or_hash
301
- else
302
- { slug: slug_or_hash, type: tag_type }
303
- end
304
- end
305
-
306
- def tag_result(slug_or_hash, tag_type = nil, options = {})
307
- tag = tag_hash(slug_or_hash, tag_type)
308
-
309
- parent = tag_result(tag[:parent]) if tag[:parent]
310
- pluralized_tag_type = simple_tag_type_pluralizer(tag[:type])
311
-
312
- {
313
- "id" => "#{CONTENT_API_ENDPOINT}/tags/#{CGI.escape(pluralized_tag_type)}/#{CGI.escape(tag[:slug])}.json",
314
- "slug" => tag[:slug],
315
- "web_url" => "http://www.test.gov.uk/browse/#{tag[:slug]}",
316
- "title" => tag[:title] || titleize_slug(tag[:slug].split("/").last),
317
- "details" => {
318
- "type" => tag[:type],
319
- "description" => tag[:description] || "#{tag[:slug]} description",
320
- "short_description" => tag[:short_description] || "#{tag[:slug]} short description"
321
- },
322
- "parent" => parent,
323
- "content_with_tag" => {
324
- "id" => "#{CONTENT_API_ENDPOINT}/with_tag.json?tag=#{CGI.escape(tag[:slug])}",
325
- "web_url" => "http://www.test.gov.uk/browse/#{tag[:slug]}"
326
- },
327
- "state" => options[:state]
328
- }
329
- end
330
-
331
- # This is a nasty hack to get around the pluralized slugs in tag paths
332
- # without having to require ActiveSupport
333
- #
334
- def simple_tag_type_pluralizer(s)
335
- case s
336
- when /o\Z/ then s.sub(/o\Z/, "es")
337
- when /y\Z/ then s.sub(/y\Z/, "ies")
338
- when /ss\Z/ then s.sub(/ss\Z/, "sses")
339
- else
340
- "#{s}s"
341
- end
342
- end
343
-
344
- def content_api_has_artefacts_for_need_id(need_id, artefacts)
345
- url = "#{CONTENT_API_ENDPOINT}/for_need/#{CGI.escape(need_id.to_s)}.json"
346
- body = plural_response_base.merge(
347
- 'results' => artefacts
348
- )
349
-
350
- stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: [])
351
- end
352
- end
353
- end
354
- end
355
-
356
- # This has to be after the definition of TestHelpers::ContentApi, otherwise, this doesn't pick up
357
- # the include of TestHelpers::CommonResponses
358
- require_relative 'content_api/artefact_stub'
@@ -1,58 +0,0 @@
1
- require 'webmock'
2
-
3
- module GdsApi
4
- module TestHelpers
5
- module ContentApi
6
- class ArtefactStub
7
- include WebMock::API
8
- # This is ugly, but the nicest way we found to get access to artefact_for_slug
9
- include GdsApi::TestHelpers::ContentApi
10
-
11
- attr_accessor :slug, :query_parameters, :response_body, :response_status
12
-
13
- def initialize(slug)
14
- @slug = slug
15
- @query_parameters = {}
16
- @response_body = artefact_for_slug(slug)
17
- @response_status = 200
18
- end
19
-
20
- def with_query_parameters(hash)
21
- @query_parameters = hash
22
- self
23
- end
24
-
25
- def with_response_body(response_body)
26
- @response_body = response_body
27
- self
28
- end
29
-
30
- def with_response_status(response_status)
31
- @response_status = response_status
32
- self
33
- end
34
-
35
- # Nothing is stubbed until this is called
36
- def stub
37
- stub_request(:get, url_without_query)
38
- .with(query: hash_including(comparable_query_params))
39
- .to_return(status: @response_status, body: @response_body.to_json)
40
- end
41
-
42
- private
43
-
44
- def url_without_query
45
- "#{CONTENT_API_ENDPOINT}/#{CGI.escape(slug)}.json"
46
- end
47
-
48
- # Ensure that all keys and values are strings
49
- # because Webmock doesn't handle symbols
50
- def comparable_query_params
51
- @query_parameters.each_with_object({}) do |(k, v), hash|
52
- hash[k.to_s] = v.nil? ? v : v.to_s
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end