Class: GmapsGeocoding::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/gmaps_geocoding/api.rb

Overview

Google Maps Geocoding Service abstraction class

Examples:

opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Api

Returns a new instance of Api.



17
18
19
20
21
22
23
24
25
26
# File 'lib/gmaps_geocoding/api.rb', line 17

def initialize(opts = {})
  @logger = opts.delete(:logger)
  unless @logger
    @logger = Logger.new(STDERR) do |l|
      l.progname = 'gmaps_geocoding'.freeze
      l.level = $DEBUG ? Logger::DEBUG : Logger::INFO
    end
  end
  @config = GmapsGeocoding::Config.new(opts)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/gmaps_geocoding/api.rb', line 15

def config
  @config
end

Instance Method Details

#finest_latlng(data) ⇒ Array

Get the best latlng for an address based on Google Maps Geocoder “location_type”

location_type stores additional data about the specified location. The following values are currently supported:

google.maps.GeocoderLocationType.ROOFTOP            indicates that the returned result reflects a precise geocode.
google.maps.GeocoderLocationType.RANGE_INTERPOLATED indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
google.maps.GeocoderLocationType.GEOMETRIC_CENTER   indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).
google.maps.GeocoderLocationType.APPROXIMATE        indicates that the returned result is approximate.

rubocop:disable Metrics/AbcSize

Examples:

# json output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)
data = api.location
if data.include?('status') && data['status'].eql?('OK') # or more simple : if data.include?('results')
  return finest_latlng(data['results']) # output : [2.291018, 48.857269]
end

Parameters:

  • data (Array)

    The json#results or xml#result array from #location method

Returns:

  • (Array)

    array contains latitude and longitude of the location



85
86
87
88
89
90
91
92
93
# File 'lib/gmaps_geocoding/api.rb', line 85

def finest_latlng(data)
  result = retrieve_finest_location(data)
  return [result['ROOFTOP'][:lng], result['ROOFTOP'][:lat]] if result.include?('ROOFTOP')
  return [result['RANGE_INTERPOLATED'][:lng], result['RANGE_INTERPOLATED'][:lat]] if result.include?('RANGE_INTERPOLATED')
  return [result['GEOMETRIC_CENTER'][:lng], result['GEOMETRIC_CENTER'][:lat]] if result.include?('GEOMETRIC_CENTER')
  [result['APPROXIMATE'][:lng], result['APPROXIMATE'][:lat]]
rescue
  [0.0, 0.0]
end

#locationObject

Return a Ruby Hash object of the Google Maps Geocoding Service response

/ Google Maps Geocoding Service documentation.

Examples:

# json output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'json'}
api = GmapsGeocoding::Api.new(opts)
result = api.location
# xml output example
opts = {address: 'Tour Eiffel, Paris, IDF, France', output: 'xml'}
api = GmapsGeocoding::Api.new(opts)
result = api.location


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gmaps_geocoding/api.rb', line 42

def location
  begin
    if @config.valid?
      rest_client = retrieve_geocoding_data
      result = case @config.json_format?
               when true
                 Yajl::Parser.parse(rest_client)
               else
                 r = Nori.new.parse(rest_client)
                 if r.include?('GeocodeResponse')
                   r['GeocodeResponse']
                 else
                   { status: 'UNKNOWN_ERROR' }
                 end
               end
      return result
    end
  rescue => e
    @logger.error e
  end
  nil
end