Class: GoogleMapsJuice::Geocoding

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/google_maps_juice/geocoding.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

ENDPOINT =
'/geocode'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

#initialize

Constructor Details

This class inherits a constructor from GoogleMapsJuice::Endpoint

Class Method Details

.geocode(params, api_key: GoogleMapsJuice.config.api_key) ⇒ Object



9
10
11
12
# File 'lib/google_maps_juice/geocoding.rb', line 9

def geocode(params, api_key: GoogleMapsJuice.config.api_key)
  client = GoogleMapsJuice::Client.new(api_key: api_key)
  self.new(client).geocode(params)
end

.i_geocode(params, sleep_before_retry: 0, api_key: GoogleMapsJuice.config.api_key) ⇒ Object



14
15
16
17
# File 'lib/google_maps_juice/geocoding.rb', line 14

def i_geocode(params, sleep_before_retry: 0, api_key: GoogleMapsJuice.config.api_key)
  client = GoogleMapsJuice::Client.new(api_key: api_key)
  self.new(client).i_geocode(params, sleep_before_retry: sleep_before_retry)
end

Instance Method Details

#build_components_param(params, keys: []) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/google_maps_juice/geocoding.rb', line 90

def build_components_param(params, keys: [])
  keys.map do |key|
    if params[key].present?
      "#{key}:#{params[key]}"
    end
  end.compact.join('|')
end

#build_request_params(i_geocode_params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/google_maps_juice/geocoding.rb', line 71

def build_request_params(i_geocode_params)
  req_params = Hash.new

  [:address, :language].each do |key|
    if i_geocode_params[key].present?
      req_params[key] = i_geocode_params[key]
    end
  end

  components = build_components_param(i_geocode_params, keys:
    [:locality, :postal_code, :administrative_area, :country]
  )
  if components.present?
    req_params[:components] = components
  end

  req_params
end

#geocode(params) ⇒ Object



21
22
23
24
25
26
# File 'lib/google_maps_juice/geocoding.rb', line 21

def geocode(params)
  validate_geocode_params(params)
  response_text = @client.get("#{ENDPOINT}/json", params)
  response = JSON.parse(response_text, object_class: Response)
  detect_errors(response)
end

#i_geocode(params, sleep_before_retry: 0) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/google_maps_juice/geocoding.rb', line 28

def i_geocode(params, sleep_before_retry: 0)
  validate_i_geocode_params(params)
  response = nil
  removable_keys = [:administrative_area, :locality, :address, :postal_code]
  begin
    request_params = build_request_params(params)
    response = geocode(request_params)
  rescue ZeroResults => e
    deleted_param = nil
    while removable_keys.present? && deleted_param.nil?
      key = removable_keys.pop
      deleted_param = params.delete(key)
    end
    if deleted_param.present?
      sleep sleep_before_retry
      retry
    else
      raise e
    end
  end
  response
end

#validate_geocode_params(params) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
# File 'lib/google_maps_juice/geocoding.rb', line 51

def validate_geocode_params(params)
  raise ArgumentError, 'Hash argument expected' unless params.is_a?(Hash)

  supported_keys = %w( address components bounds language region )
  validate_supported_params(params, supported_keys)

  required_keys = %w( address components )
  validate_any_required_params(params, required_keys)
end

#validate_i_geocode_params(params) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
69
# File 'lib/google_maps_juice/geocoding.rb', line 61

def validate_i_geocode_params(params)
  raise ArgumentError, 'Hash argument expected' unless params.is_a?(Hash)

  supported_keys = %w( address locality postal_code administrative_area country language)
  validate_supported_params(params, supported_keys)

  required_keys = %w( address country )
  validate_any_required_params(params, required_keys)
end