Class: Geocoder::Result::Opencagedata

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

Instance Attribute Summary

Attributes inherited from Base

#cache_hit, #data

Class Method Summary collapse

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

Class Method Details

.response_attributesObject


87
88
89
90
# File 'lib/geocoder/results/opencagedata.rb', line 87

def self.response_attributes
  %w[boundingbox license 
    formatted stadium]
end

Instance Method Details

#addressObject


17
18
19
# File 'lib/geocoder/results/opencagedata.rb', line 17

def address
  @data['formatted']
end

#cityObject


28
29
30
31
32
33
# File 'lib/geocoder/results/opencagedata.rb', line 28

def city
  %w[city town village hamlet].each do |key|
    return @data['components'][key] if @data['components'].key?(key)
  end
  return nil
end

#coordinatesObject


67
68
69
# File 'lib/geocoder/results/opencagedata.rb', line 67

def coordinates
  [@data['geometry']['lat'].to_f, @data['geometry']['lng'].to_f]
end

#countryObject


55
56
57
# File 'lib/geocoder/results/opencagedata.rb', line 55

def country
  @data['components']['country']
end

#country_codeObject


59
60
61
# File 'lib/geocoder/results/opencagedata.rb', line 59

def country_code
  @data['components']['country_code']
end

#countyObject


51
52
53
# File 'lib/geocoder/results/opencagedata.rb', line 51

def county
  @data['components']['county']
end

#house_numberObject


13
14
15
# File 'lib/geocoder/results/opencagedata.rb', line 13

def house_number
  @data['components']['house_number']
end

#poiObject


6
7
8
9
10
11
# File 'lib/geocoder/results/opencagedata.rb', line 6

def poi
  %w[stadium bus_stop tram_stop].each do |key|
    return @data['components'][key] if @data['components'].key?(key)
  end
  return nil
end

#postal_codeObject


47
48
49
# File 'lib/geocoder/results/opencagedata.rb', line 47

def postal_code
  @data['components']['postcode'].to_s
end

#stateObject


39
40
41
# File 'lib/geocoder/results/opencagedata.rb', line 39

def state
  @data['components']['state']
end

#state_codeObject


43
44
45
# File 'lib/geocoder/results/opencagedata.rb', line 43

def state_code
  @data['components']['state_code']
end

#streetObject


21
22
23
24
25
26
# File 'lib/geocoder/results/opencagedata.rb', line 21

def street
  %w[road pedestrian highway].each do |key|
    return @data['components'][key] if @data['components'].key?(key)
  end
  return nil
end

#suburbObject


63
64
65
# File 'lib/geocoder/results/opencagedata.rb', line 63

def suburb
  @data['components']['suburb']
end

#time_zoneObject


78
79
80
81
82
83
84
85
# File 'lib/geocoder/results/opencagedata.rb', line 78

def time_zone
  # The OpenCage API documentation states that `annotations` is available
  # "when possible" https://geocoder.opencagedata.com/api#annotations
  @data
    .fetch('annotations', {})
    .fetch('timezone', {})
    .fetch('name', nil)
end

#viewportObject


71
72
73
74
75
76
# File 'lib/geocoder/results/opencagedata.rb', line 71

def viewport
  bounds = @data['bounds'] || fail
  south, west = %w(lat lng).map { |i| bounds['southwest'][i] }
  north, east = %w(lat lng).map { |i| bounds['northeast'][i] }
  [south, west, north, east]
end

#villageObject


35
36
37
# File 'lib/geocoder/results/opencagedata.rb', line 35

def village
  @data['components']['village']
end