Class: Geocoder::Result::Geoip2

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

Direct Known Subclasses

MaxmindGeoip2

Instance Attribute Summary

Attributes inherited from Base

#cache_hit

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



41
42
43
# File 'lib/geocoder/results/geoip2.rb', line 41

def self.response_attributes
  %w[ip]
end

Instance Method Details

#address(format = :full) ⇒ Object



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

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

#cityObject



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

def city
  data.fetch('city', {}).fetch('names', {}).fetch(locale, '')
end

#coordinatesObject



11
12
13
14
15
# File 'lib/geocoder/results/geoip2.rb', line 11

def coordinates
  %w[latitude longitude].map do |l|
    data.fetch('location', {}).fetch(l, 0.0)
  end
end

#countryObject



29
30
31
# File 'lib/geocoder/results/geoip2.rb', line 29

def country
  data.fetch('country', {}).fetch('names', {}).fetch(locale, '')
end

#country_codeObject



33
34
35
# File 'lib/geocoder/results/geoip2.rb', line 33

def country_code
  data.fetch('country', {}).fetch('iso_code', '')
end

#postal_codeObject



37
38
39
# File 'lib/geocoder/results/geoip2.rb', line 37

def postal_code
  data.fetch('postal', {}).fetch('code', '')
end

#stateObject



21
22
23
# File 'lib/geocoder/results/geoip2.rb', line 21

def state
  data.fetch('subdivisions', []).fetch(0, {}).fetch('names', {}).fetch(locale, '')
end

#state_codeObject



25
26
27
# File 'lib/geocoder/results/geoip2.rb', line 25

def state_code
  data.fetch('subdivisions', []).fetch(0, {}).fetch('iso_code', '')
end