gds-api-adapters 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,10 @@ class GdsApi::ContentApi < GdsApi::Base
12
12
  get_json!("#{base_url}/with_tag.json?tag=#{tag}&include_children=1")
13
13
  end
14
14
 
15
+ def artefact(slug)
16
+ get_json!("#{base_url}/#{slug}.json")
17
+ end
18
+
15
19
  private
16
20
  def base_url
17
21
  endpoint
@@ -4,6 +4,10 @@ require 'gds_api/panopticon'
4
4
 
5
5
  module GdsApi
6
6
  module Helpers
7
+ def content_api
8
+ @content_api ||= GdsApi::ContentApi.new(Plek.current.environment)
9
+ end
10
+
7
11
  def publisher_api
8
12
  @api ||= GdsApi::Publisher.new(Plek.current.environment)
9
13
  end
@@ -26,7 +30,7 @@ module GdsApi
26
30
 
27
31
  def self.included(klass)
28
32
  if klass.respond_to?(:helper_method)
29
- klass.helper_method :publisher_api, :panopticon_api, :imminence_api
33
+ klass.helper_method :publisher_api, :panopticon_api, :imminence_api, :content_api
30
34
  end
31
35
  end
32
36
  end
@@ -86,8 +86,8 @@ module GdsApi
86
86
  Response.new(response)
87
87
  else
88
88
  body = begin
89
- JSON.parse(response.body)
90
- rescue
89
+ JSON.parse(response.body.to_s)
90
+ rescue JSON::ParserError
91
91
  response.body
92
92
  end
93
93
  loggable.merge!(status: response.code, end_time: Time.now.to_f, body: body)
@@ -1,4 +1,5 @@
1
1
  require 'gds_api/test_helpers/json_client_helper'
2
+ require 'cgi'
2
3
 
3
4
  module GdsApi
4
5
  module TestHelpers
@@ -11,7 +12,7 @@ module GdsApi
11
12
  {
12
13
  "id" => "http://contentapi.test.gov.uk/tags/#{CGI.escape(slug)}.json",
13
14
  "web_url" => nil,
14
- "title" => slug.gsub("-", " ").capitalize,
15
+ "title" => titleize_slug(slug),
15
16
  "details" => {
16
17
  "type" => "section",
17
18
  "description" => "#{slug} description"
@@ -28,7 +29,93 @@ module GdsApi
28
29
  stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
29
30
  end
30
31
 
32
+ def content_api_has_an_artefact(slug, body = artefact_for_slug(slug))
33
+ url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
34
+ stub_request(:get, url).to_return(status: 200, body: body.to_json, headers: {})
35
+ end
36
+
37
+ def content_api_does_not_have_an_artefact(slug)
38
+ body = {
39
+ "_response_info" => {
40
+ "status" => "not found"
41
+ }
42
+ }
43
+ url = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
44
+ stub_request(:get, url).to_return(status: 404, body: body.to_json, headers: {})
45
+ end
46
+
47
+ def artefact_for_slug(slug)
48
+ singular_response_base.merge(
49
+ "title" => titleize_slug(slug),
50
+ "id" => "http://contentapi.test.gov.uk/#{slug}.json",
51
+ "web_url" => "http://frontend.test.gov.uk/#{slug}",
52
+ "details" => {
53
+ "need_id" => "1234",
54
+ "business_proposition" => false, # To be removed and replaced with proposition tags
55
+ "format" => "Guide",
56
+ "alternative_title" => "",
57
+ "overview" => "This is an overview",
58
+ "video_summary" => "",
59
+ "video_url" => "",
60
+ "parts" => [
61
+ {
62
+ "id" => "overview",
63
+ "order" => 1,
64
+ "title" => "Overview",
65
+ "body" => "<p>Some content</p>"
66
+ },
67
+ {
68
+ "id" => "#{slug}-part-2",
69
+ "order" => 2,
70
+ "title" => "How to make a nomination",
71
+ "body" => "<p>Some more content</p>"
72
+ }
73
+ ]
74
+ },
75
+ "tags" => [],
76
+ "related" => []
77
+ )
78
+ end
79
+
80
+ def artefact_for_slug_in_a_section(slug, section_slug)
81
+ artefact = artefact_for_slug(slug)
82
+ artefact["tags"] << full_tag_for_slug(section_slug, "section")
83
+ artefact
84
+ end
85
+
86
+ def artefact_for_slug_with_related_artefacts(slug, related_artefact_slugs)
87
+ artefact = artefact_for_slug(slug)
88
+ artefact["related"] = related_artefact_slugs.map do |related_slug|
89
+ {
90
+ "title" => titleize_slug(related_slug),
91
+ "id" => "https://contentapi.test.alphagov.co.uk/#{CGI.escape(related_slug)}.json",
92
+ "web_url" => "https://www.test.gov.uk/#{related_slug}",
93
+ "details" => {}
94
+ }
95
+ end
96
+ artefact
97
+ end
98
+
99
+ def tag_for_slug(slug, tag_type)
100
+ {
101
+ "title" => titleize_slug(slug),
102
+ "id" => "https://contentapi.test.alphagov.co.uk/tags/#{CGI.escape(slug)}.json",
103
+ "details" => {
104
+ "type" => tag_type
105
+ },
106
+ "content_with_tag" => {
107
+ "id" => "https://contentapi.test.alphagov.co.uk/with_tag.json?tag=#{CGI.escape(slug)}",
108
+ "web_url" => "https://www.test.gov.uk/browse/#{slug}",
109
+ }
110
+ }
111
+ end
112
+
31
113
  private
114
+
115
+ def titleize_slug(slug)
116
+ slug.gsub("-", " ").capitalize
117
+ end
118
+
32
119
  def response_base
33
120
  {
34
121
  "_response_info" => {
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '1.8.0'
2
+ VERSION = '1.9.0'
3
3
  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: 1.8.0
5
+ version: 1.9.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-09-10 00:00:00 Z
13
+ date: 2012-09-12 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: plek
@@ -202,7 +202,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
202
202
  requirements:
203
203
  - - ">="
204
204
  - !ruby/object:Gem::Version
205
- hash: 2196406087180137809
205
+ hash: 1130325677672649430
206
206
  segments:
207
207
  - 0
208
208
  version: "0"
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
211
211
  requirements:
212
212
  - - ">="
213
213
  - !ruby/object:Gem::Version
214
- hash: 2196406087180137809
214
+ hash: 1130325677672649430
215
215
  segments:
216
216
  - 0
217
217
  version: "0"