Class: ConsolidatedScreeningList::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/consolidated_screening_list/query.rb

Constant Summary collapse

PARAMETERS =
{
  q: nil,
  sources: Source.keys,
  countries: Query.countries,
  address: nil,
  name: nil,
  fuzzy_name: false,
  type: nil,
  size: 100,
  offset: 0,
}
TYPES =
%w[
  Individual
  Entity
  Vessel
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, api_key) ⇒ Query

Returns a new instance of Query.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/consolidated_screening_list/query.rb', line 31

def initialize(params, api_key)
  params = {q: params} if params.is_a? String
  @params = PARAMETERS.merge(params)
  @api_key = api_key
  invalid_parameter = @params.find { |key, value| !PARAMETERS.key?(key) }
  invalid_source = @params[:sources].find { |source| !Source.find_by_key(source) }
  invalid_country = @params[:countries].find { |country| !Query.countries.include?(country) }
  invalid_api_key = !UUID.validate(api_key)
  raise ArgumentError, "Invalid parameter: #{invalid[0]}" if invalid_parameter
  raise ArgumentError, "Invalid source: #{invalid}" if invalid_source
  raise ArgumentError, "Invalid country: #{invalid}" if invalid_country
  raise ArgumentError, "Invalid API key: #{invalid}" if invalid_api_key
end

Class Method Details

.countriesObject



4
5
6
# File 'lib/consolidated_screening_list/query.rb', line 4

def countries
  @countries ||= IsoCountryCodes.all.map { |c| c.alpha2 }
end

.endpointObject



8
9
10
# File 'lib/consolidated_screening_list/query.rb', line 8

def endpoint
  @endpoint ||= URI.join(ConsolidatedScreeningList::API_BASE, "search").to_s
end

Instance Method Details

#callObject



45
46
47
48
49
50
51
# File 'lib/consolidated_screening_list/query.rb', line 45

def call
  RestClient.get Query.endpoint, {
    :params => params,
    "Authorization" => "Bearer #{@api_key}",
    "User-Agent" => ConsolidatedScreeningList.user_agent,
  }
end