Module: Geocodio::Utils

Included in:
Client
Defined in:
lib/geocodio/utils.rb

Instance Method Summary collapse

Instance Method Details

#normalize_coordinates(coordinates) ⇒ Object


23
24
25
26
# File 'lib/geocodio/utils.rb', line 23

def normalize_coordinates(coordinates)
  return coordinates unless coordinates.is_a?(Hash)
  coordinates.sort.map { |p| p[1] }.join(',')
end

#normalize_params_and_options(hash) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/geocodio/utils.rb', line 28

def normalize_params_and_options(hash)
  hash = Hash[hash.map { |k, v| [k.to_sym, v] }]

  # The only supported parameter is fields
  params = hash.select { |k, _| [:fields].include?(k) }

  # Normalize this particular parameter to be a comma-separated string
  params[:fields] = params[:fields].join(',') if params[:fields]

  # The only supported option is `timeout`
  options = hash.select { |k, _| [:timeout].include?(k) }

  [params, options]
end

#parse_nested_results(response) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/geocodio/utils.rb', line 9

def parse_nested_results(response)
  results = response.body['results']

  results = results.map do |result_set|
    addresses = Array(result_set['response']['results'])
    addresses.map! { |result| Address.new(result) }

    query = result_set['query']
    input = result_set['response']['input']['formatted_address'] if result_set['response']['input']

    AddressSet.new(query, *addresses, input: input)
  end
end

#parse_results(response) ⇒ Object


3
4
5
6
7
# File 'lib/geocodio/utils.rb', line 3

def parse_results(response)
  results   = response.body['results']
  input     = response.body['input']['formatted_address'] if response.body['input']
  [results.map { |result| Address.new(result) }, input]
end