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 27 |
# 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) Oj. = Oj..merge(bigdecimal_load: :float, float_precision: 7) 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/gmaps_geocoding/api.rb', line 77 def finest_latlng(data) result = retrieve_finest_location(data) return [result['ROOFTOP'][:lng], result['ROOFTOP'][:lat]] if result.include?('ROOFTOP'.freeze) return [result['RANGE_INTERPOLATED'][:lng], result['RANGE_INTERPOLATED'][:lat]] if result.include?('RANGE_INTERPOLATED'.freeze) return [result['GEOMETRIC_CENTER'][:lng], result['GEOMETRIC_CENTER'][:lat]] if result.include?('GEOMETRIC_CENTER'.freeze) [result['APPROXIMATE'][:lng], result['APPROXIMATE'][:lat]] rescue [0.0, 0.0].freeze end |
#location ⇒ Object
Return a Ruby Hash object of the Google Maps Geocoding Service response
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gmaps_geocoding/api.rb', line 43 def location fail 'Invalid configuration parameters check the Google Geocoding API documentation'.freeze unless @config.valid? rest_client = retrieve_geocoding_data case @config.json_format? when true json_to_hash(rest_client) else xml_to_hash(rest_client) end rescue => e @logger.error e nil end |