File: maxmind.rb

package info (click to toggle)
ruby-geocoder 1.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 732 kB
  • sloc: ruby: 6,173; makefile: 3
file content (130 lines) | stat: -rw-r--r-- 2,700 bytes parent folder | download
1
2
3
4
5
6
7
8
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'geocoder/results/base'

module Geocoder::Result
  class Maxmind < Base

    ##
    # Hash mapping service names to names of returned fields.
    #
    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

    ##
    # Name of the MaxMind service being used.
    #
    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

    def field_names
      self.class.field_names[service_name]
    end

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

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

    def city
      data_hash[:city_name]
    end

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

    def state_code
      data_hash[:region_code]
    end

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

    def country_code
      data_hash[:country_code]
    end

    def postal_code
      data_hash[:postal_code]
    end

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

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