Class: GmapsGeocoding::Api
- Inherits:
-
Object
- Object
- GmapsGeocoding::Api
- Defined in:
- lib/gmaps_geocoding/api.rb
Overview
Google Maps Geocoding Service abstraction class
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#finest_latlng(data) ⇒ Array
Get the best latlng for an address based on Google Maps Geocoder “location_type”.
-
#initialize(opts = {}) ⇒ Api
constructor
A new instance of Api.
-
#location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response.
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
#config ⇒ Object (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
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 |
#location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response
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 |