File: lookup.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (155 lines) | stat: -rw-r--r-- 3,638 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require "geocoder/lookups/test"

module Geocoder
  module Lookup
    extend self

    ##
    # Array of valid Lookup service names.
    #
    def all_services
      street_services + ip_services
    end

    ##
    # Array of valid Lookup service names, excluding :test.
    #
    def all_services_except_test
      all_services - [:test]
    end

    ##
    # Array of valid Lookup service names, excluding any that do not build their own HTTP requests.
    # For example, Amazon Location Service uses the AWS gem, not HTTP REST requests, to fetch data.
    #
    def all_services_with_http_requests
      all_services_except_test - [:amazon_location_service, :maxmind_local, :geoip2, :ip2location_lite]
    end

    ##
    # All street address lookup services, default first.
    #
    def street_services
      @street_services ||= [
        :location_iq,
        :azure,
        :esri,
        :google,
        :google_premier,
        :google_places_details,
        :google_places_search,
        :bing,
        :geocoder_ca,
        :yandex,
        :nationaal_georegister_nl,
        :nominatim,
        :mapbox,
        :mapquest,
        :uk_ordnance_survey_names,
        :opencagedata,
        :pelias,
        :pdok_nl,
        :pickpoint,
        :here,
        :baidu,
        :tencent,
        :geocodio,
        :smarty_streets,
        :postcode_anywhere_uk,
        :postcodes_io,
        :geoportail_lu,
        :ban_data_gouv_fr,
        :test,
        :latlon,
        :amap,
        :osmnames,
        :melissa_street,
        :amazon_location_service,
        :geoapify,
        :photon,
        :twogis,
        :pc_miler
      ]
    end

    ##
    # All IP address lookup services, default first.
    #
    def ip_services
      @ip_services ||= [
        :baidu_ip,
        :abstract_api,
        :freegeoip,
        :geoip2,
        :maxmind,
        :maxmind_local,
        :telize,
        :pointpin,
        :maxmind_geoip2,
        :ipinfo_io,
        :ipinfo_io_lite,
        :ipregistry,
        :ipapi_com,
        :ipdata_co,
        :db_ip_com,
        :ipstack,
        :ip2location,
        :ipgeolocation,
        :ipqualityscore,
        :ipbase,
        :ip2location_io,
        :ip2location_lite
      ]
    end

    attr_writer :street_services, :ip_services

    ##
    # Retrieve a Lookup object from the store.
    # Use this instead of Geocoder::Lookup::X.new to get an
    # already-configured Lookup object.
    #
    def get(name)
      @services = {} unless defined?(@services)
      @services[name] = spawn(name) unless @services.include?(name)
      @services[name]
    end


    private # -----------------------------------------------------------------

    ##
    # Spawn a Lookup of the given name.
    #
    def spawn(name)
      if all_services.include?(name)
        name = name.to_s
        instantiate_lookup(name)
      else
        valids = all_services.map(&:inspect).join(", ")
        raise ConfigurationError, "Please specify a valid lookup for Geocoder " +
          "(#{name.inspect} is not one of: #{valids})."
      end
    end

    ##
    # Convert an "underscore" version of a name into a "class" version.
    #
    def classify_name(filename)
      filename.to_s.split("_").map{ |i| i[0...1].upcase + i[1..-1] }.join
    end

    ##
    # Safely instantiate Lookup
    #
    def instantiate_lookup(name)
      class_name = classify_name(name)
      begin
        Geocoder::Lookup.const_get(class_name, inherit=false)
      rescue NameError
        require "geocoder/lookups/#{name}"
      end
      Geocoder::Lookup.const_get(class_name).new
    end
  end
end