gds-api-adapters 59.1.0 → 59.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5545c2f65d5f31f2aaf737590b5204ee385ec783ad553d09515ac0b7e3d73929
4
- data.tar.gz: 850f74dc5f709157bad18d79308edd76ae3323b8e33d867ebfda9f075eb82b65
3
+ metadata.gz: ec24c3b59b3cc1db154edf2b009ef223651a1530067b85931e11201f993276c5
4
+ data.tar.gz: e3b9c3fa2dab1c6741049a91175290b7277c4630bcc958621ac8f47412f35d12
5
5
  SHA512:
6
- metadata.gz: ce46e8885c9e7b37cffa51ff0a3666270e825ccc073c17659d23d85ed15598f190eb8b016f599aec26275c829062106e3d7500b76f74a0b88bfa194d76568901
7
- data.tar.gz: af840ab6b6d7053d2e746bdfe64fc5a00daeba7105d7bbabf771ba54c74e539579cd95d3de2131936d06113063865644f096a858cd4a0ca352e270f9f6791721
6
+ metadata.gz: cd38630932fd7eee080846734651f34147924e43cbffdf53636e111c9649ff292fe7ee930f0a4496a4a75c341887246cd564c9d6dc4e38754d13e942685a79df
7
+ data.tar.gz: fb37484a2789b2a2f0c90faf6fa9f1cd1f45f9011ace188980687555f72141cc1fe9f024f73a758a1ad512f3b6796c341f82920747e0e4f120f0d95f47aa6dfe
data/lib/gds_api.rb CHANGED
@@ -14,6 +14,7 @@ require 'gds_api/publishing_api'
14
14
  require 'gds_api/publishing_api_v2'
15
15
  require 'gds_api/router'
16
16
  require 'gds_api/rummager'
17
+ require 'gds_api/search'
17
18
  require 'gds_api/support'
18
19
  require 'gds_api/support_api'
19
20
  require 'gds_api/worldwide'
@@ -168,14 +169,16 @@ module GdsApi
168
169
  #
169
170
  # @return [GdsApi::Rummager]
170
171
  def self.rummager(options = {})
172
+ warn "GdsApi.rummager is deprecated. Use GdsApi.search instead."
173
+
171
174
  GdsApi::Rummager.new(Plek.find('search'), options)
172
175
  end
173
176
 
174
- # Creates a GdsApi::Rummager adapter to access via a search.* hostname
177
+ # Creates a GdsApi::Search adapter to access via a search.* hostname
175
178
  #
176
- # @return [GdsApi::Rummager]
179
+ # @return [GdsApi::Search]
177
180
  def self.search(options = {})
178
- GdsApi::Rummager.new(Plek.find('search'), options)
181
+ GdsApi::Search.new(Plek.find('search'), options)
179
182
  end
180
183
 
181
184
  # Creates a GdsApi::Support adapter
data/lib/gds_api/base.rb CHANGED
@@ -18,14 +18,15 @@ class GdsApi::Base
18
18
  GdsApi::JsonClient.new(options)
19
19
  end
20
20
 
21
- def_delegators :client, :get_json,
22
- :post_json,
23
- :put_json,
24
- :patch_json,
25
- :delete_json,
26
- :get_raw, :get_raw!,
27
- :put_multipart,
28
- :post_multipart
21
+ def_delegators :client,
22
+ :get_json,
23
+ :post_json,
24
+ :put_json,
25
+ :patch_json,
26
+ :delete_json,
27
+ :get_raw, :get_raw!,
28
+ :put_multipart,
29
+ :post_multipart
29
30
 
30
31
  attr_reader :options
31
32
 
@@ -1,184 +1,7 @@
1
- require 'gds_api/base'
2
- require 'rack/utils'
1
+ require 'gds_api/search'
3
2
 
4
3
  module GdsApi
5
- # @api documented
6
- class Rummager < Base
7
- # @api documented
8
- class V1 < SimpleDelegator
9
- def add_document(type, id, document)
10
- post_json(
11
- documents_url,
12
- document.merge(
13
- _type: type,
14
- _id: id,
15
- )
16
- )
17
- end
18
-
19
- def delete_document(type, id)
20
- delete_json(
21
- "#{documents_url}/#{id}",
22
- nil,
23
- _type: type,
24
- )
25
- end
26
- end
27
-
28
- # @api documented
29
- class V2 < SimpleDelegator
30
- class InvalidIndex < StandardError; end
31
-
32
- def add_document(id, document, index_name)
33
- raise(InvalidIndex, index_name) unless index_name == 'metasearch'
34
-
35
- post_json(
36
- "#{base_url}/v2/metasearch/documents",
37
- document.merge(
38
- _id: id,
39
- )
40
- )
41
- end
42
-
43
- def delete_document(id, index_name)
44
- raise(InvalidIndex, index_name) unless index_name == 'metasearch'
45
-
46
- delete_json("#{base_url}/v2/metasearch/documents/#{id}")
47
- end
48
- end
49
-
50
- DEFAULT_API_VERSION = 'V1'.freeze
51
- API_VERSIONS = {
52
- 'V1' => GdsApi::Rummager::V1,
53
- 'V2' => GdsApi::Rummager::V2,
54
- }.freeze
55
- class UnknownAPIVersion < StandardError; end
56
-
57
- def initialize(endpoint_url, options = {})
58
- super
59
- # The API version provides a simple wrapper around this base class so that we
60
- # can still access the shared methods present in this class.
61
- version = options.fetch(:api_version, DEFAULT_API_VERSION)
62
- api_class = API_VERSIONS[version] || raise(UnknownAPIVersion)
63
- @api = api_class.new(self)
64
- end
65
-
66
- # Perform a search.
67
- #
68
- # @param args [Hash] A valid search query. See Rummager documentation for options.
69
- #
70
- # @see https://github.com/alphagov/rummager/blob/master/doc/search-api.md
71
- def search(args, additional_headers = {})
72
- request_url = "#{base_url}/search.json?#{Rack::Utils.build_nested_query(args)}"
73
- get_json(request_url, additional_headers)
74
- end
75
-
76
- # Perform a batch search.
77
- #
78
- # @param searches [Array] An array valid search queries. Maximum of 6. See Rummager documentation for options.
79
- #
80
- # # @see https://github.com/alphagov/rummager/blob/master/doc/search-api.md
81
- def batch_search(searches, additional_headers = {})
82
- url_friendly_searches = searches.each_with_index.map do |search, index|
83
- { index => search }
84
- end
85
- searches_query = { search: url_friendly_searches }
86
- request_url = "#{base_url}/batch_search.json?#{Rack::Utils.build_nested_query(searches_query)}"
87
- get_json(request_url, additional_headers)
88
- end
89
-
90
- # Perform a search, returning the results as an enumerator.
91
- #
92
- # The enumerator abstracts away rummager's pagination and fetches new pages when
93
- # necessary.
94
- #
95
- # @param args [Hash] A valid search query. See Rummager documentation for options.
96
- # @param page_size [Integer] Number of results in each page.
97
- #
98
- # @see https://github.com/alphagov/rummager/blob/master/doc/search-api.md
99
- def search_enum(args, page_size: 100, additional_headers: {})
100
- Enumerator.new do |yielder|
101
- (0..Float::INFINITY).step(page_size).each do |index|
102
- search_params = args.merge(start: index.to_i, count: page_size)
103
- results = search(search_params, additional_headers).to_h.fetch('results', [])
104
- results.each do |result|
105
- yielder << result
106
- end
107
- if results.count < page_size
108
- break
109
- end
110
- end
111
- end
112
- end
113
-
114
- # Advanced search.
115
- #
116
- # @deprecated Only in use by Whitehall. Use the `#search` method.
117
- def advanced_search(args)
118
- raise ArgumentError.new("Args cannot be blank") if args.nil? || args.empty?
119
-
120
- request_path = "#{base_url}/advanced_search?#{Rack::Utils.build_nested_query(args)}"
121
- get_json(request_path)
122
- end
123
-
124
- # Add a document to the search index.
125
- #
126
- # @param type [String] The rummager/elasticsearch document type.
127
- # @param id [String] The rummager/elasticsearch id. Typically the same as the `link` field, but this is not strictly enforced.
128
- # @param document [Hash] The document to add. Must match the rummager schema matchin the `type` parameter and contain a `link` field.
129
- # @param index_name (V2 only) Name of the index to be deleted from on
130
- # GOV.UK - we only allow deletion from metasearch
131
- # @return [GdsApi::Response] A status code of 202 indicates the document has been successfully queued.
132
- #
133
- # @see https://github.com/alphagov/rummager/blob/master/doc/documents.md
134
- def add_document(*args)
135
- @api.add_document(*args)
136
- end
137
-
138
- # Delete a content-document from the index by base path.
139
- #
140
- # Content documents are pages on GOV.UK that have a base path and are
141
- # returned in searches. This excludes best bets, recommended-links,
142
- # and contacts, which may be deleted with `delete_document`.
143
- #
144
- # @param base_path Base path of the page on GOV.UK.
145
- # @see https://github.com/alphagov/rummager/blob/master/doc/content-api.md
146
- def delete_content(base_path)
147
- request_url = "#{base_url}/content?link=#{base_path}"
148
- delete_json(request_url)
149
- end
150
-
151
- # Retrieve a content-document from the index.
152
- #
153
- # Content documents are pages on GOV.UK that have a base path and are
154
- # returned in searches. This excludes best bets, recommended-links,
155
- # and contacts.
156
- #
157
- # @param base_path [String] Base path of the page on GOV.UK.
158
- # @see https://github.com/alphagov/rummager/blob/master/doc/content-api.md
159
- def get_content(base_path)
160
- request_url = "#{base_url}/content?link=#{base_path}"
161
- get_json(request_url)
162
- end
163
-
164
- # Delete a non-content document from the search index.
165
- #
166
- # For example, best bets, recommended links, or contacts.
167
- #
168
- # @param type [String] The rummager/elasticsearch document type.
169
- # @param id [String] The rummager/elasticsearch id. Typically the same as the `link` field.
170
- # @param index_name (V2 only) Name of the index to be deleted from on
171
- # GOV.UK - we only allow deletion from metasearch
172
- def delete_document(*args)
173
- @api.delete_document(*args)
174
- end
175
-
176
- def base_url
177
- endpoint
178
- end
179
-
180
- def documents_url
181
- "#{base_url}/documents"
182
- end
4
+ class Rummager < Search
5
+ warn "GdsApi::Rummager is deprecated. Use GdsApi::Search instead."
183
6
  end
184
7
  end
@@ -0,0 +1,184 @@
1
+ require 'gds_api/base'
2
+ require 'rack/utils'
3
+
4
+ module GdsApi
5
+ # @api documented
6
+ class Search < Base
7
+ # @api documented
8
+ class V1 < SimpleDelegator
9
+ def add_document(type, id, document)
10
+ post_json(
11
+ documents_url,
12
+ document.merge(
13
+ _type: type,
14
+ _id: id,
15
+ )
16
+ )
17
+ end
18
+
19
+ def delete_document(type, id)
20
+ delete_json(
21
+ "#{documents_url}/#{id}",
22
+ nil,
23
+ _type: type,
24
+ )
25
+ end
26
+ end
27
+
28
+ # @api documented
29
+ class V2 < SimpleDelegator
30
+ class InvalidIndex < StandardError; end
31
+
32
+ def add_document(id, document, index_name)
33
+ raise(InvalidIndex, index_name) unless index_name == 'metasearch'
34
+
35
+ post_json(
36
+ "#{base_url}/v2/metasearch/documents",
37
+ document.merge(
38
+ _id: id,
39
+ )
40
+ )
41
+ end
42
+
43
+ def delete_document(id, index_name)
44
+ raise(InvalidIndex, index_name) unless index_name == 'metasearch'
45
+
46
+ delete_json("#{base_url}/v2/metasearch/documents/#{id}")
47
+ end
48
+ end
49
+
50
+ DEFAULT_API_VERSION = 'V1'.freeze
51
+ API_VERSIONS = {
52
+ 'V1' => GdsApi::Search::V1,
53
+ 'V2' => GdsApi::Search::V2,
54
+ }.freeze
55
+ class UnknownAPIVersion < StandardError; end
56
+
57
+ def initialize(endpoint_url, options = {})
58
+ super
59
+ # The API version provides a simple wrapper around this base class so that we
60
+ # can still access the shared methods present in this class.
61
+ version = options.fetch(:api_version, DEFAULT_API_VERSION)
62
+ api_class = API_VERSIONS[version] || raise(UnknownAPIVersion)
63
+ @api = api_class.new(self)
64
+ end
65
+
66
+ # Perform a search.
67
+ #
68
+ # @param args [Hash] A valid search query. See search-api documentation for options.
69
+ #
70
+ # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
71
+ def search(args, additional_headers = {})
72
+ request_url = "#{base_url}/search.json?#{Rack::Utils.build_nested_query(args)}"
73
+ get_json(request_url, additional_headers)
74
+ end
75
+
76
+ # Perform a batch search.
77
+ #
78
+ # @param searches [Array] An array valid search queries. Maximum of 6. See search-api documentation for options.
79
+ #
80
+ # # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
81
+ def batch_search(searches, additional_headers = {})
82
+ url_friendly_searches = searches.each_with_index.map do |search, index|
83
+ { index => search }
84
+ end
85
+ searches_query = { search: url_friendly_searches }
86
+ request_url = "#{base_url}/batch_search.json?#{Rack::Utils.build_nested_query(searches_query)}"
87
+ get_json(request_url, additional_headers)
88
+ end
89
+
90
+ # Perform a search, returning the results as an enumerator.
91
+ #
92
+ # The enumerator abstracts away search-api's pagination and fetches new pages when
93
+ # necessary.
94
+ #
95
+ # @param args [Hash] A valid search query. See search-api documentation for options.
96
+ # @param page_size [Integer] Number of results in each page.
97
+ #
98
+ # @see https://github.com/alphagov/search-api/blob/master/doc/search-api.md
99
+ def search_enum(args, page_size: 100, additional_headers: {})
100
+ Enumerator.new do |yielder|
101
+ (0..Float::INFINITY).step(page_size).each do |index|
102
+ search_params = args.merge(start: index.to_i, count: page_size)
103
+ results = search(search_params, additional_headers).to_h.fetch('results', [])
104
+ results.each do |result|
105
+ yielder << result
106
+ end
107
+ if results.count < page_size
108
+ break
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ # Advanced search.
115
+ #
116
+ # @deprecated Only in use by Whitehall. Use the `#search` method.
117
+ def advanced_search(args)
118
+ raise ArgumentError.new("Args cannot be blank") if args.nil? || args.empty?
119
+
120
+ request_path = "#{base_url}/advanced_search?#{Rack::Utils.build_nested_query(args)}"
121
+ get_json(request_path)
122
+ end
123
+
124
+ # Add a document to the search index.
125
+ #
126
+ # @param type [String] The search-api document type.
127
+ # @param id [String] The search-api/elasticsearch id. Typically the same as the `link` field, but this is not strictly enforced.
128
+ # @param document [Hash] The document to add. Must match the search-api schema matching the `type` parameter and contain a `link` field.
129
+ # @param index_name (V2 only) Name of the index to be deleted from on
130
+ # GOV.UK - we only allow deletion from metasearch
131
+ # @return [GdsApi::Response] A status code of 202 indicates the document has been successfully queued.
132
+ #
133
+ # @see https://github.com/alphagov/search-api/blob/master/doc/documents.md
134
+ def add_document(*args)
135
+ @api.add_document(*args)
136
+ end
137
+
138
+ # Delete a content-document from the index by base path.
139
+ #
140
+ # Content documents are pages on GOV.UK that have a base path and are
141
+ # returned in searches. This excludes best bets, recommended-links,
142
+ # and contacts, which may be deleted with `delete_document`.
143
+ #
144
+ # @param base_path Base path of the page on GOV.UK.
145
+ # @see https://github.com/alphagov/search-api/blob/master/doc/content-api.md
146
+ def delete_content(base_path)
147
+ request_url = "#{base_url}/content?link=#{base_path}"
148
+ delete_json(request_url)
149
+ end
150
+
151
+ # Retrieve a content-document from the index.
152
+ #
153
+ # Content documents are pages on GOV.UK that have a base path and are
154
+ # returned in searches. This excludes best bets, recommended-links,
155
+ # and contacts.
156
+ #
157
+ # @param base_path [String] Base path of the page on GOV.UK.
158
+ # @see https://github.com/alphagov/search-api/blob/master/doc/content-api.md
159
+ def get_content(base_path)
160
+ request_url = "#{base_url}/content?link=#{base_path}"
161
+ get_json(request_url)
162
+ end
163
+
164
+ # Delete a non-content document from the search index.
165
+ #
166
+ # For example, best bets, recommended links, or contacts.
167
+ #
168
+ # @param type [String] The search-api document type.
169
+ # @param id [String] The search-api/elasticsearch id. Typically the same as the `link` field.
170
+ # @param index_name (V2 only) Name of the index to be deleted from on
171
+ # GOV.UK - we only allow deletion from metasearch
172
+ def delete_document(*args)
173
+ @api.delete_document(*args)
174
+ end
175
+
176
+ def base_url
177
+ endpoint
178
+ end
179
+
180
+ def documents_url
181
+ "#{base_url}/documents"
182
+ end
183
+ end
184
+ end
@@ -1,198 +1,90 @@
1
1
  require 'json'
2
2
  require 'gds_api/test_helpers/json_client_helper'
3
+ require 'gds_api/test_helpers/search'
3
4
 
4
5
  module GdsApi
5
6
  module TestHelpers
6
7
  module Rummager
7
- RUMMAGER_ENDPOINT = Plek.current.find('search')
8
+ warn "GdsApi::TestHelpers::Rummager is deprecated. Use GdsApi::TestHelpers::Search instead."
8
9
 
9
- def stub_any_rummager_post(index: nil)
10
- if index
11
- stub_request(:post, %r{#{RUMMAGER_ENDPOINT}/#{index}/documents})
12
- .to_return(status: [202, "Accepted"])
13
- else
14
- stub_request(:post, %r{#{RUMMAGER_ENDPOINT}/documents})
15
- .to_return(status: [202, "Accepted"])
16
- end
17
- end
10
+ include GdsApi::TestHelpers::Search
18
11
 
19
- def assert_rummager_posted_item(attributes, index: nil, **options)
20
- if index
21
- url = RUMMAGER_ENDPOINT + "/#{index}/documents"
22
- else
23
- url = RUMMAGER_ENDPOINT + "/documents"
24
- end
12
+ RUMMAGER_ENDPOINT = SEARCH_ENDPOINT
25
13
 
26
- assert_requested(:post, url, **options) do |req|
27
- data = JSON.parse(req.body)
28
- attributes.to_a.all? do |key, value|
29
- data[key.to_s] == value
30
- end
31
- end
14
+ def stub_any_rummager_post(*args)
15
+ stub_any_search_post(*args)
32
16
  end
33
17
 
34
- def stub_any_rummager_search
35
- stub_request(:get, %r{#{RUMMAGER_ENDPOINT}/search.json})
18
+ def assert_rummager_posted_item(*args)
19
+ assert_search_posted_item(*args)
36
20
  end
37
21
 
38
- def stub_any_rummager_search_to_return_no_results
39
- stub_any_rummager_search.to_return(body: { results: [] }.to_json)
22
+ def stub_any_rummager_search(*args)
23
+ stub_any_search(*args)
40
24
  end
41
25
 
42
- def assert_rummager_search(options)
43
- assert_requested :get, "#{RUMMAGER_ENDPOINT}/search.json", **options
26
+ def stub_any_rummager_search_to_return_no_results(*args)
27
+ stub_any_search_to_return_no_results(*args)
44
28
  end
45
29
 
46
- def stub_any_rummager_delete(index: nil)
47
- if index
48
- stub_request(:delete, %r{#{RUMMAGER_ENDPOINT}/#{index}/documents/.*})
49
- else
50
- # use rummager's default index
51
- stub_request(:delete, %r{#{RUMMAGER_ENDPOINT}/documents/.*})
52
- end
30
+ def assert_rummager_search(*args)
31
+ assert_search(*args)
53
32
  end
54
33
 
55
- def stub_any_rummager_delete_content
56
- stub_request(:delete, %r{#{RUMMAGER_ENDPOINT}/content.*})
34
+ def stub_any_rummager_delete(*args)
35
+ stub_any_search_delete(*args)
57
36
  end
58
37
 
59
- def assert_rummager_deleted_item(id, index: nil, **options)
60
- if id =~ %r{^/}
61
- raise ArgumentError, 'Rummager id must not start with a slash'
62
- end
63
-
64
- if index
65
- assert_requested(
66
- :delete,
67
- %r{#{RUMMAGER_ENDPOINT}/#{index}/documents/#{id}},
68
- **options
69
- )
70
- else
71
- assert_requested(
72
- :delete,
73
- %r{#{RUMMAGER_ENDPOINT}/documents/#{id}},
74
- **options
75
- )
76
- end
38
+ def stub_any_rummager_delete_content(*args)
39
+ stub_any_search_delete_content(*args)
77
40
  end
78
41
 
79
- def assert_rummager_deleted_content(base_path, **options)
80
- assert_requested(
81
- :delete,
82
- %r{#{RUMMAGER_ENDPOINT}/content.*#{base_path}},
83
- **options
84
- )
42
+ def assert_rummager_deleted_item(*args)
43
+ assert_search_deleted_item(*args)
85
44
  end
86
45
 
87
- def stub_rummager_has_services_and_info_data_for_organisation
88
- stub_request_for(search_results_found)
89
- run_example_query
46
+ def assert_rummager_deleted_content(*args)
47
+ assert_search_deleted_content(*args)
90
48
  end
91
49
 
92
- def stub_rummager_has_no_services_and_info_data_for_organisation
93
- stub_request_for(no_search_results_found)
94
- run_example_query
50
+ def stub_rummager_has_services_and_info_data_for_organisation(*args)
51
+ stub_search_has_services_and_info_data_for_organisation(*args)
95
52
  end
96
53
 
97
- def stub_rummager_has_specialist_sector_organisations(_sub_sector)
98
- stub_request_for(sub_sector_organisations_results)
99
- run_example_query
54
+ def stub_rummager_has_no_services_and_info_data_for_organisation(*args)
55
+ stub_search_has_no_services_and_info_data_for_organisation(*args)
100
56
  end
101
57
 
102
- def stub_rummager_has_no_policies_for_any_type
103
- stub_request(:get, %r{/search.json})
104
- .to_return(body: no_search_results_found)
58
+ def stub_rummager_has_specialist_sector_organisations(*args)
59
+ stub_rummager_has_specialist_sector_organisations(*args)
105
60
  end
106
61
 
107
- def stub_rummager_has_policies_for_every_type(options = {})
108
- if options[:count]
109
- stub_request(:get, %r{/search.json.*count=#{options[:count]}.*})
110
- .to_return(body: first_n_results(new_policies_results, n: options[:count]))
111
- else
112
- stub_request(:get, %r{/search.json})
113
- .to_return(body: new_policies_results)
114
- end
62
+ def stub_rummager_has_no_policies_for_any_type(*args)
63
+ stub_search_has_no_policies_for_any_type(*args)
115
64
  end
116
65
 
117
- # Aliases for DEPRECATED methods
118
- alias_method :rummager_has_services_and_info_data_for_organisation, :stub_rummager_has_services_and_info_data_for_organisation
119
- alias_method :rummager_has_no_services_and_info_data_for_organisation, :stub_rummager_has_no_services_and_info_data_for_organisation
120
- alias_method :rummager_has_specialist_sector_organisations, :stub_rummager_has_specialist_sector_organisations
121
- alias_method :rummager_has_no_policies_for_any_type, :stub_rummager_has_no_policies_for_any_type
122
- alias_method :rummager_has_policies_for_every_type, :stub_rummager_has_policies_for_every_type
123
-
124
- private
125
-
126
- def stub_request_for(result_set)
127
- stub_request(:get, /example.com\/search/).to_return(body: result_set)
66
+ def stub_rummager_has_policies_for_every_type(*args)
67
+ stub_search_has_policies_for_every_type(*args)
128
68
  end
129
69
 
130
- def run_example_query
131
- client.search(example_query)
70
+ def rummager_has_services_and_info_data_for_organisation(*args)
71
+ stub_rummager_has_services_and_info_data_for_organisation(*args)
132
72
  end
133
73
 
134
- def search_results_found
135
- File.read(
136
- File.expand_path(
137
- "../../../test/fixtures/services_and_info_fixture.json",
138
- __dir__
139
- )
140
- )
74
+ def rummager_has_no_services_and_info_data_for_organisation(*args)
75
+ stub_rummager_has_no_services_and_info_data_for_organisation(*args)
141
76
  end
142
77
 
143
- def no_search_results_found
144
- File.read(
145
- File.expand_path(
146
- "../../../test/fixtures/no_services_and_info_data_found_fixture.json",
147
- __dir__
148
- )
149
- )
150
- end
151
-
152
- def sub_sector_organisations_results
153
- File.read(
154
- File.expand_path(
155
- "../../../test/fixtures/sub_sector_organisations.json",
156
- __dir__
157
- )
158
- )
159
- end
160
-
161
- def new_policies_results
162
- File.read(
163
- File.expand_path(
164
- "../../../test/fixtures/new_policies_for_dwp.json",
165
- __dir__
166
- )
167
- )
168
- end
169
-
170
- def old_policies_results
171
- File.read(
172
- File.expand_path(
173
- "../../../test/fixtures/old_policies_for_dwp.json",
174
- __dir__
175
- )
176
- )
177
- end
178
-
179
- def first_n_results(results, options)
180
- n = options[:n]
181
- results = JSON.parse(results)
182
- results["results"] = results["results"][0...n]
183
-
184
- results.to_json
78
+ def rummager_has_specialist_sector_organisations(*args)
79
+ stub_rummager_has_specialist_sector_organisations(*args)
185
80
  end
186
81
 
187
- def client
188
- GdsApi::Rummager.new("http://example.com")
82
+ def rummager_has_no_policies_for_any_type(*args)
83
+ stub_rummager_has_no_policies_for_any_type(*args)
189
84
  end
190
85
 
191
- def example_query
192
- {
193
- filter_organisations: ["an-organisation-slug"],
194
- facet_specialist_sectors: "1000,examples:4,example_scope:global,order:value.title"
195
- }
86
+ def rummager_has_policies_for_every_type(*args)
87
+ stub_rummager_has_policies_for_every_type(*args)
196
88
  end
197
89
  end
198
90
  end
@@ -0,0 +1,192 @@
1
+ require 'json'
2
+ require 'gds_api/test_helpers/json_client_helper'
3
+
4
+ module GdsApi
5
+ module TestHelpers
6
+ module Search
7
+ SEARCH_ENDPOINT = Plek.current.find('search')
8
+
9
+ def stub_any_search_post(index: nil)
10
+ if index
11
+ stub_request(:post, %r{#{SEARCH_ENDPOINT}/#{index}/documents})
12
+ .to_return(status: [202, "Accepted"])
13
+ else
14
+ stub_request(:post, %r{#{SEARCH_ENDPOINT}/documents})
15
+ .to_return(status: [202, "Accepted"])
16
+ end
17
+ end
18
+
19
+ def assert_search_posted_item(attributes, index: nil, **options)
20
+ if index
21
+ url = SEARCH_ENDPOINT + "/#{index}/documents"
22
+ else
23
+ url = SEARCH_ENDPOINT + "/documents"
24
+ end
25
+
26
+ assert_requested(:post, url, **options) do |req|
27
+ data = JSON.parse(req.body)
28
+ attributes.to_a.all? do |key, value|
29
+ data[key.to_s] == value
30
+ end
31
+ end
32
+ end
33
+
34
+ def stub_any_search
35
+ stub_request(:get, %r{#{SEARCH_ENDPOINT}/search.json})
36
+ end
37
+
38
+ def stub_any_search_to_return_no_results
39
+ stub_any_search.to_return(body: { results: [] }.to_json)
40
+ end
41
+
42
+ def assert_search(options)
43
+ assert_requested :get, "#{SEARCH_ENDPOINT}/search.json", **options
44
+ end
45
+
46
+ def stub_any_search_delete(index: nil)
47
+ if index
48
+ stub_request(:delete, %r{#{SEARCH_ENDPOINT}/#{index}/documents/.*})
49
+ else
50
+ # use search-api's default index
51
+ stub_request(:delete, %r{#{SEARCH_ENDPOINT}/documents/.*})
52
+ end
53
+ end
54
+
55
+ def stub_any_search_delete_content
56
+ stub_request(:delete, %r{#{SEARCH_ENDPOINT}/content.*})
57
+ end
58
+
59
+ def assert_search_deleted_item(id, index: nil, **options)
60
+ if id =~ %r{^/}
61
+ raise ArgumentError, 'Search id must not start with a slash'
62
+ end
63
+
64
+ if index
65
+ assert_requested(
66
+ :delete,
67
+ %r{#{SEARCH_ENDPOINT}/#{index}/documents/#{id}},
68
+ **options
69
+ )
70
+ else
71
+ assert_requested(
72
+ :delete,
73
+ %r{#{SEARCH_ENDPOINT}/documents/#{id}},
74
+ **options
75
+ )
76
+ end
77
+ end
78
+
79
+ def assert_search_deleted_content(base_path, **options)
80
+ assert_requested(
81
+ :delete,
82
+ %r{#{SEARCH_ENDPOINT}/content.*#{base_path}},
83
+ **options
84
+ )
85
+ end
86
+
87
+ def stub_search_has_services_and_info_data_for_organisation
88
+ stub_request_for(search_results_found)
89
+ run_example_query
90
+ end
91
+
92
+ def stub_search_has_no_services_and_info_data_for_organisation
93
+ stub_request_for(no_search_results_found)
94
+ run_example_query
95
+ end
96
+
97
+ def stub_search_has_specialist_sector_organisations(_sub_sector)
98
+ stub_request_for(sub_sector_organisations_results)
99
+ run_example_query
100
+ end
101
+
102
+ def stub_search_has_no_policies_for_any_type
103
+ stub_request(:get, %r{/search.json})
104
+ .to_return(body: no_search_results_found)
105
+ end
106
+
107
+ def stub_search_has_policies_for_every_type(options = {})
108
+ if options[:count]
109
+ stub_request(:get, %r{/search.json.*count=#{options[:count]}.*})
110
+ .to_return(body: first_n_results(new_policies_results, n: options[:count]))
111
+ else
112
+ stub_request(:get, %r{/search.json})
113
+ .to_return(body: new_policies_results)
114
+ end
115
+ end
116
+
117
+ private
118
+
119
+ def stub_request_for(result_set)
120
+ stub_request(:get, /example.com\/search/).to_return(body: result_set)
121
+ end
122
+
123
+ def run_example_query
124
+ client.search(example_query)
125
+ end
126
+
127
+ def search_results_found
128
+ File.read(
129
+ File.expand_path(
130
+ "../../../test/fixtures/services_and_info_fixture.json",
131
+ __dir__
132
+ )
133
+ )
134
+ end
135
+
136
+ def no_search_results_found
137
+ File.read(
138
+ File.expand_path(
139
+ "../../../test/fixtures/no_services_and_info_data_found_fixture.json",
140
+ __dir__
141
+ )
142
+ )
143
+ end
144
+
145
+ def sub_sector_organisations_results
146
+ File.read(
147
+ File.expand_path(
148
+ "../../../test/fixtures/sub_sector_organisations.json",
149
+ __dir__
150
+ )
151
+ )
152
+ end
153
+
154
+ def new_policies_results
155
+ File.read(
156
+ File.expand_path(
157
+ "../../../test/fixtures/new_policies_for_dwp.json",
158
+ __dir__
159
+ )
160
+ )
161
+ end
162
+
163
+ def old_policies_results
164
+ File.read(
165
+ File.expand_path(
166
+ "../../../test/fixtures/old_policies_for_dwp.json",
167
+ __dir__
168
+ )
169
+ )
170
+ end
171
+
172
+ def first_n_results(results, options)
173
+ n = options[:n]
174
+ results = JSON.parse(results)
175
+ results["results"] = results["results"][0...n]
176
+
177
+ results.to_json
178
+ end
179
+
180
+ def client
181
+ GdsApi::Search.new("http://example.com")
182
+ end
183
+
184
+ def example_query
185
+ {
186
+ filter_organisations: ["an-organisation-slug"],
187
+ facet_specialist_sectors: "1000,examples:4,example_scope:global,order:value.title"
188
+ }
189
+ end
190
+ end
191
+ end
192
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '59.1.0'.freeze
2
+ VERSION = '59.2.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: 59.1.0
4
+ version: 59.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
11
+ date: 2019-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -411,6 +411,7 @@ files:
411
411
  - lib/gds_api/response.rb
412
412
  - lib/gds_api/router.rb
413
413
  - lib/gds_api/rummager.rb
414
+ - lib/gds_api/search.rb
414
415
  - lib/gds_api/support.rb
415
416
  - lib/gds_api/support_api.rb
416
417
  - lib/gds_api/test_helpers/asset_manager.rb
@@ -433,6 +434,7 @@ files:
433
434
  - lib/gds_api/test_helpers/publishing_api_v2.rb
434
435
  - lib/gds_api/test_helpers/router.rb
435
436
  - lib/gds_api/test_helpers/rummager.rb
437
+ - lib/gds_api/test_helpers/search.rb
436
438
  - lib/gds_api/test_helpers/support.rb
437
439
  - lib/gds_api/test_helpers/support_api.rb
438
440
  - lib/gds_api/test_helpers/whitehall_admin_api.rb
@@ -465,7 +467,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
465
467
  - !ruby/object:Gem::Version
466
468
  version: '0'
467
469
  requirements: []
468
- rubygems_version: 3.0.1
470
+ rubygems_version: 3.0.3
469
471
  signing_key:
470
472
  specification_version: 4
471
473
  summary: Adapters to work with GDS APIs