Class: Geocoder::Result::Bag42

Inherits:
Google
  • Object
show all
Defined in:
lib/bag42_geocoder/results/bag42.rb

Instance Method Summary collapse

Instance Method Details

#address(format = :full) ⇒ Object



21
22
23
# File 'lib/bag42_geocoder/results/bag42.rb', line 21

def address(format = :full)
  formatted_address
end

#address_componentsObject



81
82
83
# File 'lib/bag42_geocoder/results/bag42.rb', line 81

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


99
100
101
# File 'lib/bag42_geocoder/results/bag42.rb', line 99

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

#cityObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bag42_geocoder/results/bag42.rb', line 25

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

#company_componentsObject



85
86
87
# File 'lib/bag42_geocoder/results/bag42.rb', line 85

def company_components
  @data['companny_components'] || @data['company_components'] # NOTE: The typo is on purpose! We fallback on non-type to be compatible when they fix this!
end

#company_components_of_type(type) ⇒ Object



103
104
105
106
# File 'lib/bag42_geocoder/results/bag42.rb', line 103

def company_components_of_type(type)
  return [] if company_components.nil?
  company_components.select{ |c| c['types'].include?(type.to_s) }
end

#coordinatesObject

Nothing to do here..



17
18
19
# File 'lib/bag42_geocoder/results/bag42.rb', line 17

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

#countryObject



49
50
51
52
53
# File 'lib/bag42_geocoder/results/bag42.rb', line 49

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

#country_codeObject



55
56
57
58
59
# File 'lib/bag42_geocoder/results/bag42.rb', line 55

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

#formatted_addressObject



77
78
79
# File 'lib/bag42_geocoder/results/bag42.rb', line 77

def formatted_address
  @data['formatted_address']
end

#geometryObject



108
109
110
# File 'lib/bag42_geocoder/results/bag42.rb', line 108

def geometry
  @data['geometry']
end

#postal_codeObject



61
62
63
64
65
# File 'lib/bag42_geocoder/results/bag42.rb', line 61

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

#precisionObject



112
113
114
# File 'lib/bag42_geocoder/results/bag42.rb', line 112

def precision
  geometry['location_type'] if geometry
end

#registered_nameObject



67
68
69
70
71
# File 'lib/bag42_geocoder/results/bag42.rb', line 67

def registered_name
  if registered_name = company_components_of_type(:registered_name).first
    registered_name['long_name']
  end
end

#stateObject



37
38
39
40
41
# File 'lib/bag42_geocoder/results/bag42.rb', line 37

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

#state_codeObject



43
44
45
46
47
# File 'lib/bag42_geocoder/results/bag42.rb', line 43

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

#typesObject



73
74
75
# File 'lib/bag42_geocoder/results/bag42.rb', line 73

def types
  @data['types']
end