gds-api-adapters 21.0.0 → 22.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: 6bcaacb3f37eb313895187dd726c2dce6e09170c
4
- data.tar.gz: 02f222992af858d36f5ef0f1787e379e77ca88f5
3
+ metadata.gz: 30afe3107beb650860db70e91c9d42835c570de2
4
+ data.tar.gz: d3b8c1636e24c02501718a1f6f1c319cd43c818c
5
5
  SHA512:
6
- metadata.gz: 1b6ac58a27bc20babe9dc54e00f64298ee2b86ad617955e989a511666ff968317848617ea9ae02ab732fe0b8f5288173e996228f6d31ce64829c7348fd387233
7
- data.tar.gz: 2a5109ede1a0b00e64f11db81e61c94210783c57090c0692422837160f5318820d81c7a15e55345ccbf5e39890846bf101cc548833df472d6a55b31135408129
6
+ metadata.gz: 085750359ca93887f245ffe02241413cfd2d5532c4eee64265fc8605ddc04b50df7994b9f3a869b43b36eb1d1359f1da1c308deef366f1284db75fcc95970ee5
7
+ data.tar.gz: 7b34634b10f4ba9c74c3bbe271d411681d831af8fa2405f78036f7856ade1c893af9ab8b7fdab2587f3b9388630fcc8df24dd6107021df67a486b027d70c3007
@@ -3,12 +3,20 @@ require_relative 'exceptions'
3
3
 
4
4
  class GdsApi::ContentStore < GdsApi::Base
5
5
 
6
+ class ItemNotFound < GdsApi::HTTPNotFound
7
+ def self.build_from(http_error)
8
+ new(http_error.code, http_error.message, http_error.error_details)
9
+ end
10
+ end
11
+
6
12
  def content_item(base_path)
7
13
  get_json(content_item_url(base_path))
8
14
  end
9
15
 
10
16
  def content_item!(base_path)
11
17
  get_json!(content_item_url(base_path))
18
+ rescue GdsApi::HTTPNotFound => e
19
+ raise ItemNotFound.build_from(e)
12
20
  end
13
21
 
14
22
  private
@@ -67,10 +67,6 @@ module GdsApi
67
67
  @worldwide_api ||= GdsApi::Worldwide.new(Plek.current.find("whitehall-admin"), options)
68
68
  end
69
69
 
70
- def finder_api(options = {})
71
- @finder_api ||= FinderApi.new(Plek.current.find("finder-api"), options)
72
- end
73
-
74
70
  def email_alert_api(options = {})
75
71
  @email_alert_api ||= EmailAlertApi.new(Plek.current.find("email-alert-api"), options)
76
72
  end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '21.0.0'
2
+ VERSION = '22.0.0'
3
3
  end
@@ -10,12 +10,35 @@ describe GdsApi::ContentStore do
10
10
  @api = GdsApi::ContentStore.new(@base_api_url)
11
11
  end
12
12
 
13
- describe "item" do
13
+ describe "content_item" do
14
14
  it "should return the item" do
15
15
  base_path = "/test-from-content-store"
16
16
  content_store_has_item(base_path)
17
17
  response = @api.content_item(base_path)
18
18
  assert_equal base_path, response["base_path"]
19
19
  end
20
+
21
+ it "should return nil if the item doesn't exist" do
22
+ content_store_does_not_have_item("/non-existent")
23
+ assert_nil @api.content_item("/non-existent")
24
+ end
25
+ end
26
+
27
+ describe "content_item!" do
28
+ it "should return the item" do
29
+ base_path = "/test-from-content-store"
30
+ content_store_has_item(base_path)
31
+ response = @api.content_item!(base_path)
32
+ assert_equal base_path, response["base_path"]
33
+ end
34
+
35
+ it "should raise if the item doesn't exist" do
36
+ content_store_does_not_have_item("/non-existent")
37
+ e = assert_raises GdsApi::ContentStore::ItemNotFound do
38
+ @api.content_item!("/non-existent")
39
+ end
40
+ assert_equal 404, e.code
41
+ assert_equal "url: #{@base_api_url}/content/non-existent", e.message.strip
42
+ end
20
43
  end
21
44
  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: 21.0.0
4
+ version: 22.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: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: plek
@@ -283,8 +283,6 @@ files:
283
283
  - lib/gds_api/exceptions.rb
284
284
  - lib/gds_api/external_link_tracker.rb
285
285
  - lib/gds_api/fact_cave.rb
286
- - lib/gds_api/finder_api.rb
287
- - lib/gds_api/finder_schema.rb
288
286
  - lib/gds_api/gov_uk_delivery.rb
289
287
  - lib/gds_api/govuk_headers.rb
290
288
  - lib/gds_api/helpers.rb
@@ -322,7 +320,6 @@ files:
322
320
  - lib/gds_api/test_helpers/content_store.rb
323
321
  - lib/gds_api/test_helpers/email_alert_api.rb
324
322
  - lib/gds_api/test_helpers/fact_cave.rb
325
- - lib/gds_api/test_helpers/finder_api.rb
326
323
  - lib/gds_api/test_helpers/gov_uk_delivery.rb
327
324
  - lib/gds_api/test_helpers/imminence.rb
328
325
  - lib/gds_api/test_helpers/intent_helpers.rb
@@ -352,8 +349,6 @@ files:
352
349
  - test/email_alert_api_test.rb
353
350
  - test/external_link_tracker_test.rb
354
351
  - test/fact_cave_test.rb
355
- - test/finder_api_test.rb
356
- - test/finder_schema_test.rb
357
352
  - test/fixtures/finder_api/cma-case-schema.json
358
353
  - test/fixtures/hello.txt
359
354
  - test/fixtures/new_policies_for_dwp.json
@@ -417,7 +412,6 @@ test_files:
417
412
  - test/publishing_api_test.rb
418
413
  - test/whitehall_admin_api_test.rb
419
414
  - test/pp_data_in_test.rb
420
- - test/finder_schema_test.rb
421
415
  - test/need_api_test.rb
422
416
  - test/publisher_api_test.rb
423
417
  - test/rummager_helpers_test.rb
@@ -425,7 +419,6 @@ test_files:
425
419
  - test/content_register_test.rb
426
420
  - test/fact_cave_test.rb
427
421
  - test/test_helpers/panopticon_test.rb
428
- - test/finder_api_test.rb
429
422
  - test/licence_application_api_test.rb
430
423
  - test/gov_uk_delivery_test.rb
431
424
  - test/maslow_test.rb
@@ -1,30 +0,0 @@
1
- require_relative 'base'
2
- require_relative 'finder_schema'
3
-
4
- module GdsApi
5
- class FinderApi < Base
6
- def initialize(url, options = {})
7
- @schema_factory = options.fetch(:schema_factory) { FinderSchema.method(:new) }
8
- super
9
- end
10
-
11
- def get_documents(finder_slug, options = {})
12
- get_json(documents_url(finder_slug, options))
13
- end
14
-
15
- def get_schema(finder_slug, options = {})
16
- @schema_factory.call(
17
- get_json(finder_url(finder_slug, 'schema', options)).to_hash
18
- )
19
- end
20
-
21
- private
22
- def documents_url(finder_slug, options = {})
23
- finder_url(finder_slug, 'documents', options)
24
- end
25
-
26
- def finder_url(finder_slug, action, options = {})
27
- "#{endpoint}/finders/#{CGI.escape(finder_slug)}/#{action}.json#{query_string(options)}"
28
- end
29
- end
30
- end
@@ -1,70 +0,0 @@
1
- class GdsApi::FinderSchema
2
- NotFoundError = Class.new(RuntimeError)
3
-
4
- def initialize(schema_hash)
5
- @schema_hash = schema_hash
6
- end
7
-
8
- def user_friendly_values(document_attributes)
9
- document_attributes.each_with_object({}) do |(k, v), values|
10
- values.store(
11
- k.to_s,
12
- {
13
- label: user_friendly_facet_label(k.to_s),
14
- values: user_friendly_facet_value(k.to_s, v),
15
- }
16
- )
17
- end
18
- end
19
-
20
- attr_reader :schema_hash
21
- private :schema_hash
22
-
23
- private
24
-
25
- def slug
26
- schema_hash.fetch("slug")
27
- end
28
-
29
- def user_friendly_facet_label(facet_key)
30
- find_facet(facet_key.to_s).fetch("name")
31
- end
32
-
33
- def user_friendly_facet_value(facet_key, value)
34
- Array(value).map { |value|
35
- {
36
- label: find_schema_allowed_value_label(facet_key, value),
37
- slug: value,
38
- }
39
- }
40
- end
41
-
42
- def find_schema_allowed_value_label(facet_key, value)
43
- value_label_pair = allowed_values_for(facet_key)
44
- .find { |schema_value|
45
- schema_value.fetch("value") == value
46
- }
47
-
48
- if value_label_pair.nil?
49
- raise_value_not_found_error(facet_key, value)
50
- else
51
- value_label_pair.fetch("label")
52
- end
53
- end
54
-
55
- def allowed_values_for(facet_key)
56
- find_facet(facet_key).fetch("allowed_values")
57
- end
58
-
59
- def find_facet(facet_key)
60
- facets.find { |facet| facet.fetch("key") == facet_key }
61
- end
62
-
63
- def facets
64
- schema_hash.fetch("facets")
65
- end
66
-
67
- def raise_value_not_found_error(facet_key, value)
68
- raise NotFoundError.new("#{facet_key} value '#{value}' not found in #{slug} schema")
69
- end
70
- end
@@ -1,24 +0,0 @@
1
- require 'gds_api/test_helpers/json_client_helper'
2
-
3
- module GdsApi
4
- module TestHelpers
5
- module FinderApi
6
- FINDER_API_ENDPOINT = Plek.current.find('finder-api')
7
-
8
- def finder_api_has_schema(finder_slug, schema_fixture = FinderApi.schema_fixture)
9
- stub_request(:get, "#{FINDER_API_ENDPOINT}/finders/#{finder_slug}/schema.json")
10
- .with(:headers => {'Content-Type'=>'application/json'})
11
- .to_return(:status => 200, :body => schema_fixture)
12
- end
13
-
14
- def self.schema_fixture
15
- File.read(
16
- File.expand_path(
17
- "../../../../test/fixtures/finder_api/cma-case-schema.json",
18
- __FILE__
19
- )
20
- )
21
- end
22
- end
23
- end
24
- end
@@ -1,123 +0,0 @@
1
- require 'test_helper'
2
- require 'gds_api/finder_api'
3
-
4
- describe GdsApi::FinderApi do
5
- let(:base_api_url) { "http://finder-api" }
6
- let(:api) {
7
- GdsApi::FinderApi.new(
8
- base_api_url,
9
- schema_factory: schema_factory,
10
- )
11
- }
12
-
13
- let(:schema) { Object.new }
14
- let(:schema_factory) {
15
- ->(schema_as_a_hash) { schema }
16
- }
17
-
18
- describe "get_documents" do
19
- it "should return all documents" do
20
- documents_hash = {
21
- 'documents' => [
22
- {
23
- 'title' => 'A document',
24
- 'date' => '2014-01-24 00:00:00 +0000',
25
- 'case_type' => 'market-investigations'
26
- },
27
- {
28
- 'title' => 'Blah blah',
29
- 'date' => '2014-01-25 00:00:00 +0000',
30
- 'case_type' => 'merger-inquiries'
31
- }
32
- ]
33
- }
34
-
35
- req = WebMock.stub_request(:get, "#{base_api_url}/finders/some-finder-slug/documents.json").
36
- to_return(:body => documents_hash.to_json,
37
- :headers => {"Content-type" => "application/json"})
38
-
39
- response = api.get_documents("some-finder-slug")
40
- assert_equal 200, response.code
41
- assert_equal documents_hash, response.to_hash
42
-
43
- assert_requested(req)
44
- end
45
-
46
- it "should forward query parameters" do
47
- documents_hash = {
48
- 'documents' => [
49
- {
50
- 'title' => 'A document',
51
- 'date' => '2014-01-24 00:00:00 +0000',
52
- 'case_type' => 'market-investigations'
53
- }
54
- ]
55
- }
56
-
57
- req = WebMock.stub_request(:get, "#{base_api_url}/finders/some-finder-slug/documents.json").
58
- with(query: {case_type: 'market-investigations'}).
59
- to_return(:body => documents_hash.to_json,
60
- :headers => {"Content-type" => "application/json"})
61
-
62
- response = api.get_documents("some-finder-slug", case_type: 'market-investigations')
63
- assert_equal 200, response.code
64
- assert_equal documents_hash, response.to_hash
65
-
66
- assert_requested(req)
67
- end
68
- end
69
-
70
- describe "get_schema" do
71
- let(:schema_factory) {
72
- Minitest::Mock.new
73
- .expect(:call, schema, [schema_hash])
74
- }
75
-
76
- let(:schema_hash) {
77
- {'it is' => 'a schema'}
78
- }
79
-
80
- let(:schema_json) {
81
- schema_hash.to_json
82
- }
83
-
84
- let(:schema_url) {
85
- "#{base_api_url}/finders/cma-cases/schema.json"
86
- }
87
-
88
-
89
- before do
90
- @req = WebMock.stub_request(:get, schema_url).
91
- to_return(:body => schema_json,
92
- :headers => {"Content-type" => "application/json"})
93
- end
94
-
95
- it "requests the finder's schema" do
96
- api.get_schema("cma-cases")
97
-
98
- assert_requested(@req)
99
- end
100
-
101
- it "constructs and returns a schema object" do
102
- returned_schema = api.get_schema("cma-cases")
103
-
104
- assert_equal schema, returned_schema
105
- schema_factory.verify
106
- end
107
-
108
- it "should forward query parameters" do
109
- req = WebMock.stub_request(:get, "#{base_api_url}/finders/some-finder-slug/schema.json").
110
- with(query: {locale: 'fr-FR'}).
111
- to_return(:body => schema_json,
112
- :headers => {"Content-type" => "application/json"})
113
-
114
- response = api.get_schema("some-finder-slug", locale: 'fr-FR')
115
-
116
- assert_requested(req)
117
- end
118
-
119
- it "defaults the schema factory if not provided" do
120
- GdsApi::FinderApi.new(base_api_url).get_schema("cma-cases")
121
- end
122
- end
123
- end
@@ -1,119 +0,0 @@
1
- require "test_helper"
2
- require "gds_api/finder_schema"
3
-
4
- describe GdsApi::FinderSchema do
5
-
6
- let(:schema) { GdsApi::FinderSchema.new(schema_hash) }
7
-
8
- let(:schema_hash) {
9
- {
10
- "slug" => "cma-cases",
11
- "name" => "Competition and Markets Authority cases",
12
- "document_noun" => "case",
13
- "facets" => [
14
- {
15
- "key" => "case_type",
16
- "name" => "Case type",
17
- "type" => "single-select",
18
- "include_blank" => "All case types",
19
- "allowed_values" => [
20
- {
21
- "label" => "CA98 and civil cartels",
22
- "value" => "ca98-and-civil-cartels",
23
- },
24
- ],
25
- },
26
- {
27
- "key" => "market_sector",
28
- "name" => "Market sector",
29
- "type" => "single-select",
30
- "include_blank" => false,
31
- "allowed_values" => [
32
- {
33
- "label" => "Aerospace",
34
- "value" => "aerospace",
35
- },
36
- ],
37
- },
38
- ],
39
- }
40
- }
41
-
42
- describe "#user_friendly_values" do
43
- let(:document_attrs) {
44
- {
45
- case_type: "ca98-and-civil-cartels",
46
- market_sector: "aerospace",
47
- }
48
- }
49
-
50
- let(:formatted_attrs) {
51
- {
52
- "case_type" => {
53
- :label => "Case type",
54
- :values => [
55
- {
56
- :label => "CA98 and civil cartels",
57
- :slug => "ca98-and-civil-cartels"
58
- }
59
- ]
60
- },
61
- "market_sector" => {
62
- :label => "Market sector",
63
- :values => [
64
- {
65
- :label => "Aerospace",
66
- :slug=>"aerospace"
67
- }
68
- ]
69
- }
70
- }
71
- }
72
-
73
- it "formats the given keys and values" do
74
- schema.user_friendly_values(document_attrs).must_equal(formatted_attrs)
75
- end
76
-
77
- describe "when a value is not found" do
78
- let(:document_attrs) {
79
- {
80
- market_sector: "does-not-exist"
81
- }
82
- }
83
-
84
- it "raises an error" do
85
- ->(){
86
- schema.user_friendly_values(document_attrs)
87
- }.must_raise(
88
- GdsApi::FinderSchema::NotFoundError,
89
- "market sector 'does-not-exist' not found in cma-cases schema")
90
- end
91
- end
92
-
93
- describe "when a value is an array of values" do
94
- let(:document_attrs) {
95
- {
96
- market_sector: ["aerospace"],
97
- }
98
- }
99
-
100
- let(:formatted_attrs) {
101
- {
102
- "market_sector" => {
103
- :label => "Market sector",
104
- :values => [
105
- {
106
- :label => "Aerospace",
107
- :slug=>"aerospace"
108
- }
109
- ]
110
- }
111
- }
112
- }
113
-
114
- it "formats the given keys and values" do
115
- schema.user_friendly_values(document_attrs).must_equal(formatted_attrs)
116
- end
117
- end
118
- end
119
- end