Class: GdsApi::Rummager
Defined Under Namespace
Classes: UnknownAPIVersion, V1, V2
Constant Summary collapse
- DEFAULT_API_VERSION =
'V1'.freeze
- API_VERSIONS =
{ 'V1' => GdsApi::Rummager::V1, 'V2' => GdsApi::Rummager::V2, }.freeze
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#add_document(*args) ⇒ GdsApi::Response
Add a document to the search index.
-
#advanced_search(args) ⇒ Object
deprecated
Deprecated.
Only in use by Whitehall. Use the ‘#search` method.
- #base_url ⇒ Object
-
#delete_content(base_path) ⇒ Object
Delete a content-document from the index by base path.
- #delete_content! ⇒ Object
-
#delete_document(*args) ⇒ Object
Delete a non-content document from the search index.
- #documents_url ⇒ Object
-
#get_content(base_path) ⇒ Object
Retrieve a content-document from the index.
- #get_content! ⇒ Object
-
#initialize(endpoint_url, options = {}) ⇒ Rummager
constructor
A new instance of Rummager.
-
#search(args, additional_headers = {}) ⇒ Object
Perform a search.
-
#search_enum(args, page_size: 100, additional_headers: {}) ⇒ Object
Perform a search, returning the results as an enumerator.
Methods inherited from Base
#client, #create_client, #get_list, #url_for_slug
Constructor Details
#initialize(endpoint_url, options = {}) ⇒ Rummager
Returns a new instance of Rummager.
54 55 56 57 58 59 60 61 |
# File 'lib/gds_api/rummager.rb', line 54 def initialize(endpoint_url, = {}) super # The API version provides a simple wrapper around this base class so that we # can still access the shared methods present in this class. version = .fetch(:api_version, DEFAULT_API_VERSION) api_class = API_VERSIONS[version] || raise(UnknownAPIVersion) @api = api_class.new(self) end |
Instance Method Details
#add_document(*args) ⇒ GdsApi::Response
Add a document to the search index.
116 117 118 |
# File 'lib/gds_api/rummager.rb', line 116 def add_document(*args) @api.add_document(*args) end |
#advanced_search(args) ⇒ Object
Only in use by Whitehall. Use the ‘#search` method.
Advanced search.
100 101 102 103 104 |
# File 'lib/gds_api/rummager.rb', line 100 def advanced_search(args) raise ArgumentError.new("Args cannot be blank") if args.nil? || args.empty? request_path = "#{base_url}/advanced_search?#{Rack::Utils.build_nested_query(args)}" get_json(request_path) end |
#base_url ⇒ Object
168 169 170 |
# File 'lib/gds_api/rummager.rb', line 168 def base_url endpoint end |
#delete_content(base_path) ⇒ Object
Delete a content-document from the index by base path.
Content documents are pages on GOV.UK that have a base path and are returned in searches. This excludes best bets, recommended-links, and contacts, which may be deleted with ‘delete_document`.
128 129 130 131 |
# File 'lib/gds_api/rummager.rb', line 128 def delete_content(base_path) request_url = "#{base_url}/content?link=#{base_path}" delete_json(request_url) end |
#delete_content! ⇒ Object
134 135 136 |
# File 'lib/gds_api/rummager.rb', line 134 def delete_content!(*) raise "`Rummager#delete_content!` is deprecated. Use `Rummager#delete_content`" end |
#delete_document(*args) ⇒ Object
Delete a non-content document from the search index.
For example, best bets, recommended links, or contacts.
164 165 166 |
# File 'lib/gds_api/rummager.rb', line 164 def delete_document(*args) @api.delete_document(*args) end |
#documents_url ⇒ Object
172 173 174 |
# File 'lib/gds_api/rummager.rb', line 172 def documents_url "#{base_url}/documents" end |
#get_content(base_path) ⇒ Object
Retrieve a content-document from the index.
Content documents are pages on GOV.UK that have a base path and are returned in searches. This excludes best bets, recommended-links, and contacts.
146 147 148 149 |
# File 'lib/gds_api/rummager.rb', line 146 def get_content(base_path) request_url = "#{base_url}/content?link=#{base_path}" get_json(request_url) end |
#get_content! ⇒ Object
152 153 154 |
# File 'lib/gds_api/rummager.rb', line 152 def get_content!(*) raise "`Rummager#get_content!` is deprecated. Use `Rummager#get_content`" end |
#search(args, additional_headers = {}) ⇒ Object
Perform a search.
68 69 70 71 |
# File 'lib/gds_api/rummager.rb', line 68 def search(args, additional_headers = {}) request_url = "#{base_url}/search.json?#{Rack::Utils.build_nested_query(args)}" get_json(request_url, additional_headers) end |
#search_enum(args, page_size: 100, additional_headers: {}) ⇒ Object
Perform a search, returning the results as an enumerator.
The enumerator abstracts away rummager’s pagination and fetches new pages when necessary.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/gds_api/rummager.rb', line 82 def search_enum(args, page_size: 100, additional_headers: {}) Enumerator.new do |yielder| (0..Float::INFINITY).step(page_size).each do |index| search_params = args.merge(start: index.to_i, count: page_size) results = search(search_params, additional_headers).to_h.fetch('results', []) results.each do |result| yielder << result end if results.count < page_size break end end end end |