Class: SearchTypeahead::Service

Inherits:
Common::Client::Base show all
Includes:
Common::Client::Concerns::Monitoring
Defined in:
lib/search_typeahead/service.rb

Overview

This class builds a wrapper around Search.gov web suggestioms API.

Constant Summary collapse

STATSD_KEY_PREFIX =
'api.search_typeahead'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::Client::Concerns::Monitoring

#increment, #increment_failure, #increment_total, #with_monitoring

Methods inherited from Common::Client::Base

#config, configuration, #connection, #delete, #get, #perform, #post, #put, #raise_backend_exception, #raise_not_authenticated, #request, #sanitize_headers!, #service_name

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initialize(query) ⇒ Service

Returns a new instance of Service.



22
23
24
# File 'lib/search_typeahead/service.rb', line 22

def initialize(query)
  @query = query || ''
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



20
21
22
# File 'lib/search_typeahead/service.rb', line 20

def query
  @query
end

Instance Method Details

#api_keyObject (private)



61
62
63
# File 'lib/search_typeahead/service.rb', line 61

def api_key
  Settings.search_typeahead.api_key
end

#nameObject (private)



57
58
59
# File 'lib/search_typeahead/service.rb', line 57

def name
  Settings.search_typeahead.name
end

#query_paramsObject (private)

Required params [name, access_key, query]



49
50
51
52
53
54
55
# File 'lib/search_typeahead/service.rb', line 49

def query_params
  {
    name:,
    q: query,
    api_key:
  }
end

#suggestionsObject

GETs suggestion data from search.gov



28
29
30
31
32
33
34
35
36
37
# File 'lib/search_typeahead/service.rb', line 28

def suggestions
  with_monitoring do
    Faraday.get(suggestions_url, query_params) do |req|
      req.options.timeout = 2
      req.options.open_timeout = 2
    end
  end
rescue => e
  e
end

#suggestions_urlObject (private)



41
42
43
# File 'lib/search_typeahead/service.rb', line 41

def suggestions_url
  config.base_path
end