Class: GoogleMapsGeocoder
- Inherits:
-
Object
- Object
- GoogleMapsGeocoder
- Defined in:
- lib/google_maps_geocoder.rb
Overview
A simple PORO wrapper for geocoding with Google Maps.
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: GeocodingError
Instance Attribute Summary collapse
-
#formatted_address ⇒ String
(also: #address)
readonly
Returns the complete formatted address with standardized abbreviations.
-
#formatted_street_address ⇒ String
readonly
Returns the formatted street address with standardized abbreviations.
Instance Method Summary collapse
-
#coordinates ⇒ Object
Returns the address’ coordinates as an array of floats.
-
#exact_match? ⇒ boolean
Returns true if the address Google returns is an exact match.
-
#initialize(address, logger: Logger.new($stderr)) ⇒ GoogleMapsGeocoder
constructor
Geocodes the specified address and wraps the results in a GoogleMapsGeocoder object.
-
#partial_match? ⇒ boolean
Returns true if the address Google returns isn’t an exact match.
Constructor Details
#initialize(address, logger: Logger.new($stderr)) ⇒ GoogleMapsGeocoder
Geocodes the specified address and wraps the results in a GoogleMapsGeocoder object.
74 75 76 77 78 79 80 81 82 |
# File 'lib/google_maps_geocoder.rb', line 74 def initialize(address, logger: Logger.new($stderr)) @json = address.is_a?(String) ? google_maps_response(address) : address status = @json && @json['status'] raise RuntimeError if status == 'OVER_QUERY_LIMIT' raise GeocodingError.new(@json, logger:) if !@json || @json.empty? || status != 'OK' set_attributes_from_json logger.info('GoogleMapsGeocoder') { "Geocoded \"#{address}\" => \"#{formatted_address}\"" } end |
Instance Attribute Details
#formatted_address ⇒ String (readonly) Also known as: address
Returns the complete formatted address with standardized abbreviations.
38 39 40 |
# File 'lib/google_maps_geocoder.rb', line 38 def formatted_address @formatted_address end |
#formatted_street_address ⇒ String (readonly)
Returns the formatted street address with standardized abbreviations.
46 47 48 |
# File 'lib/google_maps_geocoder.rb', line 46 def formatted_street_address @formatted_street_address end |
Instance Method Details
#coordinates ⇒ Object
Returns the address’ coordinates as an array of floats.
85 86 87 |
# File 'lib/google_maps_geocoder.rb', line 85 def coordinates [lat, lng] end |
#exact_match? ⇒ boolean
Returns true if the address Google returns is an exact match.
95 96 97 |
# File 'lib/google_maps_geocoder.rb', line 95 def exact_match? !partial_match? end |
#partial_match? ⇒ boolean
Returns true if the address Google returns isn’t an exact match.
105 106 107 |
# File 'lib/google_maps_geocoder.rb', line 105 def partial_match? @json['results'][0]['partial_match'] == true end |