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



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

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
20
21
# File 'lib/geocoder/results/geoip2.rb', line 17

def city
  fetch_name(
    data.fetch('city', {}).fetch('names', {})
  )
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



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

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

#country_codeObject



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

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

#languageObject



61
62
63
# File 'lib/geocoder/results/geoip2.rb', line 61

def language
  @language ||= default_language
end

#language=(l) ⇒ Object



57
58
59
# File 'lib/geocoder/results/geoip2.rb', line 57

def language=(l)
  @language = l.to_s
end

#postal_codeObject



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

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

#stateObject



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

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

#state_codeObject



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

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