Class: Geocoder::Result::Geoip2

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

Instance Attribute Summary

Attributes inherited from Base

#cache_hit, #data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #province, #province_code

Constructor Details

This class inherits a constructor from Geocoder::Result::Base

Class Method Details

.response_attributesObject



53
54
55
# File 'lib/geocoder/results/geoip2.rb', line 53

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



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

def city
  return '' unless @data['city']
  @data['city']['names']['en']
end

#coordinatesObject



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

def coordinates
  [latitude, longitude]
end

#countryObject



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

def country
  @data['country']['names']['en']
end

#country_codeObject



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

def country_code
  @data['country']['iso_code']
end

#latitudeObject



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

def latitude
  return 0.0 unless @data['location']
  @data['location']['latitude'].to_f
end

#longitudeObject



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

def longitude
  return 0.0 unless @data['location']
  @data['location']['longitude'].to_f
end

#postal_codeObject



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

def postal_code
  return '' unless @data['postal']
  @data['postal']['code']
end

#stateObject



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

def state
  return '' unless @data['subdivisions']
  @data['subdivisions'][0]['names']['en']
end

#state_codeObject



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

def state_code
  return '' unless @data['subdivisions']
  @data['subdivisions'][0]['iso_code']
end