Class: Geocoder::Result::Google

Inherits:
Base
  • Object
show all
Defined in:
lib/geocoder/results/google.rb

Direct Known Subclasses

GooglePremier

Instance Attribute Summary

Attributes inherited from Base

#cache_hit, #data

Instance Method Summary collapse

Methods inherited from Base

#initialize, #latitude, #longitude, #province, #province_code

Constructor Details

This class inherits a constructor from Geocoder::Result::Base

Instance Method Details

#address(format = :full) ⇒ Object



10
11
12
# File 'lib/geocoder/results/google.rb', line 10

def address(format = :full)
  formatted_address
end

#address_componentsObject



64
65
66
# File 'lib/geocoder/results/google.rb', line 64

def address_components
  @data['address_components']
end

#address_components_of_type(type) ⇒ Object

Get address components of a given type. Valid types are defined in Google’s Geocoding API documentation and include (among others):

:street_number
:locality
:neighborhood
:route
:postal_code


78
79
80
# File 'lib/geocoder/results/google.rb', line 78

def address_components_of_type(type)
  address_components.select{ |c| c['types'].include?(type.to_s) }
end

#cityObject



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/geocoder/results/google.rb', line 14

def city
  fields = [:locality, :sublocality,
    :administrative_area_level_3,
    :administrative_area_level_2]
  fields.each do |f|
    if entity = address_components_of_type(f).first
      return entity['long_name']
    end
  end
  return nil # no appropriate components found
end

#coordinatesObject



6
7
8
# File 'lib/geocoder/results/google.rb', line 6

def coordinates
  ['lat', 'lng'].map{ |i| geometry['location'][i] }
end

#countryObject



38
39
40
41
42
# File 'lib/geocoder/results/google.rb', line 38

def country
  if country = address_components_of_type(:country).first
    country['long_name']
  end
end

#country_codeObject



44
45
46
47
48
# File 'lib/geocoder/results/google.rb', line 44

def country_code
  if country = address_components_of_type(:country).first
    country['short_name']
  end
end

#formatted_addressObject



60
61
62
# File 'lib/geocoder/results/google.rb', line 60

def formatted_address
  @data['formatted_address']
end

#geometryObject



82
83
84
# File 'lib/geocoder/results/google.rb', line 82

def geometry
  @data['geometry']
end

#postal_codeObject



50
51
52
53
54
# File 'lib/geocoder/results/google.rb', line 50

def postal_code
  if postal = address_components_of_type(:postal_code).first
    postal['long_name']
  end
end

#precisionObject



86
87
88
# File 'lib/geocoder/results/google.rb', line 86

def precision
  geometry['location_type'] if geometry
end

#stateObject



26
27
28
29
30
# File 'lib/geocoder/results/google.rb', line 26

def state
  if state = address_components_of_type(:administrative_area_level_1).first
    state['long_name']
  end
end

#state_codeObject



32
33
34
35
36
# File 'lib/geocoder/results/google.rb', line 32

def state_code
  if state = address_components_of_type(:administrative_area_level_1).first
    state['short_name']
  end
end

#typesObject



56
57
58
# File 'lib/geocoder/results/google.rb', line 56

def types
  @data['types']
end