class Geocoder::Result::Google

Public Instance Methods

address(format = :full) click to toggle source
# File lib/geocoder/results/google.rb, line 10
def address(format = :full)
  formatted_address
end
address_components() click to toggle source
# File lib/geocoder/results/google.rb, line 98
def address_components
  @data['address_components']
end
address_components_of_type(type) click to toggle source

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
# File lib/geocoder/results/google.rb, line 112
def address_components_of_type(type)
  address_components.select{ |c| c['types'].include?(type.to_s) }
end
bounds() click to toggle source
# File lib/geocoder/results/google.rb, line 137
def bounds
  bounding_box_from geometry['bounds']
end
city() click to toggle source
# File lib/geocoder/results/google.rb, line 20
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
coordinates() click to toggle source
# File lib/geocoder/results/google.rb, line 6
def coordinates
  ['lat', 'lng'].map{ |i| geometry['location'][i] }
end
country() click to toggle source
# File lib/geocoder/results/google.rb, line 56
def country
  if country = address_components_of_type(:country).first
    country['long_name']
  end
end
country_code() click to toggle source
# File lib/geocoder/results/google.rb, line 62
def country_code
  if country = address_components_of_type(:country).first
    country['short_name']
  end
end
formatted_address() click to toggle source
# File lib/geocoder/results/google.rb, line 94
def formatted_address
  @data['formatted_address']
end
geometry() click to toggle source
# File lib/geocoder/results/google.rb, line 116
def geometry
  @data['geometry']
end
neighborhood() click to toggle source
# File lib/geocoder/results/google.rb, line 14
def neighborhood
  if neighborhood = address_components_of_type(:neighborhood).first
    neighborhood['long_name']
  end
end
partial_match() click to toggle source
# File lib/geocoder/results/google.rb, line 124
def partial_match
  @data['partial_match']
end
place_id() click to toggle source
# File lib/geocoder/results/google.rb, line 128
def place_id
  @data['place_id']
end
postal_code() click to toggle source
# File lib/geocoder/results/google.rb, line 68
def postal_code
  if postal = address_components_of_type(:postal_code).first
    postal['long_name']
  end
end
precision() click to toggle source
# File lib/geocoder/results/google.rb, line 120
def precision
  geometry['location_type'] if geometry
end
route() click to toggle source
# File lib/geocoder/results/google.rb, line 74
def route
  if route = address_components_of_type(:route).first
    route['long_name']
  end
end
state() click to toggle source
# File lib/geocoder/results/google.rb, line 32
def state
  if state = address_components_of_type(:administrative_area_level_1).first
    state['long_name']
  end
end
state_code() click to toggle source
# File lib/geocoder/results/google.rb, line 38
def state_code
  if state = address_components_of_type(:administrative_area_level_1).first
    state['short_name']
  end
end
street_address() click to toggle source
# File lib/geocoder/results/google.rb, line 86
def street_address
  [street_number, route].compact.join(' ')
end
street_number() click to toggle source
# File lib/geocoder/results/google.rb, line 80
def street_number
  if street_number = address_components_of_type(:street_number).first
    street_number['long_name']
  end
end
sub_state() click to toggle source
# File lib/geocoder/results/google.rb, line 44
def sub_state
  if state = address_components_of_type(:administrative_area_level_2).first
    state['long_name']
  end
end
sub_state_code() click to toggle source
# File lib/geocoder/results/google.rb, line 50
def sub_state_code
  if state = address_components_of_type(:administrative_area_level_2).first
    state['short_name']
  end
end
types() click to toggle source
# File lib/geocoder/results/google.rb, line 90
def types
  @data['types']
end
viewport() click to toggle source
# File lib/geocoder/results/google.rb, line 132
def viewport
  viewport = geometry['viewport'] || fail
  bounding_box_from viewport
end

Private Instance Methods

bounding_box_from(box) click to toggle source
# File lib/geocoder/results/google.rb, line 143
def bounding_box_from(box)
  return nil unless box
  south, west = %w(lat lng).map { |c| box['southwest'][c] }
  north, east = %w(lat lng).map { |c| box['northeast'][c] }
  [south, west, north, east]
end