Class: HasHelpers::SearchAPI

Inherits:
RackAPI
  • Object
show all
Defined in:
app/api/has_helpers/search_api.rb

Constant Summary collapse

OPTION_PARAM_KEYS =
%w[with]
DEFAULT_QUERY_OPTIONS =
{ page: 1, per_page: 15 }.freeze
INVALID_QUERY_MSG =
"Missing or invalid query"

Constants inherited from RackAPI

RackAPI::DEFAULT_HEADERS, RackAPI::INTERNAL_ERROR_MSG, RackAPI::NOT_ACCEPTABLE_MSG, RackAPI::UNAUTHORIZED_MSG

Class Attribute Summary collapse

Attributes inherited from RackAPI

#request

Instance Method Summary collapse

Methods inherited from RackAPI

call, #initialize, #run

Constructor Details

This class inherits a constructor from HasHelpers::RackAPI

Class Attribute Details

.options_hookObject

Returns the value of attribute options_hook.



11
12
13
# File 'app/api/has_helpers/search_api.rb', line 11

def options_hook
  @options_hook
end

Instance Method Details

#handle_requestObject



14
15
16
17
18
19
20
21
# File 'app/api/has_helpers/search_api.rb', line 14

def handle_request
  validate_query!(raw_query)
  resources.each { |r| validate_resource!(r) } # Validate each resource, if one fails it will throw an error.
  api_response(
    status: 200,
    body: search # Run the search and return the result
  )
end

#searchObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/api/has_helpers/search_api.rb', line 23

def search
  search_results = provider.search(
    resource: resources,
    query: raw_query,
    options: query_options
  )

  # Show type when searching multiple resources OR any of the resources uses STI
  show_type = resources.size > 1 || resource_klasses.any? { |k| k.descendants.any? }
  search_presenter = ::HasHelpers::SearchPresenter.new(search_results[:results], show_type: show_type)
  search_results.merge!(results: search_presenter.results(format_ungrouped: true))
end