Class: GoogleMapsGeocoder

Inherits:
Object
  • Object
show all
Defined in:
lib/google_maps_geocoder/version.rb,
lib/google_maps_geocoder/google_maps_geocoder.rb

Overview

A simple PORO wrapper for geocoding with Google Maps.

rubocop:disable Metrics/ClassLength

Examples:

chez_barack = GoogleMapsGeocoder.new '1600 Pennsylvania Ave'
chez_barack.formatted_address
  => "1600 Pennsylvania Avenue Northwest, President's Park,
      Washington, DC 20500, USA"

Defined Under Namespace

Classes: GeocodingError, InvalidRequestError, QueryLimitError, RequestDeniedError, UnknownError, ZeroResultsError

Constant Summary collapse

VERSION =
'0.5.0'.freeze
ERROR_STATUSES =
{ zero_results: 'ZERO_RESULTS',
  query_limit: 'OVER_QUERY_LIMIT',
  request_denied: 'REQUEST_DENIED',
  invalid_request: 'INVALID_REQUEST',
  unknown: 'UNKNOWN_ERROR'
}.freeze
GOOGLE_ADDRESS_SEGMENTS =
i(
  city country_long_name country_short_name county lat lng postal_code
  state_long_name state_short_name
).freeze
GOOGLE_API_URI =
'https://maps.googleapis.com/maps/api/geocode/json'.freeze
ALL_ADDRESS_SEGMENTS =
(
  GOOGLE_ADDRESS_SEGMENTS + i(
    formatted_address formatted_street_address
  )
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ GoogleMapsGeocoder

Geocodes the specified address and wraps the results in a GoogleMapsGeocoder object.

Examples:

chez_barack = GoogleMapsGeocoder.new '1600 Pennsylvania Ave'

Parameters:

  • data (String)

    a geocodable address



80
81
82
83
84
85
86
87
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 80

def initialize(data)
  @json = data.is_a?(String) ? json_from_url(data) : data
  handle_error if @json.blank? || @json['status'] != 'OK'
  set_attributes_from_json
  logger.info('GoogleMapsGeocoder') do
    "Geocoded \"#{data}\" => \"#{formatted_address}\""
  end
end

Instance Attribute Details

#formatted_addressString (readonly)

Returns the complete formatted address with standardized abbreviations.

Examples:

chez_barack.formatted_address
  => "1600 Pennsylvania Avenue Northwest, President's Park,
      Washington, DC 20500, USA"

Returns:

  • (String)

    the complete formatted address



60
61
62
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 60

def formatted_address
  @formatted_address
end

#formatted_street_addressString (readonly)

Returns the formatted street address with standardized abbreviations.

Examples:

chez_barack.formatted_street_address
  => "1600 Pennsylvania Avenue"

Returns:

  • (String)

    the formatted street address



68
69
70
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 68

def formatted_street_address
  @formatted_street_address
end

Class Method Details

.error_class_name(key) ⇒ Object



109
110
111
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 109

def self.error_class_name(key)
  "google_maps_geocoder/#{key}_error".classify.constantize
end

Instance Method Details

#exact_match?boolean

Returns true if the address Google returns is an exact match.

Examples:

chez_barack.exact_match?
  => true

Returns:

  • (boolean)

    whether the Google Maps result is an exact match



95
96
97
# File 'lib/google_maps_geocoder/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.

Examples:

GoogleMapsGeocoder.new('1600 Pennsylvania Washington').partial_match?
  => true

Returns:

  • (boolean)

    whether the Google Maps result is a partial match



105
106
107
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 105

def partial_match?
  @json['results'][0]['partial_match'] == true
end