Class: Geocoder::Result::Maxmind

Inherits:
Base
  • Object
show all
Defined in:
lib/geocoder/results/maxmind.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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/geocoder/results/maxmind.rb', line 119

def method_missing(method, *args, &block)
  if field_names.include?(method)
    data_hash[method]
  else
    super
  end
end

Class Method Details

.field_namesObject

Hash mapping service names to names of returned fields.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/geocoder/results/maxmind.rb', line 9

def self.field_names
  {
    :country => [
      :country_code,
      :error
    ],

    :city => [
      :country_code,
      :region_code,
      :city_name,
      :latitude,
      :longitude,
      :error
    ],

    :city_isp_org => [
      :country_code,
      :region_code,
      :city_name,
      :postal_code,
      :latitude,
      :longitude,
      :metro_code,
      :area_code,
      :isp_name,
      :organization_name,
      :error
    ],

    :omni => [
      :country_code,
      :country_name,
      :region_code,
      :region_name,
      :city_name,
      :latitude,
      :longitude,
      :metro_code,
      :area_code,
      :time_zone,
      :continent_code,
      :postal_code,
      :isp_name,
      :organization_name,
      :domain,
      :as_number,
      :netspeed,
      :user_type,
      :accuracy_radius,
      :country_confidence_factor,
      :city_confidence_factor,
      :region_confidence_factor,
      :postal_confidence_factor,
      :error
    ]
  }
end

Instance Method Details

#address(format = :full) ⇒ Object



90
91
92
93
# File 'lib/geocoder/results/maxmind.rb', line 90

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

#cityObject



95
96
97
# File 'lib/geocoder/results/maxmind.rb', line 95

def city
  data_hash[:city_name]
end

#coordinatesObject



86
87
88
# File 'lib/geocoder/results/maxmind.rb', line 86

def coordinates
  [data_hash[:latitude].to_f, data_hash[:longitude].to_f]
end

#countryObject

not given by MaxMind



107
108
109
# File 'lib/geocoder/results/maxmind.rb', line 107

def country #not given by MaxMind
  data_hash[:country_name] || data_hash[:country_code]
end

#country_codeObject



111
112
113
# File 'lib/geocoder/results/maxmind.rb', line 111

def country_code
  data_hash[:country_code]
end

#data_hashObject



82
83
84
# File 'lib/geocoder/results/maxmind.rb', line 82

def data_hash
  @data_hash ||= Hash[*field_names.zip(@data).flatten]
end

#field_namesObject



78
79
80
# File 'lib/geocoder/results/maxmind.rb', line 78

def field_names
  self.class.field_names[service_name]
end

#postal_codeObject



115
116
117
# File 'lib/geocoder/results/maxmind.rb', line 115

def postal_code
  data_hash[:postal_code]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
# File 'lib/geocoder/results/maxmind.rb', line 127

def respond_to?(method)
  if field_names.include?(method)
    true
  else
    super
  end
end

#service_nameObject

Name of the MaxMind service being used.



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

def service_name
  # it would be much better to infer this from the length of the @data
  # array, but MaxMind seems to send inconsistent and wide-ranging response
  # lengths (see https://github.com/alexreisner/geocoder/issues/396)
  Geocoder.config.maxmind[:service]
end

#stateObject

not given by MaxMind



99
100
101
# File 'lib/geocoder/results/maxmind.rb', line 99

def state # not given by MaxMind
  data_hash[:region_name] || data_hash[:region_code]
end

#state_codeObject



103
104
105
# File 'lib/geocoder/results/maxmind.rb', line 103

def state_code
  data_hash[:region_code]
end