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.
Files changed (100) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +107 -0
  3. data/LICENSE +1 -1
  4. data/README.md +364 -243
  5. data/bin/console +13 -0
  6. data/examples/app_defined_lookup_services.rb +22 -0
  7. data/lib/easting_northing.rb +171 -0
  8. data/lib/generators/geocoder/config/templates/initializer.rb +6 -1
  9. data/lib/geocoder/cache.rb +16 -33
  10. data/lib/geocoder/cache_stores/base.rb +40 -0
  11. data/lib/geocoder/cache_stores/generic.rb +35 -0
  12. data/lib/geocoder/cache_stores/redis.rb +34 -0
  13. data/lib/geocoder/configuration.rb +19 -5
  14. data/lib/geocoder/configuration_hash.rb +4 -4
  15. data/lib/geocoder/ip_address.rb +11 -1
  16. data/lib/geocoder/lookup.rb +42 -6
  17. data/lib/geocoder/lookups/abstract_api.rb +46 -0
  18. data/lib/geocoder/lookups/amap.rb +2 -2
  19. data/lib/geocoder/lookups/amazon_location_service.rb +58 -0
  20. data/lib/geocoder/lookups/azure.rb +56 -0
  21. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +15 -2
  22. data/lib/geocoder/lookups/base.rb +4 -1
  23. data/lib/geocoder/lookups/bing.rb +2 -2
  24. data/lib/geocoder/lookups/esri.rb +24 -5
  25. data/lib/geocoder/lookups/freegeoip.rb +12 -10
  26. data/lib/geocoder/lookups/geoapify.rb +78 -0
  27. data/lib/geocoder/lookups/geocodio.rb +1 -1
  28. data/lib/geocoder/lookups/geoip2.rb +4 -0
  29. data/lib/geocoder/lookups/geoportail_lu.rb +1 -1
  30. data/lib/geocoder/lookups/google.rb +7 -2
  31. data/lib/geocoder/lookups/google_places_details.rb +26 -12
  32. data/lib/geocoder/lookups/google_places_search.rb +44 -2
  33. data/lib/geocoder/lookups/google_premier.rb +4 -0
  34. data/lib/geocoder/lookups/here.rb +27 -31
  35. data/lib/geocoder/lookups/ip2location.rb +10 -14
  36. data/lib/geocoder/lookups/ip2location_io.rb +62 -0
  37. data/lib/geocoder/lookups/ip2location_lite.rb +40 -0
  38. data/lib/geocoder/lookups/ipbase.rb +49 -0
  39. data/lib/geocoder/lookups/ipdata_co.rb +1 -1
  40. data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  41. data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
  42. data/lib/geocoder/lookups/ipregistry.rb +68 -0
  43. data/lib/geocoder/lookups/latlon.rb +1 -2
  44. data/lib/geocoder/lookups/location_iq.rb +5 -1
  45. data/lib/geocoder/lookups/mapbox.rb +3 -3
  46. data/lib/geocoder/lookups/maxmind_local.rb +7 -1
  47. data/lib/geocoder/lookups/melissa_street.rb +41 -0
  48. data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  49. data/lib/geocoder/lookups/osmnames.rb +57 -0
  50. data/lib/geocoder/lookups/pc_miler.rb +85 -0
  51. data/lib/geocoder/lookups/pdok_nl.rb +43 -0
  52. data/lib/geocoder/lookups/photon.rb +89 -0
  53. data/lib/geocoder/lookups/pickpoint.rb +1 -1
  54. data/lib/geocoder/lookups/smarty_streets.rb +6 -1
  55. data/lib/geocoder/lookups/telize.rb +1 -1
  56. data/lib/geocoder/lookups/tencent.rb +9 -9
  57. data/lib/geocoder/lookups/test.rb +5 -0
  58. data/lib/geocoder/lookups/twogis.rb +58 -0
  59. data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  60. data/lib/geocoder/lookups/yandex.rb +6 -7
  61. data/lib/geocoder/query.rb +1 -1
  62. data/lib/geocoder/results/abstract_api.rb +146 -0
  63. data/lib/geocoder/results/amazon_location_service.rb +62 -0
  64. data/lib/geocoder/results/azure.rb +65 -0
  65. data/lib/geocoder/results/baidu.rb +0 -4
  66. data/lib/geocoder/results/ban_data_gouv_fr.rb +28 -3
  67. data/lib/geocoder/results/db_ip_com.rb +1 -1
  68. data/lib/geocoder/results/esri.rb +5 -2
  69. data/lib/geocoder/results/geoapify.rb +179 -0
  70. data/lib/geocoder/results/here.rb +20 -22
  71. data/lib/geocoder/results/ip2location_io.rb +21 -0
  72. data/lib/geocoder/results/ip2location_lite.rb +47 -0
  73. data/lib/geocoder/results/ipbase.rb +40 -0
  74. data/lib/geocoder/results/ipgeolocation.rb +59 -0
  75. data/lib/geocoder/results/ipqualityscore.rb +54 -0
  76. data/lib/geocoder/results/ipregistry.rb +304 -0
  77. data/lib/geocoder/results/mapbox.rb +34 -10
  78. data/lib/geocoder/results/melissa_street.rb +46 -0
  79. data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  80. data/lib/geocoder/results/nominatim.rb +27 -15
  81. data/lib/geocoder/results/osmnames.rb +56 -0
  82. data/lib/geocoder/results/pc_miler.rb +98 -0
  83. data/lib/geocoder/results/pdok_nl.rb +62 -0
  84. data/lib/geocoder/results/photon.rb +119 -0
  85. data/lib/geocoder/results/twogis.rb +76 -0
  86. data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  87. data/lib/geocoder/results/yandex.rb +217 -59
  88. data/lib/geocoder/sql.rb +4 -4
  89. data/lib/geocoder/util.rb +29 -0
  90. data/lib/geocoder/version.rb +1 -1
  91. data/lib/maxmind_database.rb +12 -12
  92. data/lib/tasks/maxmind.rake +1 -1
  93. metadata +81 -23
  94. data/examples/autoexpire_cache_dalli.rb +0 -62
  95. data/examples/autoexpire_cache_redis.rb +0 -28
  96. data/lib/geocoder/lookups/dstk.rb +0 -22
  97. data/lib/geocoder/lookups/geocoder_us.rb +0 -51
  98. data/lib/geocoder/results/dstk.rb +0 -6
  99. data/lib/geocoder/results/geocoder_us.rb +0 -39
  100. 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("#{self.name} Geocoding API error: invalid api key.")
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("#{self.name} Geocoding API error: server error - [#{doc['info']}]")
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 village town city).include?(param.downcase)
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
- @cache = Cache.new(store, configuration.cache_prefix)
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 = CGI.escape(query.sanitized_text.strip)
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
- language: (query.language || configuration.language)
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
- return []
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
- [:http, :https]
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 cache_key(query)
27
- query_url(query)
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
- "metrocode" => "",
48
- "zipcode" => "",
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.net"
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
@@ -29,7 +29,7 @@ module Geocoder::Lookup
29
29
 
30
30
  def base_query_url(query)
31
31
  path = query.reverse_geocode? ? "reverse" : "geocode"
32
- "#{protocol}://api.geocod.io/v1.3/#{path}?"
32
+ "#{protocol}://api.geocod.io/v1.6/#{path}?"
33
33
  end
34
34
 
35
35
  def query_url_params(query)
@@ -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
@@ -56,7 +56,7 @@ module Geocoder
56
56
  else
57
57
  result = []
58
58
  raise_error(Geocoder::Error) ||
59
- warn("Geportail.lu Geocoding API error")
59
+ Geocoder.log(:warn, "Geportail.lu Geocoding API error")
60
60
  end
61
61
  result
62
62
  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']; when "OK" # OK status implies >0 results
50
- return doc['results']
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
- return [] unless doc = fetch_data(query)
27
-
28
- case doc["status"]
29
- when "OK"
30
- return [doc["result"]]
31
- when "OVER_QUERY_LIMIT"
32
- raise_error(Geocoder::OverQueryLimitError) || Geocoder.log(:warn, "Google Places Details API error: over query limit.")
33
- when "REQUEST_DENIED"
34
- raise_error(Geocoder::RequestDenied) || Geocoder.log(:warn, "Google Places Details API error: request denied.")
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/textsearch/json?"
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
- query: query.text,
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