Class: Geocoder::Result::Okf

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

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/okf.rb', line 10

def address(format = :full)
  formatted_address
end

#address_componentsObject



80
81
82
# File 'lib/geocoder/results/okf.rb', line 80

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


94
95
96
# File 'lib/geocoder/results/okf.rb', line 94

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/okf.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/okf.rb', line 6

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

#countryObject



38
39
40
41
42
# File 'lib/geocoder/results/okf.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/okf.rb', line 44

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

#formatted_addressObject



76
77
78
# File 'lib/geocoder/results/okf.rb', line 76

def formatted_address
  @data['formatted_address']
end

#geometryObject



98
99
100
# File 'lib/geocoder/results/okf.rb', line 98

def geometry
  @data['geometry']
end

#postal_codeObject



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

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

#precisionObject



102
103
104
# File 'lib/geocoder/results/okf.rb', line 102

def precision
  geometry['location_type'] if geometry
end

#routeObject



56
57
58
59
60
# File 'lib/geocoder/results/okf.rb', line 56

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

#stateObject



26
27
28
# File 'lib/geocoder/results/okf.rb', line 26

def state
  ""
end

#state_codeObject



34
35
36
# File 'lib/geocoder/results/okf.rb', line 34

def state_code
  ""
end

#street_addressObject



68
69
70
# File 'lib/geocoder/results/okf.rb', line 68

def street_address
  [route, street_number].compact.join(' ')
end

#street_numberObject



62
63
64
65
66
# File 'lib/geocoder/results/okf.rb', line 62

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

#sub_stateObject



30
31
32
# File 'lib/geocoder/results/okf.rb', line 30

def sub_state
  ""
end

#typesObject



72
73
74
# File 'lib/geocoder/results/okf.rb', line 72

def types
  @data['types']
end