gds-api-adapters 10.5.0 → 10.6.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.
@@ -1,13 +1,21 @@
1
1
  require_relative 'base'
2
+ require_relative 'finder_schema'
2
3
 
3
4
  module GdsApi
4
5
  class FinderApi < Base
6
+ def initialize(url, options, &block)
7
+ @schema_factory = options.fetch(:schema_factory) { Schema.method(:new) }
8
+ super(url, options, &block)
9
+ end
10
+
5
11
  def get_documents(finder_slug, options = {})
6
12
  get_json(documents_url(finder_slug, options))
7
13
  end
8
14
 
9
15
  def get_schema(finder_slug, options = {})
10
- get_json(finder_url(finder_slug, 'schema', options))
16
+ @schema_factory.call(
17
+ get_json(finder_url(finder_slug, 'schema', options)).to_hash
18
+ )
11
19
  end
12
20
 
13
21
  private
@@ -0,0 +1,58 @@
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
+ user_friendly_facet_label(k.to_s),
12
+ find_schema_allowed_value_entry(k.to_s, v)
13
+ )
14
+ end
15
+ end
16
+
17
+ attr_reader :schema_hash
18
+ private :schema_hash
19
+
20
+ private
21
+
22
+ def slug
23
+ schema_hash.fetch("slug")
24
+ end
25
+
26
+ def user_friendly_facet_label(facet_key)
27
+ find_facet(facet_key.to_s).fetch("name")
28
+ end
29
+
30
+ def find_schema_allowed_value_entry(facet_key, value)
31
+ value_label_pair = allowed_values_for(facet_key)
32
+ .find { |schema_value|
33
+ schema_value.fetch("value") == value
34
+ }
35
+
36
+ if value_label_pair.nil?
37
+ raise_value_not_found_error(facet_key, value)
38
+ else
39
+ value_label_pair.fetch("label")
40
+ end
41
+ end
42
+
43
+ def allowed_values_for(facet_key)
44
+ find_facet(facet_key).fetch("allowed_values")
45
+ end
46
+
47
+ def find_facet(facet_key)
48
+ facets.find { |facet| facet.fetch("key") == facet_key }
49
+ end
50
+
51
+ def facets
52
+ schema_hash.fetch("facets")
53
+ end
54
+
55
+ def raise_value_not_found_error(facet_key, value)
56
+ raise NotFoundError.new("#{facet_key} value '#{value}' not found in #{slug} schema")
57
+ end
58
+ end
@@ -8,6 +8,7 @@ require 'gds_api/need_api'
8
8
  require 'gds_api/panopticon'
9
9
  require 'gds_api/publisher'
10
10
  require 'gds_api/worldwide'
11
+ require 'gds_api/finder_api'
11
12
 
12
13
  module GdsApi
13
14
  module Helpers
@@ -55,6 +56,10 @@ module GdsApi
55
56
  @worldwide_api ||= GdsApi::Worldwide.new(Plek.current.find("whitehall-admin"), options)
56
57
  end
57
58
 
59
+ def finder_api(options = {})
60
+ @finder_api ||= FinderApi.new(Plek.current.find("finder-api"), options)
61
+ end
62
+
58
63
  def self.included(klass)
59
64
  if klass.respond_to?(:helper_method)
60
65
  klass.helper_method :publisher_api, :panopticon_api, :imminence_api, :content_api, :licence_application_api
@@ -0,0 +1,24 @@
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,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '10.5.0'
2
+ VERSION = '10.6.0'
3
3
  end
@@ -4,9 +4,14 @@ require 'gds_api/finder_api'
4
4
  describe GdsApi::FinderApi do
5
5
  before do
6
6
  @base_api_url = Plek.current.find('finder-api')
7
- @api = GdsApi::FinderApi.new(@base_api_url)
7
+ @api = GdsApi::FinderApi.new(@base_api_url, schema_factory: schema_factory)
8
8
  end
9
9
 
10
+ let(:schema) { Object.new }
11
+ let(:schema_factory) {
12
+ ->(schema_as_a_hash) { schema }
13
+ }
14
+
10
15
  describe "get_documents" do
11
16
  it "should return all documents" do
12
17
  documents_hash = {
@@ -60,6 +65,11 @@ describe GdsApi::FinderApi do
60
65
  end
61
66
 
62
67
  describe "get_schema" do
68
+ let(:schema_factory) {
69
+ Minitest::Mock.new
70
+ .expect(:call, schema, [schema_hash])
71
+ }
72
+
63
73
  let(:schema_hash) {
64
74
  {'it is' => 'a schema'}
65
75
  }
@@ -69,21 +79,32 @@ describe GdsApi::FinderApi do
69
79
  }
70
80
 
71
81
  let(:schema_url) {
72
- "#{@base_api_url}/finders/some-finder-slug/schema.json"
82
+ "#{@base_api_url}/finders/cma-cases/schema.json"
73
83
  }
74
84
 
75
- it "should return the finder's schema" do
85
+ it "requests the finder's schema" do
76
86
  req = WebMock.stub_request(:get, schema_url).
77
87
  to_return(:body => schema_json,
78
88
  :headers => {"Content-type" => "application/json"})
79
89
 
80
- response = @api.get_schema("some-finder-slug")
81
- assert_equal 200, response.code
82
- assert_equal schema_hash, response.to_hash
90
+ response = @api.get_schema("cma-cases")
83
91
 
84
92
  assert_requested(req)
85
93
  end
86
94
 
95
+ it "constructs and returns a schema object" do
96
+ WebMock.stub_request(:get, schema_url)
97
+ .to_return(
98
+ :body => schema_json,
99
+ :headers => {"Content-type" => "application/json"},
100
+ )
101
+
102
+ returned_schema = @api.get_schema("cma-cases")
103
+
104
+ assert_equal schema, returned_schema
105
+ schema_factory.verify
106
+ end
107
+
87
108
  it "should forward query parameters" do
88
109
  req = WebMock.stub_request(:get, "#{@base_api_url}/finders/some-finder-slug/schema.json").
89
110
  with(query: {locale: 'fr-FR'}).
@@ -91,8 +112,6 @@ describe GdsApi::FinderApi do
91
112
  :headers => {"Content-type" => "application/json"})
92
113
 
93
114
  response = @api.get_schema("some-finder-slug", locale: 'fr-FR')
94
- assert_equal 200, response.code
95
- assert_equal schema_hash, response.to_hash
96
115
 
97
116
  assert_requested(req)
98
117
  end
@@ -0,0 +1,77 @@
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" => "CA98 and civil cartels",
53
+ "Market sector" => "Aerospace",
54
+ }
55
+ }
56
+
57
+ it "formats the given keys and values" do
58
+ schema.user_friendly_values(document_attrs).must_equal(formatted_attrs)
59
+ end
60
+
61
+ describe "when a value is not found" do
62
+ let(:document_attrs) {
63
+ {
64
+ market_sector: "does-not-exist"
65
+ }
66
+ }
67
+
68
+ it "raises an error" do
69
+ ->(){
70
+ schema.user_friendly_values(document_attrs)
71
+ }.must_raise(
72
+ GdsApi::FinderSchema::NotFoundError,
73
+ "market sector 'does-not-exist' not found in cma-cases schema")
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,103 @@
1
+ {
2
+ "slug": "cma-cases",
3
+ "name": "Competition and Markets Authority cases",
4
+ "document_noun": "case",
5
+ "facets": [
6
+ {
7
+ "key": "case_type",
8
+ "name": "Case type",
9
+ "type": "single-select",
10
+ "include_blank": "All case types",
11
+ "allowed_values": [
12
+ {"label": "CA98 and civil cartels", "value": "ca98-and-civil-cartels"},
13
+ {"label": "Consumer enforcement", "value": "consumer-enforcement"},
14
+ {"label": "Criminal cartels", "value": "criminal-cartels"},
15
+ {"label": "Markets", "value": "markets"},
16
+ {"label": "Mergers", "value": "mergers"},
17
+ {"label": "Regulatory references and appeals", "value": "regulatory-references-and-appeals"},
18
+ {"label": "Reviews of orders and undertakings", "value": "review-of-orders-and-undertakings"}
19
+ ]
20
+ },
21
+
22
+ {
23
+ "key": "case_state",
24
+ "name": "Case state",
25
+ "type": "single-select",
26
+ "include_blank": false,
27
+ "allowed_values": [
28
+ {"label": "Open", "value": "open"},
29
+ {"label": "Closed", "value": "closed"}
30
+ ]
31
+ },
32
+
33
+ {
34
+ "key": "market_sector",
35
+ "name": "Market sector",
36
+ "type": "single-select",
37
+ "include_blank": false,
38
+ "allowed_values": [
39
+ {"label": "Aerospace", "value": "aerospace"},
40
+ {"label": "Agriculture, environment and natural resources", "value": "agriculture-environment-and-natural-resources"},
41
+ {"label": "Building and construction", "value": "building-and-construction"},
42
+ {"label": "Chemicals", "value": "chemicals"},
43
+ {"label": "Clothing, footwear and fashion", "value": "clothing-footwear-and-fashion"},
44
+ {"label": "Communications", "value": "communications"},
45
+ {"label": "Defence", "value": "defence"},
46
+ {"label": "Distribution and Service Industries", "value": "distribution-and-service-industries"},
47
+ {"label": "Electronics Industry", "value": "electronics-industry"},
48
+ {"label": "Energy", "value": "energy"},
49
+ {"label": "Engineering", "value": "engineering"},
50
+ {"label": "Financial services", "value": "financial-services"},
51
+ {"label": "Fire, police, and security", "value": "fire-police-and-security"},
52
+ {"label": "Food manufacturing", "value": "food-manufacturing"},
53
+ {"label": "Giftware, jewellery and tableware", "value": "giftware-jewellery-and-tableware"},
54
+ {"label": "Healthcare and medical equipment", "value": "healthcare-and-medical-equipment"},
55
+ {"label": "Household goods, furniture and furnishings", "value": "household-goods-furniture-and-furnishings"},
56
+ {"label": "Mineral extraction, mining and quarrying", "value": "mineral-extraction-mining-and-quarrying"},
57
+ {"label": "Motor Industry", "value": "motor-industry"},
58
+ {"label": "Oil and Gas refining and Petrochemicals", "value": "oil-and-gas-refining-and-petrochemicals"},
59
+ {"label": "Paper printing and packaging", "value": "paper-printing-and-packaging"},
60
+ {"label": "Pharmaceuticals", "value": "pharmaceuticals"},
61
+ {"label": "Public markets", "value": "public-markets"},
62
+ {"label": "Recreation and Leisure", "value": "recreation-and-leisure"},
63
+ {"label": "Retail and wholesale", "value": "retail-and-wholesale"},
64
+ {"label": "Telecommunications", "value": "telecommunications"},
65
+ {"label": "Textiles", "value": "textiles"},
66
+ {"label": "Transport", "value": "transport"},
67
+ {"label": "Utilities", "value": "utilities"}
68
+ ]
69
+ },
70
+
71
+ {
72
+ "key": "outcome_type",
73
+ "name": "Outcome",
74
+ "type": "single-select",
75
+ "include_blank": false,
76
+ "allowed_values": [
77
+ {"label": "CA98 - administrative priorities", "value": "ca98-administrative-priorities"},
78
+ {"label": "CA98 - commitment", "value": "ca98-commitment"},
79
+ {"label": "CA98 - infringement Chapter I", "value": "ca98-infringement-chapter-i"},
80
+ {"label": "CA98 - infringement Chapter II", "value": "ca98-infringement-chapter-ii"},
81
+ {"label": "CA98 - no grounds for action/non-infringement", "value": "ca98-no-grounds-for-action-non-infringement"},
82
+ {"label": "Consumer enforcement - court order", "value": "consumer-enforcement-court-order"},
83
+ {"label": "Consumer enforcement - no action", "value": "consumer-enforcement-no-action"},
84
+ {"label": "Consumer enforcement - undertakings", "value": "consumer-enforcement-undertakings"},
85
+ {"label": "Criminal cartels - verdict", "value": "criminal-cartels-verdict"},
86
+ {"label": "Markets - phase 1 no enforcement action", "value": "markets-phase-1-no-enforcement-action"},
87
+ {"label": "Markets - phase 1 referral", "value": "markets-phase-1-referral"},
88
+ {"label": "Markets - phase 1 undertakings in lieu of reference", "value": "markets-phase-1-undertakings-in-lieu-of-reference"},
89
+ {"label": "Markets - phase 2 adverse effect on competition leading to remedies", "value": "markets-phase-2-adverse-effect-on-competition-leading-to-remedies"},
90
+ {"label": "Markets - phase 2 clearance - no adverse effect on competition", "value": "markets-phase-2-clearance-no-adverse-effect-on-competition"},
91
+ {"label": "Markets - phase 2 decision to dispense with procedural obligations", "value": "markets-phase-2-decision-to-dispense-with-procedural-obligations"},
92
+ {"label": "Mergers - phase 1 clearance with undertakings in lieu", "value": "mergers-phase-1-clearance-with-undertakings-in-lieu"},
93
+ {"label": "Mergers - phase 1 clearance", "value": "mergers-phase-1-clearance"},
94
+ {"label": "Mergers - phase 1 found not to qualify", "value": "mergers-phase-1-found-not-to-qualify"},
95
+ {"label": "Mergers - phase 1 referral", "value": "mergers-phase-1-referral"},
96
+ {"label": "Mergers - phase 2 clearance with remedies", "value": "mergers-phase-2-clearance-with-remedies"},
97
+ {"label": "Mergers - phase 2 clearance", "value": "mergers-phase-2-clearance"},
98
+ {"label": "Mergers - phase 2 prohibition", "value": "mergers-phase-2-prohibition"},
99
+ {"label": "Regulatory references and appeals - final determination", "value": "regulatory-references-and-appeals-final-determination"}
100
+ ]
101
+ }
102
+ ]
103
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.5.0
4
+ version: 10.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-01 00:00:00.000000000 Z
12
+ date: 2014-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -282,6 +282,7 @@ files:
282
282
  - lib/gds_api/finder_api.rb
283
283
  - lib/gds_api/railtie.rb
284
284
  - lib/gds_api/exceptions.rb
285
+ - lib/gds_api/finder_schema.rb
285
286
  - lib/gds_api/performance_platform/data_in.rb
286
287
  - lib/gds_api/json_client.rb
287
288
  - lib/gds_api/base.rb
@@ -291,6 +292,7 @@ files:
291
292
  - lib/gds_api/panopticon/registerer.rb
292
293
  - lib/gds_api/test_helpers/support.rb
293
294
  - lib/gds_api/test_helpers/need_api.rb
295
+ - lib/gds_api/test_helpers/finder_api.rb
294
296
  - lib/gds_api/test_helpers/content_api/artefact_stub.rb
295
297
  - lib/gds_api/test_helpers/performance_platform/data_in.rb
296
298
  - lib/gds_api/test_helpers/licence_application.rb
@@ -333,6 +335,7 @@ files:
333
335
  - test/support_api_test.rb
334
336
  - test/mapit_test.rb
335
337
  - test/pp_data_in_test.rb
338
+ - test/finder_schema_test.rb
336
339
  - test/need_api_test.rb
337
340
  - test/publisher_api_test.rb
338
341
  - test/fact_cave_test.rb
@@ -351,6 +354,7 @@ files:
351
354
  - test/asset_manager_test.rb
352
355
  - test/fixtures/hello.txt
353
356
  - test/fixtures/world_organisations_australia.json
357
+ - test/fixtures/finder_api/cma-case-schema.json
354
358
  - test/router_test.rb
355
359
  - test/list_response_test.rb
356
360
  - test/external_link_tracker_test.rb
@@ -372,7 +376,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
372
376
  version: '0'
373
377
  segments:
374
378
  - 0
375
- hash: -3888185947824795971
379
+ hash: 1564566442515579197
376
380
  required_rubygems_version: !ruby/object:Gem::Requirement
377
381
  none: false
378
382
  requirements:
@@ -381,7 +385,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
381
385
  version: '0'
382
386
  segments:
383
387
  - 0
384
- hash: -3888185947824795971
388
+ hash: 1564566442515579197
385
389
  requirements: []
386
390
  rubyforge_project:
387
391
  rubygems_version: 1.8.23
@@ -392,6 +396,7 @@ test_files:
392
396
  - test/support_api_test.rb
393
397
  - test/mapit_test.rb
394
398
  - test/pp_data_in_test.rb
399
+ - test/finder_schema_test.rb
395
400
  - test/need_api_test.rb
396
401
  - test/publisher_api_test.rb
397
402
  - test/fact_cave_test.rb
@@ -410,6 +415,7 @@ test_files:
410
415
  - test/asset_manager_test.rb
411
416
  - test/fixtures/hello.txt
412
417
  - test/fixtures/world_organisations_australia.json
418
+ - test/fixtures/finder_api/cma-case-schema.json
413
419
  - test/router_test.rb
414
420
  - test/list_response_test.rb
415
421
  - test/external_link_tracker_test.rb