Class: ReverseGeocode

Inherits:
Object
  • Object
show all
Defined in:
lib/reverse_geocode.rb

Overview

ReverseGeocode provides an simple API for accessing reverse geocode data for a given geocode using the google maps geocoding API.

Examples:

ReverseGeocode.new(30.0098974, -81.3876544).city # => "St Augustine"

Defined Under Namespace

Classes: GeocodeError

Constant Summary collapse

GOOGLE_URI =
"http://maps.google.com/maps/geo"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, long) ⇒ ReverseGeocode

Creates a new reverse geocode object.

Raises:

  • (ArgumentError)


34
35
36
37
# File 'lib/reverse_geocode.rb', line 34

def initialize(lat, long)
  raise ArgumentError, "Latitude and longitude required" unless lat && long
  @lat, @long = lat, long
end

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



31
32
33
# File 'lib/reverse_geocode.rb', line 31

def api_key
  @api_key
end

Instance Attribute Details

#latObject (readonly)

Returns the value of attribute lat.



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

def lat
  @lat
end

#longObject (readonly)

Returns the value of attribute long.



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

def long
  @long
end

Instance Method Details

#addressObject



45
46
47
# File 'lib/reverse_geocode.rb', line 45

def address
  @address ||= first_placemark/'address'
end

#cityObject



61
62
63
# File 'lib/reverse_geocode.rb', line 61

def city
  @city ||= (placemark_by_accuracy(4)/'AddressDetails'/'Country'/'AdministrativeArea'/'AddressLine').first
end

#countyObject



65
66
67
# File 'lib/reverse_geocode.rb', line 65

def county
  @county ||= (placemark_by_accuracy(3)/'AddressDetails'/'Country'/'AdministrativeArea'/'AddressLine').first
end

#responseObject

The raw response hash from the reverse geocode request



41
42
43
# File 'lib/reverse_geocode.rb', line 41

def response
  @response ||= handle_response
end

#stateObject



53
54
55
# File 'lib/reverse_geocode.rb', line 53

def state
  @state ||= first_administrative_area/'AdministrativeAreaName'
end

#streetObject



49
50
51
# File 'lib/reverse_geocode.rb', line 49

def street
  @street ||= first_administrative_area/'Thoroughfare'/'ThoroughfareName'
end

#zipObject



57
58
59
# File 'lib/reverse_geocode.rb', line 57

def zip
  @zip ||= first_administrative_area/'PostalCode'/'PostalCodeNumber'
end