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 DC'
chez_barack.formatted_address
  => "1600 Pennsylvania Avenue Northwest, President's Park,
      Washington, DC 20500, USA"

Defined Under Namespace

Classes: GeocodingError

Constant Summary collapse

VERSION =

This gem's version.

'0.7.10'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ GoogleMapsGeocoder

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

Examples:

chez_barack = GoogleMapsGeocoder.new '1600 Pennsylvania DC'

Parameters:

  • address (String)

    a geocodable address

Raises:

  • (RuntimeError)

73
74
75
76
77
78
79
80
81
82
83
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 73

def initialize(address)
  @json = address.is_a?(String) ? google_maps_response(address) : address
  status = @json && @json['status']
  raise RuntimeError if status == 'OVER_QUERY_LIMIT'
  raise GeocodingError, @json if !@json || @json.empty? || status != 'OK'

  set_attributes_from_json
  Logger.new($stderr).info('GoogleMapsGeocoder') do
    "Geocoded \"#{address}\" => \"#{formatted_address}\""
  end
end

Instance Attribute Details

#formatted_addressString (readonly) Also known as: address

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


38
39
40
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 38

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 Northwest"

Returns:

  • (String)

    the formatted street address


46
47
48
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 46

def formatted_street_address
  @formatted_street_address
end

Instance Method Details

#coordinatesObject

Returns the address' coordinates as an array of floats.


86
87
88
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 86

def coordinates
  [lat, lng]
end

#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


96
97
98
# File 'lib/google_maps_geocoder/google_maps_geocoder.rb', line 96

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 DC').partial_match?
  => true

Returns:

  • (boolean)

    whether the Google Maps result is a partial match


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

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