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



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

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



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

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

#cityObject



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

def city
  data_hash[:city_name]
end

#coordinatesObject



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

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

#countryObject

not given by MaxMind



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

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

#country_codeObject



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

def country_code
  data_hash[:country_code]
end

#data_hashObject



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

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

#field_namesObject



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

def field_names
  self.class.field_names[service_name]
end

#postal_codeObject



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

def postal_code
  data_hash[:postal_code]
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#service_nameObject

Name of the MaxMind service being used. Inferred from format of @data.



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

def service_name
  self.class.field_names.to_a.each do |n,f|
    return n if f.size == @data.size
  end
  nil
end

#stateObject

not given by MaxMind



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

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

#state_codeObject



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

def state_code
  data_hash[:region_code]
end