Module: Google::Geocoding

Defined in:
lib/google/geocoding.rb,
lib/google/geocoding/version.rb

Defined Under Namespace

Classes: Address, Configuration, Geometry, LatLng, RequestError, Result

Constant Summary collapse

BASE_URL =
'https://maps.googleapis.com/maps/api/geocode/json'
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.configObject



22
23
24
# File 'lib/google/geocoding.rb', line 22

def config
  @config ||= Configuration.new(Faraday.new(url: BASE_URL))
end

.configure {|config| ... } ⇒ Object

Yields:



26
27
28
# File 'lib/google/geocoding.rb', line 26

def configure(&block)
  yield(config) if block_given?
end

.lookup(arg) ⇒ Object

TODO needs better exception handling for Faraday using a custom middleware



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/google/geocoding.rb', line 31

def lookup(arg)
  unless config.api_key
    raise ArgumentError, 'lookup method requires :api_key (Make sure if you have configured that)'
  end

  request_url =
    case arg
    when String
      "?address=#{URI.escape(arg)}&key=#{config.api_key}"
    when LatLng
      "?latlng=#{arg.latitude},#{arg.longitude}&key=#{config.api_key}"
    else
      raise ArgumentError, 'lookup method only accepts either String or LatLng'
    end

  parse_response config.client.get(request_url)
end