geocoder 1.5.1 → 1.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +107 -0
- data/LICENSE +1 -1
- data/README.md +364 -243
- data/bin/console +13 -0
- data/examples/app_defined_lookup_services.rb +22 -0
- data/lib/easting_northing.rb +171 -0
- data/lib/generators/geocoder/config/templates/initializer.rb +6 -1
- data/lib/geocoder/cache.rb +16 -33
- data/lib/geocoder/cache_stores/base.rb +40 -0
- data/lib/geocoder/cache_stores/generic.rb +35 -0
- data/lib/geocoder/cache_stores/redis.rb +34 -0
- data/lib/geocoder/configuration.rb +19 -5
- data/lib/geocoder/configuration_hash.rb +4 -4
- data/lib/geocoder/ip_address.rb +11 -1
- data/lib/geocoder/lookup.rb +42 -6
- data/lib/geocoder/lookups/abstract_api.rb +46 -0
- data/lib/geocoder/lookups/amap.rb +2 -2
- data/lib/geocoder/lookups/amazon_location_service.rb +58 -0
- data/lib/geocoder/lookups/azure.rb +56 -0
- data/lib/geocoder/lookups/ban_data_gouv_fr.rb +15 -2
- data/lib/geocoder/lookups/base.rb +4 -1
- data/lib/geocoder/lookups/bing.rb +2 -2
- data/lib/geocoder/lookups/esri.rb +24 -5
- data/lib/geocoder/lookups/freegeoip.rb +12 -10
- data/lib/geocoder/lookups/geoapify.rb +78 -0
- data/lib/geocoder/lookups/geocodio.rb +1 -1
- data/lib/geocoder/lookups/geoip2.rb +4 -0
- data/lib/geocoder/lookups/geoportail_lu.rb +1 -1
- data/lib/geocoder/lookups/google.rb +7 -2
- data/lib/geocoder/lookups/google_places_details.rb +26 -12
- data/lib/geocoder/lookups/google_places_search.rb +44 -2
- data/lib/geocoder/lookups/google_premier.rb +4 -0
- data/lib/geocoder/lookups/here.rb +27 -31
- data/lib/geocoder/lookups/ip2location.rb +10 -14
- data/lib/geocoder/lookups/ip2location_io.rb +62 -0
- data/lib/geocoder/lookups/ip2location_lite.rb +40 -0
- data/lib/geocoder/lookups/ipbase.rb +49 -0
- data/lib/geocoder/lookups/ipdata_co.rb +1 -1
- data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
- data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
- data/lib/geocoder/lookups/ipregistry.rb +68 -0
- data/lib/geocoder/lookups/latlon.rb +1 -2
- data/lib/geocoder/lookups/location_iq.rb +5 -1
- data/lib/geocoder/lookups/mapbox.rb +3 -3
- data/lib/geocoder/lookups/maxmind_local.rb +7 -1
- data/lib/geocoder/lookups/melissa_street.rb +41 -0
- data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
- data/lib/geocoder/lookups/osmnames.rb +57 -0
- data/lib/geocoder/lookups/pc_miler.rb +85 -0
- data/lib/geocoder/lookups/pdok_nl.rb +43 -0
- data/lib/geocoder/lookups/photon.rb +89 -0
- data/lib/geocoder/lookups/pickpoint.rb +1 -1
- data/lib/geocoder/lookups/smarty_streets.rb +6 -1
- data/lib/geocoder/lookups/telize.rb +1 -1
- data/lib/geocoder/lookups/tencent.rb +9 -9
- data/lib/geocoder/lookups/test.rb +5 -0
- data/lib/geocoder/lookups/twogis.rb +58 -0
- data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/lookups/yandex.rb +6 -7
- data/lib/geocoder/query.rb +1 -1
- data/lib/geocoder/results/abstract_api.rb +146 -0
- data/lib/geocoder/results/amazon_location_service.rb +62 -0
- data/lib/geocoder/results/azure.rb +65 -0
- data/lib/geocoder/results/baidu.rb +0 -4
- data/lib/geocoder/results/ban_data_gouv_fr.rb +28 -3
- data/lib/geocoder/results/db_ip_com.rb +1 -1
- data/lib/geocoder/results/esri.rb +5 -2
- data/lib/geocoder/results/geoapify.rb +179 -0
- data/lib/geocoder/results/here.rb +20 -22
- data/lib/geocoder/results/ip2location_io.rb +21 -0
- data/lib/geocoder/results/ip2location_lite.rb +47 -0
- data/lib/geocoder/results/ipbase.rb +40 -0
- data/lib/geocoder/results/ipgeolocation.rb +59 -0
- data/lib/geocoder/results/ipqualityscore.rb +54 -0
- data/lib/geocoder/results/ipregistry.rb +304 -0
- data/lib/geocoder/results/mapbox.rb +34 -10
- data/lib/geocoder/results/melissa_street.rb +46 -0
- data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
- data/lib/geocoder/results/nominatim.rb +27 -15
- data/lib/geocoder/results/osmnames.rb +56 -0
- data/lib/geocoder/results/pc_miler.rb +98 -0
- data/lib/geocoder/results/pdok_nl.rb +62 -0
- data/lib/geocoder/results/photon.rb +119 -0
- data/lib/geocoder/results/twogis.rb +76 -0
- data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
- data/lib/geocoder/results/yandex.rb +217 -59
- data/lib/geocoder/sql.rb +4 -4
- data/lib/geocoder/util.rb +29 -0
- data/lib/geocoder/version.rb +1 -1
- data/lib/maxmind_database.rb +12 -12
- data/lib/tasks/maxmind.rake +1 -1
- metadata +81 -23
- data/examples/autoexpire_cache_dalli.rb +0 -62
- data/examples/autoexpire_cache_redis.rb +0 -28
- data/lib/geocoder/lookups/dstk.rb +0 -22
- data/lib/geocoder/lookups/geocoder_us.rb +0 -51
- data/lib/geocoder/results/dstk.rb +0 -6
- data/lib/geocoder/results/geocoder_us.rb +0 -39
- data/lib/hash_recursive_merge.rb +0 -74
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'geocoder/lookups/base'
|
4
|
+
require 'geocoder/results/abstract_api'
|
5
|
+
|
6
|
+
module Geocoder::Lookup
|
7
|
+
class AbstractApi < Base
|
8
|
+
|
9
|
+
def name
|
10
|
+
"Abstract API"
|
11
|
+
end
|
12
|
+
|
13
|
+
def required_api_key_parts
|
14
|
+
['api_key']
|
15
|
+
end
|
16
|
+
|
17
|
+
def supported_protocols
|
18
|
+
[:https]
|
19
|
+
end
|
20
|
+
|
21
|
+
private # ---------------------------------------------------------------
|
22
|
+
|
23
|
+
def base_query_url(query)
|
24
|
+
"#{protocol}://ipgeolocation.abstractapi.com/v1/?"
|
25
|
+
end
|
26
|
+
|
27
|
+
def query_url_params(query)
|
28
|
+
params = {api_key: configuration.api_key}
|
29
|
+
|
30
|
+
ip_address = query.sanitized_text
|
31
|
+
if ip_address.is_a?(String) && ip_address.length > 0
|
32
|
+
params[:ip_address] = ip_address
|
33
|
+
end
|
34
|
+
|
35
|
+
params.merge(super)
|
36
|
+
end
|
37
|
+
|
38
|
+
def results(query, reverse = false)
|
39
|
+
if doc = fetch_data(query)
|
40
|
+
[doc]
|
41
|
+
else
|
42
|
+
[]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -32,10 +32,10 @@ module Geocoder::Lookup
|
|
32
32
|
return doc['geocodes'] unless doc['geocodes'].blank?
|
33
33
|
when ['0', 'INVALID_USER_KEY']
|
34
34
|
raise_error(Geocoder::InvalidApiKey, "invalid api key") ||
|
35
|
-
warn
|
35
|
+
Geocoder.log(:warn, "#{self.name} Geocoding API error: invalid api key.")
|
36
36
|
else
|
37
37
|
raise_error(Geocoder::Error, "server error.") ||
|
38
|
-
warn
|
38
|
+
Geocoder.log(:warn, "#{self.name} Geocoding API error: server error - [#{doc['info']}]")
|
39
39
|
end
|
40
40
|
return []
|
41
41
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'geocoder/lookups/base'
|
2
|
+
require 'geocoder/results/amazon_location_service'
|
3
|
+
|
4
|
+
module Geocoder::Lookup
|
5
|
+
class AmazonLocationService < Base
|
6
|
+
def results(query)
|
7
|
+
params = query.options.dup
|
8
|
+
|
9
|
+
# index_name is required
|
10
|
+
# Aws::ParamValidator raises ArgumentError on missing required keys
|
11
|
+
params.merge!(index_name: configuration[:index_name])
|
12
|
+
|
13
|
+
# Aws::ParamValidator raises ArgumentError on unexpected keys
|
14
|
+
params.delete(:lookup)
|
15
|
+
|
16
|
+
# Inherit language from configuration
|
17
|
+
params.merge!(language: configuration[:language])
|
18
|
+
|
19
|
+
resp = if query.reverse_geocode?
|
20
|
+
client.search_place_index_for_position(params.merge(position: query.coordinates.reverse))
|
21
|
+
else
|
22
|
+
client.search_place_index_for_text(params.merge(text: query.text))
|
23
|
+
end
|
24
|
+
|
25
|
+
resp.results
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def client
|
31
|
+
return @client if @client
|
32
|
+
require_sdk
|
33
|
+
keys = configuration.api_key
|
34
|
+
if keys
|
35
|
+
@client = Aws::LocationService::Client.new(**{
|
36
|
+
region: keys[:region],
|
37
|
+
access_key_id: keys[:access_key_id],
|
38
|
+
secret_access_key: keys[:secret_access_key]
|
39
|
+
}.compact)
|
40
|
+
else
|
41
|
+
@client = Aws::LocationService::Client.new
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def require_sdk
|
46
|
+
begin
|
47
|
+
require 'aws-sdk-locationservice'
|
48
|
+
rescue LoadError
|
49
|
+
raise_error(Geocoder::ConfigurationError) ||
|
50
|
+
Geocoder.log(
|
51
|
+
:error,
|
52
|
+
"Couldn't load the Amazon Location Service SDK. " +
|
53
|
+
"Install it with: gem install aws-sdk-locationservice -v '~> 1.4'"
|
54
|
+
)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'geocoder/lookups/base'
|
2
|
+
require 'geocoder/results/azure'
|
3
|
+
|
4
|
+
module Geocoder::Lookup
|
5
|
+
class Azure < Base
|
6
|
+
def name
|
7
|
+
'Azure'
|
8
|
+
end
|
9
|
+
|
10
|
+
def required_api_key_parts
|
11
|
+
['api_key']
|
12
|
+
end
|
13
|
+
|
14
|
+
def supported_protocols
|
15
|
+
[:https]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def base_query_url(query)
|
21
|
+
host = 'atlas.microsoft.com/search/address'
|
22
|
+
|
23
|
+
if query.reverse_geocode?
|
24
|
+
"#{protocol}://#{host}/reverse/json?"
|
25
|
+
else
|
26
|
+
"#{protocol}://#{host}/json?"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def query_url_params(query)
|
31
|
+
params = {
|
32
|
+
'api-version' => 1.0,
|
33
|
+
'language' => query.options[:language] || 'en',
|
34
|
+
'limit' => configuration[:limit] || 10,
|
35
|
+
'query' => query.sanitized_text,
|
36
|
+
'subscription-key' => configuration.api_key
|
37
|
+
}
|
38
|
+
|
39
|
+
params.merge(super)
|
40
|
+
end
|
41
|
+
|
42
|
+
def results(query)
|
43
|
+
return [] unless (doc = fetch_data(query))
|
44
|
+
|
45
|
+
return doc if doc['error']
|
46
|
+
|
47
|
+
if doc['results']&.any?
|
48
|
+
doc['results']
|
49
|
+
elsif doc['addresses']&.any?
|
50
|
+
doc['addresses']
|
51
|
+
else
|
52
|
+
[]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -22,7 +22,7 @@ module Geocoder::Lookup
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def any_result?(doc)
|
25
|
-
doc['features'].any?
|
25
|
+
doc['features'] and doc['features'].any?
|
26
26
|
end
|
27
27
|
|
28
28
|
def results(query)
|
@@ -86,6 +86,12 @@ module Geocoder::Lookup
|
|
86
86
|
unless (citycode = query.options[:citycode]).nil? || !code_param_is_valid?(citycode)
|
87
87
|
params[:citycode] = citycode.to_s
|
88
88
|
end
|
89
|
+
unless (lat = query.options[:lat]).nil? || !latitude_is_valid?(lat)
|
90
|
+
params[:lat] = lat
|
91
|
+
end
|
92
|
+
unless (lon = query.options[:lon]).nil? || !longitude_is_valid?(lon)
|
93
|
+
params[:lon] = lon
|
94
|
+
end
|
89
95
|
params
|
90
96
|
end
|
91
97
|
|
@@ -119,12 +125,19 @@ module Geocoder::Lookup
|
|
119
125
|
end
|
120
126
|
|
121
127
|
def type_param_is_valid?(param)
|
122
|
-
%w(housenumber street locality
|
128
|
+
%w(housenumber street locality municipality).include?(param.downcase)
|
123
129
|
end
|
124
130
|
|
125
131
|
def code_param_is_valid?(param)
|
126
132
|
(1..99999).include?(param.to_i)
|
127
133
|
end
|
128
134
|
|
135
|
+
def latitude_is_valid?(param)
|
136
|
+
param.to_f <= 90 && param.to_f >= -90
|
137
|
+
end
|
138
|
+
|
139
|
+
def longitude_is_valid?(param)
|
140
|
+
param.to_f <= 180 && param.to_f >= -180
|
141
|
+
end
|
129
142
|
end
|
130
143
|
end
|
@@ -84,7 +84,8 @@ module Geocoder
|
|
84
84
|
#
|
85
85
|
def cache
|
86
86
|
if @cache.nil? and store = configuration.cache
|
87
|
-
|
87
|
+
cache_options = configuration.cache_options
|
88
|
+
@cache = Cache.new(store, cache_options)
|
88
89
|
end
|
89
90
|
@cache
|
90
91
|
end
|
@@ -197,6 +198,8 @@ module Geocoder
|
|
197
198
|
raise_error(err) or Geocoder.log(:warn, "Geocoding API connection cannot be established.")
|
198
199
|
rescue Errno::ECONNREFUSED => err
|
199
200
|
raise_error(err) or Geocoder.log(:warn, "Geocoding API connection refused.")
|
201
|
+
rescue Geocoder::NetworkError => err
|
202
|
+
raise_error(err) or Geocoder.log(:warn, "Geocoding API connection is either unreacheable or reset by the peer")
|
200
203
|
rescue Timeout::Error => err
|
201
204
|
raise_error(err) or Geocoder.log(:warn, "Geocoding API not responding fast enough " +
|
202
205
|
"(use Geocoder.configure(:timeout => ...) to set limit).")
|
@@ -19,7 +19,7 @@ module Geocoder::Lookup
|
|
19
19
|
private # ---------------------------------------------------------------
|
20
20
|
|
21
21
|
def base_query_url(query)
|
22
|
-
text =
|
22
|
+
text = ERB::Util.url_encode(query.sanitized_text.strip)
|
23
23
|
url = "#{protocol}://dev.virtualearth.net/REST/v1/Locations/"
|
24
24
|
if query.reverse_geocode?
|
25
25
|
url + "#{text}?"
|
@@ -54,7 +54,7 @@ module Geocoder::Lookup
|
|
54
54
|
def query_url_params(query)
|
55
55
|
{
|
56
56
|
key: configuration.api_key,
|
57
|
-
|
57
|
+
culture: (query.language || configuration.language)
|
58
58
|
}.merge(super)
|
59
59
|
end
|
60
60
|
|
@@ -9,6 +9,10 @@ module Geocoder::Lookup
|
|
9
9
|
"Esri"
|
10
10
|
end
|
11
11
|
|
12
|
+
def supported_protocols
|
13
|
+
[:https]
|
14
|
+
end
|
15
|
+
|
12
16
|
private # ---------------------------------------------------------------
|
13
17
|
|
14
18
|
def base_query_url(query)
|
@@ -19,15 +23,28 @@ module Geocoder::Lookup
|
|
19
23
|
def results(query)
|
20
24
|
return [] unless doc = fetch_data(query)
|
21
25
|
|
22
|
-
if (!query.reverse_geocode?)
|
23
|
-
return [] if !doc['locations'] || doc['locations'].empty?
|
24
|
-
end
|
25
|
-
|
26
26
|
if (doc['error'].nil?)
|
27
|
+
if (!query.reverse_geocode?)
|
28
|
+
return [] if !doc['locations'] || doc['locations'].empty?
|
29
|
+
end
|
27
30
|
return [ doc ]
|
28
31
|
else
|
29
|
-
|
32
|
+
case [ doc['error']['code'] ]
|
33
|
+
when [498]
|
34
|
+
raise_error(Geocoder::InvalidApiKey, doc['error']['message']) ||
|
35
|
+
Geocoder.log(:warn, "#{self.name} Geocoding API error: #{doc['error']['message']}")
|
36
|
+
when [ 403 ]
|
37
|
+
raise_error(Geocoder::RequestDenied, 'ESRI request denied') ||
|
38
|
+
Geocoder.log(:warn, "#{self.name} ESRI request denied: #{doc['error']['message']}")
|
39
|
+
when [ 500 ], [501]
|
40
|
+
raise_error(Geocoder::ServiceUnavailable, 'ESRI service unavailable') ||
|
41
|
+
Geocoder.log(:warn, "#{self.name} ESRI service error: #{doc['error']['message']}")
|
42
|
+
else
|
43
|
+
raise_error(Geocoder::Error, doc['error']['message']) ||
|
44
|
+
Geocoder.log(:warn, "#{self.name} Geocoding error: #{doc['error']['message']}")
|
45
|
+
end
|
30
46
|
end
|
47
|
+
return []
|
31
48
|
end
|
32
49
|
|
33
50
|
def query_url_params(query)
|
@@ -47,6 +64,8 @@ module Geocoder::Lookup
|
|
47
64
|
params[:forStorage] = for_storage_value
|
48
65
|
end
|
49
66
|
params[:sourceCountry] = configuration[:source_country] if configuration[:source_country]
|
67
|
+
params[:preferredLabelValues] = configuration[:preferred_label_values] if configuration[:preferred_label_values]
|
68
|
+
|
50
69
|
params.merge(super)
|
51
70
|
end
|
52
71
|
|
@@ -10,21 +10,23 @@ module Geocoder::Lookup
|
|
10
10
|
|
11
11
|
def supported_protocols
|
12
12
|
if configuration[:host]
|
13
|
-
[:
|
13
|
+
[:https]
|
14
14
|
else
|
15
15
|
# use https for default host
|
16
16
|
[:https]
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def query_url(query)
|
21
|
-
"#{protocol}://#{host}/json/#{query.sanitized_text}"
|
22
|
-
end
|
23
|
-
|
24
20
|
private # ---------------------------------------------------------------
|
25
21
|
|
26
|
-
def
|
27
|
-
|
22
|
+
def base_query_url(query)
|
23
|
+
"#{protocol}://#{host}/json/#{query.sanitized_text}?"
|
24
|
+
end
|
25
|
+
|
26
|
+
def query_url_params(query)
|
27
|
+
{
|
28
|
+
:apikey => configuration.api_key
|
29
|
+
}.merge(super)
|
28
30
|
end
|
29
31
|
|
30
32
|
def parse_raw_data(raw_data)
|
@@ -44,8 +46,8 @@ module Geocoder::Lookup
|
|
44
46
|
"city" => "",
|
45
47
|
"region_code" => "",
|
46
48
|
"region_name" => "",
|
47
|
-
"
|
48
|
-
"
|
49
|
+
"metro_code" => "",
|
50
|
+
"zip_code" => "",
|
49
51
|
"latitude" => "0",
|
50
52
|
"longitude" => "0",
|
51
53
|
"country_name" => "Reserved",
|
@@ -54,7 +56,7 @@ module Geocoder::Lookup
|
|
54
56
|
end
|
55
57
|
|
56
58
|
def host
|
57
|
-
configuration[:host] || "freegeoip.
|
59
|
+
configuration[:host] || "freegeoip.app"
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'geocoder/lookups/base'
|
4
|
+
require 'geocoder/results/geoapify'
|
5
|
+
|
6
|
+
module Geocoder
|
7
|
+
module Lookup
|
8
|
+
# https://apidocs.geoapify.com/docs/geocoding/api
|
9
|
+
class Geoapify < Base
|
10
|
+
def name
|
11
|
+
'Geoapify'
|
12
|
+
end
|
13
|
+
|
14
|
+
def required_api_key_parts
|
15
|
+
['api_key']
|
16
|
+
end
|
17
|
+
|
18
|
+
def supported_protocols
|
19
|
+
[:https]
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def base_query_url(query)
|
25
|
+
method = if query.reverse_geocode?
|
26
|
+
'reverse'
|
27
|
+
elsif query.options[:autocomplete]
|
28
|
+
'autocomplete'
|
29
|
+
else
|
30
|
+
'search'
|
31
|
+
end
|
32
|
+
"https://api.geoapify.com/v1/geocode/#{method}?"
|
33
|
+
end
|
34
|
+
|
35
|
+
def results(query)
|
36
|
+
return [] unless (doc = fetch_data(query))
|
37
|
+
|
38
|
+
# The rest of the status codes should be already handled by the default
|
39
|
+
# functionality as the API returns correct HTTP response codes in most
|
40
|
+
# cases. There may be some unhandled cases still (such as over query
|
41
|
+
# limit reached) but there is not enough documentation to cover them.
|
42
|
+
case doc['statusCode']
|
43
|
+
when 500
|
44
|
+
raise_error(Geocoder::InvalidRequest) || Geocoder.log(:warn, doc['message'])
|
45
|
+
end
|
46
|
+
|
47
|
+
return [] unless doc['type'] == 'FeatureCollection'
|
48
|
+
return [] unless doc['features'] || doc['features'].present?
|
49
|
+
|
50
|
+
doc['features']
|
51
|
+
end
|
52
|
+
|
53
|
+
def query_url_params(query)
|
54
|
+
lang = query.language || configuration.language
|
55
|
+
params = { apiKey: configuration.api_key, lang: lang, limit: query.options[:limit] }
|
56
|
+
|
57
|
+
if query.reverse_geocode?
|
58
|
+
params.merge!(query_url_params_reverse(query))
|
59
|
+
else
|
60
|
+
params.merge!(query_url_params_coordinates(query))
|
61
|
+
end
|
62
|
+
|
63
|
+
params.merge!(super)
|
64
|
+
end
|
65
|
+
|
66
|
+
def query_url_params_coordinates(query)
|
67
|
+
{ text: query.sanitized_text }
|
68
|
+
end
|
69
|
+
|
70
|
+
def query_url_params_reverse(query)
|
71
|
+
{
|
72
|
+
lat: query.coordinates[0],
|
73
|
+
lon: query.coordinates[1]
|
74
|
+
}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -37,6 +37,10 @@ module Geocoder
|
|
37
37
|
def results(query)
|
38
38
|
return [] unless configuration[:file]
|
39
39
|
|
40
|
+
if @mmdb.respond_to?(:local_ip_alias) && !configuration[:local_ip_alias].nil?
|
41
|
+
@mmdb.local_ip_alias = configuration[:local_ip_alias]
|
42
|
+
end
|
43
|
+
|
40
44
|
result = @mmdb.lookup(query.to_s)
|
41
45
|
result.nil? ? [] : [result]
|
42
46
|
end
|
@@ -44,10 +44,15 @@ module Geocoder::Lookup
|
|
44
44
|
super(response) and ['OK', 'ZERO_RESULTS'].include?(status)
|
45
45
|
end
|
46
46
|
|
47
|
+
def result_root_attr
|
48
|
+
'results'
|
49
|
+
end
|
50
|
+
|
47
51
|
def results(query)
|
48
52
|
return [] unless doc = fetch_data(query)
|
49
|
-
case doc['status']
|
50
|
-
|
53
|
+
case doc['status']
|
54
|
+
when "OK" # OK status implies >0 results
|
55
|
+
return doc[result_root_attr]
|
51
56
|
when "OVER_QUERY_LIMIT"
|
52
57
|
raise_error(Geocoder::OverQueryLimitError) ||
|
53
58
|
Geocoder.log(:warn, "#{name} API error: over query limit.")
|
@@ -22,26 +22,40 @@ module Geocoder
|
|
22
22
|
"#{protocol}://maps.googleapis.com/maps/api/place/details/json?"
|
23
23
|
end
|
24
24
|
|
25
|
+
def result_root_attr
|
26
|
+
'result'
|
27
|
+
end
|
28
|
+
|
25
29
|
def results(query)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
when "INVALID_REQUEST"
|
36
|
-
raise_error(Geocoder::InvalidRequest) || Geocoder.log(:warn, "Google Places Details API error: invalid request.")
|
30
|
+
result = super(query)
|
31
|
+
return [result] unless result.is_a? Array
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
|
36
|
+
def fields(query)
|
37
|
+
if query.options.has_key?(:fields)
|
38
|
+
return format_fields(query.options[:fields])
|
37
39
|
end
|
38
40
|
|
39
|
-
|
41
|
+
if configuration.has_key?(:fields)
|
42
|
+
return format_fields(configuration[:fields])
|
43
|
+
end
|
44
|
+
|
45
|
+
nil # use Google Places defaults
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_fields(*fields)
|
49
|
+
flattened = fields.flatten.compact
|
50
|
+
return if flattened.empty?
|
51
|
+
|
52
|
+
flattened.join(',')
|
40
53
|
end
|
41
54
|
|
42
55
|
def query_url_google_params(query)
|
43
56
|
{
|
44
57
|
placeid: query.text,
|
58
|
+
fields: fields(query),
|
45
59
|
language: query.language || configuration.language
|
46
60
|
}
|
47
61
|
end
|
@@ -18,16 +18,58 @@ module Geocoder
|
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
+
def result_root_attr
|
22
|
+
'candidates'
|
23
|
+
end
|
24
|
+
|
21
25
|
def base_query_url(query)
|
22
|
-
"#{protocol}://maps.googleapis.com/maps/api/place/
|
26
|
+
"#{protocol}://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
|
23
27
|
end
|
24
28
|
|
25
29
|
def query_url_google_params(query)
|
26
30
|
{
|
27
|
-
|
31
|
+
input: query.text,
|
32
|
+
inputtype: 'textquery',
|
33
|
+
fields: fields(query),
|
34
|
+
locationbias: locationbias(query),
|
28
35
|
language: query.language || configuration.language
|
29
36
|
}
|
30
37
|
end
|
38
|
+
|
39
|
+
def fields(query)
|
40
|
+
if query.options.has_key?(:fields)
|
41
|
+
return format_fields(query.options[:fields])
|
42
|
+
end
|
43
|
+
|
44
|
+
if configuration.has_key?(:fields)
|
45
|
+
return format_fields(configuration[:fields])
|
46
|
+
end
|
47
|
+
|
48
|
+
default_fields
|
49
|
+
end
|
50
|
+
|
51
|
+
def default_fields
|
52
|
+
basic = %w[business_status formatted_address geometry icon name
|
53
|
+
photos place_id plus_code types]
|
54
|
+
contact = %w[opening_hours]
|
55
|
+
atmosphere = %W[price_level rating user_ratings_total]
|
56
|
+
format_fields(basic, contact, atmosphere)
|
57
|
+
end
|
58
|
+
|
59
|
+
def format_fields(*fields)
|
60
|
+
flattened = fields.flatten.compact
|
61
|
+
return if flattened.empty?
|
62
|
+
|
63
|
+
flattened.join(',')
|
64
|
+
end
|
65
|
+
|
66
|
+
def locationbias(query)
|
67
|
+
if query.options.has_key?(:locationbias)
|
68
|
+
query.options[:locationbias]
|
69
|
+
else
|
70
|
+
configuration[:locationbias]
|
71
|
+
end
|
72
|
+
end
|
31
73
|
end
|
32
74
|
end
|
33
75
|
end
|
@@ -21,6 +21,10 @@ module Geocoder::Lookup
|
|
21
21
|
|
22
22
|
private # ---------------------------------------------------------------
|
23
23
|
|
24
|
+
def result_root_attr
|
25
|
+
'results'
|
26
|
+
end
|
27
|
+
|
24
28
|
def cache_key(query)
|
25
29
|
"#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + hash_to_query(cache_key_params(query))
|
26
30
|
end
|