geocoder 1.5.1 → 1.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +65 -0
  3. data/LICENSE +1 -1
  4. data/README.md +333 -238
  5. data/bin/console +13 -0
  6. data/lib/easting_northing.rb +171 -0
  7. data/lib/generators/geocoder/config/templates/initializer.rb +7 -1
  8. data/lib/geocoder/cache.rb +16 -33
  9. data/lib/geocoder/cache_stores/base.rb +40 -0
  10. data/lib/geocoder/cache_stores/generic.rb +35 -0
  11. data/lib/geocoder/cache_stores/redis.rb +34 -0
  12. data/lib/geocoder/configuration.rb +11 -4
  13. data/lib/geocoder/configuration_hash.rb +4 -4
  14. data/lib/geocoder/ip_address.rb +8 -1
  15. data/lib/geocoder/lookup.rb +21 -3
  16. data/lib/geocoder/lookups/abstract_api.rb +46 -0
  17. data/lib/geocoder/lookups/amazon_location_service.rb +54 -0
  18. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +14 -1
  19. data/lib/geocoder/lookups/base.rb +10 -2
  20. data/lib/geocoder/lookups/bing.rb +1 -1
  21. data/lib/geocoder/lookups/esri.rb +6 -0
  22. data/lib/geocoder/lookups/freegeoip.rb +4 -4
  23. data/lib/geocoder/lookups/geoapify.rb +72 -0
  24. data/lib/geocoder/lookups/geocodio.rb +1 -1
  25. data/lib/geocoder/lookups/geoip2.rb +4 -0
  26. data/lib/geocoder/lookups/google.rb +7 -2
  27. data/lib/geocoder/lookups/google_places_details.rb +8 -14
  28. data/lib/geocoder/lookups/google_places_search.rb +28 -2
  29. data/lib/geocoder/lookups/google_premier.rb +4 -0
  30. data/lib/geocoder/lookups/here.rb +7 -16
  31. data/lib/geocoder/lookups/ip2location.rb +10 -14
  32. data/lib/geocoder/lookups/ipdata_co.rb +1 -1
  33. data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  34. data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
  35. data/lib/geocoder/lookups/ipregistry.rb +68 -0
  36. data/lib/geocoder/lookups/latlon.rb +1 -2
  37. data/lib/geocoder/lookups/maxmind_local.rb +7 -1
  38. data/lib/geocoder/lookups/melissa_street.rb +41 -0
  39. data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  40. data/lib/geocoder/lookups/osmnames.rb +57 -0
  41. data/lib/geocoder/lookups/photon.rb +89 -0
  42. data/lib/geocoder/lookups/pickpoint.rb +1 -1
  43. data/lib/geocoder/lookups/smarty_streets.rb +6 -1
  44. data/lib/geocoder/lookups/telize.rb +1 -1
  45. data/lib/geocoder/lookups/tencent.rb +9 -9
  46. data/lib/geocoder/lookups/test.rb +4 -0
  47. data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  48. data/lib/geocoder/lookups/yandex.rb +3 -4
  49. data/lib/geocoder/results/abstract_api.rb +146 -0
  50. data/lib/geocoder/results/amazon_location_service.rb +57 -0
  51. data/lib/geocoder/results/baidu.rb +0 -4
  52. data/lib/geocoder/results/ban_data_gouv_fr.rb +27 -2
  53. data/lib/geocoder/results/db_ip_com.rb +1 -1
  54. data/lib/geocoder/results/esri.rb +5 -2
  55. data/lib/geocoder/results/geoapify.rb +179 -0
  56. data/lib/geocoder/results/here.rb +4 -1
  57. data/lib/geocoder/results/ipgeolocation.rb +59 -0
  58. data/lib/geocoder/results/ipqualityscore.rb +54 -0
  59. data/lib/geocoder/results/ipregistry.rb +304 -0
  60. data/lib/geocoder/results/mapbox.rb +10 -4
  61. data/lib/geocoder/results/melissa_street.rb +46 -0
  62. data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  63. data/lib/geocoder/results/nominatim.rb +27 -15
  64. data/lib/geocoder/results/osmnames.rb +56 -0
  65. data/lib/geocoder/results/photon.rb +119 -0
  66. data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  67. data/lib/geocoder/results/yandex.rb +217 -59
  68. data/lib/geocoder/sql.rb +4 -4
  69. data/lib/geocoder/util.rb +29 -0
  70. data/lib/geocoder/version.rb +1 -1
  71. data/lib/maxmind_database.rb +3 -3
  72. metadata +35 -18
  73. data/examples/autoexpire_cache_dalli.rb +0 -62
  74. data/examples/autoexpire_cache_redis.rb +0 -28
  75. data/lib/geocoder/lookups/geocoder_us.rb +0 -51
  76. data/lib/geocoder/results/geocoder_us.rb +0 -39
  77. data/lib/hash_recursive_merge.rb +0 -74
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'geocoder/results/base'
4
+
5
+ module Geocoder
6
+ module Result
7
+ # https://apidocs.geoapify.com/docs/geocoding/api
8
+ class Geoapify < Base
9
+ def address(_format = :full)
10
+ properties['formatted']
11
+ end
12
+
13
+ def address_line1
14
+ properties['address_line1']
15
+ end
16
+
17
+ def address_line2
18
+ properties['address_line2']
19
+ end
20
+
21
+ def house_number
22
+ properties['housenumber']
23
+ end
24
+
25
+ def street
26
+ properties['street']
27
+ end
28
+
29
+ def postal_code
30
+ properties['postcode']
31
+ end
32
+
33
+ def district
34
+ properties['district']
35
+ end
36
+
37
+ def suburb
38
+ properties['suburb']
39
+ end
40
+
41
+ def city
42
+ properties['city']
43
+ end
44
+
45
+ def county
46
+ properties['county']
47
+ end
48
+
49
+ def state
50
+ properties['state']
51
+ end
52
+
53
+ # Not currently available in the API
54
+ def state_code
55
+ ''
56
+ end
57
+
58
+ def country
59
+ properties['country']
60
+ end
61
+
62
+ def country_code
63
+ return unless properties['country_code']
64
+
65
+ properties['country_code'].upcase
66
+ end
67
+
68
+ def coordinates
69
+ return unless properties['lat']
70
+ return unless properties['lon']
71
+
72
+ [properties['lat'], properties['lon']]
73
+ end
74
+
75
+ # See: https://tools.ietf.org/html/rfc7946#section-3.1
76
+ #
77
+ # Each feature has a "Point" type in the Geoapify API.
78
+ def geometry
79
+ return unless data['geometry']
80
+
81
+ symbol_hash data['geometry']
82
+ end
83
+
84
+ # See: https://tools.ietf.org/html/rfc7946#section-5
85
+ def bounds
86
+ data['bbox']
87
+ end
88
+
89
+ # Type of the result, one of:
90
+ #
91
+ # * :unknown
92
+ # * :amenity
93
+ # * :building
94
+ # * :street
95
+ # * :suburb
96
+ # * :district
97
+ # * :postcode
98
+ # * :city
99
+ # * :county
100
+ # * :state
101
+ # * :country
102
+ #
103
+ def type
104
+ return :unknown unless properties['result_type']
105
+
106
+ properties['result_type'].to_sym
107
+ end
108
+
109
+ # Distance in meters to given bias:proximity or to given coordinates for
110
+ # reverse geocoding
111
+ def distance
112
+ properties['distance']
113
+ end
114
+
115
+ # Calculated rank for the result, containing the following keys:
116
+ #
117
+ # * `popularity` - The popularity score of the result
118
+ # * `confidence` - The confidence value of the result (0-1)
119
+ # * `match_type` - The result's match type, one of following:
120
+ # * full_match
121
+ # * inner_part
122
+ # * match_by_building
123
+ # * match_by_street
124
+ # * match_by_postcode
125
+ # * match_by_city_or_disrict
126
+ # * match_by_country_or_state
127
+ #
128
+ # Example:
129
+ # {
130
+ # popularity: 8.615793062435909,
131
+ # confidence: 0.88,
132
+ # match_type: :full_match
133
+ # }
134
+ def rank
135
+ return unless properties['rank']
136
+
137
+ r = symbol_hash(properties['rank'])
138
+ r[:match_type] = r[:match_type].to_sym if r[:match_type]
139
+ r
140
+ end
141
+
142
+ # Examples:
143
+ #
144
+ # Open
145
+ # {
146
+ # sourcename: 'openstreetmap',
147
+ # wheelchair: 'limited',
148
+ # wikidata: 'Q186125',
149
+ # wikipedia: 'en:Madison Square Garden',
150
+ # website: 'http://www.thegarden.com/',
151
+ # phone: '12124656741',
152
+ # osm_type: 'W',
153
+ # osm_id: 138141251,
154
+ # continent: 'North America',
155
+ # }
156
+ def datasource
157
+ return unless properties['datasource']
158
+
159
+ symbol_hash properties['datasource']
160
+ end
161
+
162
+ private
163
+
164
+ def properties
165
+ @properties ||= data['properties'] || {}
166
+ end
167
+
168
+ def symbol_hash(orig_hash)
169
+ {}.tap do |result|
170
+ orig_hash.each_key do |key|
171
+ next unless orig_hash[key]
172
+
173
+ result[key.to_sym] = orig_hash[key]
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -27,7 +27,10 @@ module Geocoder::Result
27
27
  end
28
28
 
29
29
  def state
30
- address_data['County']
30
+ fail unless d = address_data['AdditionalData']
31
+ if v = d.find{|ad| ad['key']=='StateName'}
32
+ return v['value']
33
+ end
31
34
  end
32
35
 
33
36
  def province
@@ -0,0 +1,59 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Ipgeolocation < Base
5
+
6
+ def coordinates
7
+ [@data['latitude'].to_f, @data['longitude'].to_f]
8
+ end
9
+
10
+ def address(format = :full)
11
+ "#{city}, #{state} #{postal_code}, #{country_name}".sub(/^[ ,]*/, "")
12
+ end
13
+
14
+ def state
15
+ @data['state_prov']
16
+ end
17
+
18
+ def state_code
19
+ @data['state_prov']
20
+ end
21
+
22
+ def country
23
+ @data['country_name']
24
+ end
25
+
26
+ def country_code
27
+ @data['country_code2']
28
+ end
29
+
30
+ def postal_code
31
+ @data['zipcode']
32
+ end
33
+
34
+ def self.response_attributes
35
+ [
36
+ ['ip', ''],
37
+ ['hostname', ''],
38
+ ['continent_code', ''],
39
+ ['continent_name', ''],
40
+ ['country_code2', ''],
41
+ ['country_code3', ''],
42
+ ['country_name', ''],
43
+ ['country_capital',''],
44
+ ['district',''],
45
+ ['state_prov',''],
46
+ ['city', ''],
47
+ ['zipcode', ''],
48
+ ['time_zone', {}],
49
+ ['currency', {}]
50
+ ]
51
+ end
52
+
53
+ response_attributes.each do |attr, default|
54
+ define_method attr do
55
+ @data[attr] || default
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,54 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder
4
+ module Result
5
+ class Ipqualityscore < Base
6
+
7
+ def self.key_method_mappings
8
+ {
9
+ 'request_id' => :request_id,
10
+ 'success' => :success?,
11
+ 'message' => :message,
12
+ 'city' => :city,
13
+ 'region' => :state,
14
+ 'country_code' => :country_code,
15
+ 'mobile' => :mobile?,
16
+ 'fraud_score' => :fraud_score,
17
+ 'ISP' => :isp,
18
+ 'ASN' => :asn,
19
+ 'organization' => :organization,
20
+ 'is_crawler' => :crawler?,
21
+ 'host' => :host,
22
+ 'proxy' => :proxy?,
23
+ 'vpn' => :vpn?,
24
+ 'tor' => :tor?,
25
+ 'active_vpn' => :active_vpn?,
26
+ 'active_tor' => :active_tor?,
27
+ 'recent_abuse' => :recent_abuse?,
28
+ 'bot_status' => :bot?,
29
+ 'connection_type' => :connection_type,
30
+ 'abuse_velocity' => :abuse_velocity,
31
+ 'timezone' => :timezone,
32
+ }
33
+ end
34
+
35
+ key_method_mappings.each_pair do |key, meth|
36
+ define_method meth do
37
+ @data[key]
38
+ end
39
+ end
40
+
41
+ alias_method :state_code, :state
42
+ alias_method :country, :country_code
43
+
44
+ def postal_code
45
+ '' # No suitable fallback
46
+ end
47
+
48
+ def address
49
+ [city, state, country_code].compact.reject(&:empty?).join(', ')
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,304 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Ipregistry < Base
5
+
6
+ def initialize(data)
7
+ super
8
+
9
+ @data = flatten_hash(data)
10
+ end
11
+
12
+ def coordinates
13
+ [@data['location_latitude'], @data['location_longitude']]
14
+ end
15
+
16
+ def flatten_hash(hash)
17
+ hash.each_with_object({}) do |(k, v), h|
18
+ if v.is_a? Hash
19
+ flatten_hash(v).map do |h_k, h_v|
20
+ h["#{k}_#{h_k}".to_s] = h_v
21
+ end
22
+ else
23
+ h[k] = v
24
+ end
25
+ end
26
+ end
27
+
28
+ private :flatten_hash
29
+
30
+ def city
31
+ @data['location_city']
32
+ end
33
+
34
+ def country
35
+ @data['location_country_name']
36
+ end
37
+
38
+ def country_code
39
+ @data['location_country_code']
40
+ end
41
+
42
+ def postal_code
43
+ @data['location_postal']
44
+ end
45
+
46
+ def state
47
+ @data['location_region_name']
48
+ end
49
+
50
+ def state_code
51
+ @data['location_region_code']
52
+ end
53
+
54
+ # methods for fields specific to Ipregistry
55
+
56
+ def ip
57
+ @data["ip"]
58
+ end
59
+
60
+ def type
61
+ @data["type"]
62
+ end
63
+
64
+ def hostname
65
+ @data["hostname"]
66
+ end
67
+
68
+ def carrier_name
69
+ @data["carrier_name"]
70
+ end
71
+
72
+ def carrier_mcc
73
+ @data["carrier_mcc"]
74
+ end
75
+
76
+ def carrier_mnc
77
+ @data["carrier_mnc"]
78
+ end
79
+
80
+ def connection_asn
81
+ @data["connection_asn"]
82
+ end
83
+
84
+ def connection_domain
85
+ @data["connection_domain"]
86
+ end
87
+
88
+ def connection_organization
89
+ @data["connection_organization"]
90
+ end
91
+
92
+ def connection_type
93
+ @data["connection_type"]
94
+ end
95
+
96
+ def currency_code
97
+ @data["currency_code"]
98
+ end
99
+
100
+ def currency_name
101
+ @data["currency_name"]
102
+ end
103
+
104
+ def currency_plural
105
+ @data["currency_plural"]
106
+ end
107
+
108
+ def currency_symbol
109
+ @data["currency_symbol"]
110
+ end
111
+
112
+ def currency_symbol_native
113
+ @data["currency_symbol_native"]
114
+ end
115
+
116
+ def currency_format_negative_prefix
117
+ @data["currency_format_negative_prefix"]
118
+ end
119
+
120
+ def currency_format_negative_suffix
121
+ @data["currency_format_negative_suffix"]
122
+ end
123
+
124
+ def currency_format_positive_prefix
125
+ @data["currency_format_positive_prefix"]
126
+ end
127
+
128
+ def currency_format_positive_suffix
129
+ @data["currency_format_positive_suffix"]
130
+ end
131
+
132
+ def location_continent_code
133
+ @data["location_continent_code"]
134
+ end
135
+
136
+ def location_continent_name
137
+ @data["location_continent_name"]
138
+ end
139
+
140
+ def location_country_area
141
+ @data["location_country_area"]
142
+ end
143
+
144
+ def location_country_borders
145
+ @data["location_country_borders"]
146
+ end
147
+
148
+ def location_country_calling_code
149
+ @data["location_country_calling_code"]
150
+ end
151
+
152
+ def location_country_capital
153
+ @data["location_country_capital"]
154
+ end
155
+
156
+ def location_country_code
157
+ @data["location_country_code"]
158
+ end
159
+
160
+ def location_country_name
161
+ @data["location_country_name"]
162
+ end
163
+
164
+ def location_country_population
165
+ @data["location_country_population"]
166
+ end
167
+
168
+ def location_country_population_density
169
+ @data["location_country_population_density"]
170
+ end
171
+
172
+ def location_country_flag_emoji
173
+ @data["location_country_flag_emoji"]
174
+ end
175
+
176
+ def location_country_flag_emoji_unicode
177
+ @data["location_country_flag_emoji_unicode"]
178
+ end
179
+
180
+ def location_country_flag_emojitwo
181
+ @data["location_country_flag_emojitwo"]
182
+ end
183
+
184
+ def location_country_flag_noto
185
+ @data["location_country_flag_noto"]
186
+ end
187
+
188
+ def location_country_flag_twemoji
189
+ @data["location_country_flag_twemoji"]
190
+ end
191
+
192
+ def location_country_flag_wikimedia
193
+ @data["location_country_flag_wikimedia"]
194
+ end
195
+
196
+ def location_country_languages
197
+ @data["location_country_languages"]
198
+ end
199
+
200
+ def location_country_tld
201
+ @data["location_country_tld"]
202
+ end
203
+
204
+ def location_region_code
205
+ @data["location_region_code"]
206
+ end
207
+
208
+ def location_region_name
209
+ @data["location_region_name"]
210
+ end
211
+
212
+ def location_city
213
+ @data["location_city"]
214
+ end
215
+
216
+ def location_postal
217
+ @data["location_postal"]
218
+ end
219
+
220
+ def location_latitude
221
+ @data["location_latitude"]
222
+ end
223
+
224
+ def location_longitude
225
+ @data["location_longitude"]
226
+ end
227
+
228
+ def location_language_code
229
+ @data["location_language_code"]
230
+ end
231
+
232
+ def location_language_name
233
+ @data["location_language_name"]
234
+ end
235
+
236
+ def location_language_native
237
+ @data["location_language_native"]
238
+ end
239
+
240
+ def location_in_eu
241
+ @data["location_in_eu"]
242
+ end
243
+
244
+ def security_is_bogon
245
+ @data["security_is_bogon"]
246
+ end
247
+
248
+ def security_is_cloud_provider
249
+ @data["security_is_cloud_provider"]
250
+ end
251
+
252
+ def security_is_tor
253
+ @data["security_is_tor"]
254
+ end
255
+
256
+ def security_is_tor_exit
257
+ @data["security_is_tor_exit"]
258
+ end
259
+
260
+ def security_is_proxy
261
+ @data["security_is_proxy"]
262
+ end
263
+
264
+ def security_is_anonymous
265
+ @data["security_is_anonymous"]
266
+ end
267
+
268
+ def security_is_abuser
269
+ @data["security_is_abuser"]
270
+ end
271
+
272
+ def security_is_attacker
273
+ @data["security_is_attacker"]
274
+ end
275
+
276
+ def security_is_threat
277
+ @data["security_is_threat"]
278
+ end
279
+
280
+ def time_zone_id
281
+ @data["time_zone_id"]
282
+ end
283
+
284
+ def time_zone_abbreviation
285
+ @data["time_zone_abbreviation"]
286
+ end
287
+
288
+ def time_zone_current_time
289
+ @data["time_zone_current_time"]
290
+ end
291
+
292
+ def time_zone_name
293
+ @data["time_zone_name"]
294
+ end
295
+
296
+ def time_zone_offset
297
+ @data["time_zone_offset"]
298
+ end
299
+
300
+ def time_zone_in_daylight_saving
301
+ @data["time_zone_in_daylight_saving"]
302
+ end
303
+ end
304
+ end
@@ -23,7 +23,10 @@ module Geocoder::Result
23
23
  context_part('region')
24
24
  end
25
25
 
26
- alias_method :state_code, :state
26
+ def state_code
27
+ value = context_part('region', 'short_code')
28
+ value.split('-').last unless value.nil?
29
+ end
27
30
 
28
31
  def postal_code
29
32
  context_part('postcode')
@@ -33,7 +36,10 @@ module Geocoder::Result
33
36
  context_part('country')
34
37
  end
35
38
 
36
- alias_method :country_code, :country
39
+ def country_code
40
+ value = context_part('country', 'short_code')
41
+ value.upcase unless value.nil?
42
+ end
37
43
 
38
44
  def neighborhood
39
45
  context_part('neighborhood')
@@ -45,8 +51,8 @@ module Geocoder::Result
45
51
 
46
52
  private
47
53
 
48
- def context_part(name)
49
- context.map { |c| c['text'] if c['id'] =~ Regexp.new(name) }.compact.first
54
+ def context_part(name, key = 'text')
55
+ (context.detect { |c| c['id'] =~ Regexp.new(name) } || {})[key]
50
56
  end
51
57
 
52
58
  def context
@@ -0,0 +1,46 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class MelissaStreet < Base
5
+ def address(format = :full)
6
+ @data['FormattedAddress']
7
+ end
8
+
9
+ def street_address
10
+ @data['AddressLine1']
11
+ end
12
+
13
+ def suffix
14
+ @data['ThoroughfareTrailingType']
15
+ end
16
+
17
+ def number
18
+ @data['PremisesNumber']
19
+ end
20
+
21
+ def city
22
+ @data['Locality']
23
+ end
24
+
25
+ def state_code
26
+ @data['AdministrativeArea']
27
+ end
28
+ alias_method :state, :state_code
29
+
30
+ def country
31
+ @data['CountryName']
32
+ end
33
+
34
+ def country_code
35
+ @data['CountryISO3166_1_Alpha2']
36
+ end
37
+
38
+ def postal_code
39
+ @data['PostalCode']
40
+ end
41
+
42
+ def coordinates
43
+ [@data['Latitude'].to_f, @data['Longitude'].to_f]
44
+ end
45
+ end
46
+ end