Class: Geocoder::Result::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Takes a hash of data from a parsed geocoding service response.



15
16
17
18
# File 'lib/geocoder/results/base.rb', line 15

def initialize(data)
  @data = data
  @cache_hit = nil
end

Instance Attribute Details

#cache_hitObject

true if result came from cache, false if from request to geocoding service; nil if cache is not configured



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

def cache_hit
  @cache_hit
end

#dataObject

data (hash) fetched from geocoding service



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

def data
  @data
end

Instance Method Details

#address(format = :full) ⇒ Object

A string in the given format.

This default implementation dumbly follows the United States address format and will return incorrect results for most countries. Some APIs return properly formatted addresses and those should be funneled through this method.



28
29
30
31
32
33
34
35
36
37
# File 'lib/geocoder/results/base.rb', line 28

def address(format = :full)
  if state_code.to_s != ""
    s = ", #{state_code}"
  elsif state.to_s != ""
    s = ", #{state}"
  else
    s = ""
  end
  "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, '')
end

#coordinatesObject

A two-element array: [lat, lon].



42
43
44
# File 'lib/geocoder/results/base.rb', line 42

def coordinates
  [@data['latitude'].to_f, @data['longitude'].to_f]
end

#countryObject



70
71
72
# File 'lib/geocoder/results/base.rb', line 70

def country
  fail
end

#country_codeObject



74
75
76
# File 'lib/geocoder/results/base.rb', line 74

def country_code
  fail
end

#latitudeObject



46
47
48
# File 'lib/geocoder/results/base.rb', line 46

def latitude
  coordinates[0]
end

#longitudeObject



50
51
52
# File 'lib/geocoder/results/base.rb', line 50

def longitude
  coordinates[1]
end

#provinceObject



58
59
60
# File 'lib/geocoder/results/base.rb', line 58

def province
  state
end

#province_codeObject



66
67
68
# File 'lib/geocoder/results/base.rb', line 66

def province_code
  state_code
end

#stateObject



54
55
56
# File 'lib/geocoder/results/base.rb', line 54

def state
  fail
end

#state_codeObject



62
63
64
# File 'lib/geocoder/results/base.rb', line 62

def state_code
  fail
end