geocoder 1.2.6 → 1.8.2

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 (309) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +266 -1
  3. data/LICENSE +1 -1
  4. data/README.md +530 -804
  5. data/bin/console +13 -0
  6. data/examples/app_defined_lookup_services.rb +22 -0
  7. data/examples/reverse_geocode_job.rb +40 -0
  8. data/lib/easting_northing.rb +171 -0
  9. data/lib/generators/geocoder/config/templates/initializer.rb +22 -16
  10. data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +2 -0
  11. data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +2 -0
  12. data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +1 -1
  13. data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +1 -1
  14. data/lib/generators/geocoder/migration_version.rb +15 -0
  15. data/lib/geocoder/cache.rb +20 -33
  16. data/lib/geocoder/cache_stores/base.rb +40 -0
  17. data/lib/geocoder/cache_stores/generic.rb +35 -0
  18. data/lib/geocoder/cache_stores/redis.rb +34 -0
  19. data/lib/geocoder/calculations.rb +30 -38
  20. data/lib/geocoder/cli.rb +2 -2
  21. data/lib/geocoder/configuration.rb +36 -9
  22. data/lib/geocoder/configuration_hash.rb +4 -4
  23. data/lib/geocoder/esri_token.rb +38 -0
  24. data/lib/geocoder/exceptions.rb +19 -0
  25. data/lib/geocoder/ip_address.rb +26 -11
  26. data/lib/geocoder/kernel_logger.rb +25 -0
  27. data/lib/geocoder/logger.rb +47 -0
  28. data/lib/geocoder/lookup.rb +63 -13
  29. data/lib/geocoder/lookups/abstract_api.rb +46 -0
  30. data/lib/geocoder/lookups/amap.rb +63 -0
  31. data/lib/geocoder/lookups/amazon_location_service.rb +55 -0
  32. data/lib/geocoder/lookups/baidu.rb +17 -9
  33. data/lib/geocoder/lookups/baidu_ip.rb +7 -31
  34. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +143 -0
  35. data/lib/geocoder/lookups/base.rb +75 -26
  36. data/lib/geocoder/lookups/bing.rb +38 -15
  37. data/lib/geocoder/lookups/db_ip_com.rb +52 -0
  38. data/lib/geocoder/lookups/dstk.rb +4 -2
  39. data/lib/geocoder/lookups/esri.rb +78 -12
  40. data/lib/geocoder/lookups/freegeoip.rb +22 -7
  41. data/lib/geocoder/lookups/geoapify.rb +78 -0
  42. data/lib/geocoder/lookups/geocoder_ca.rb +5 -6
  43. data/lib/geocoder/lookups/geocodio.rb +8 -8
  44. data/lib/geocoder/lookups/geoip2.rb +13 -4
  45. data/lib/geocoder/lookups/geoportail_lu.rb +65 -0
  46. data/lib/geocoder/lookups/google.rb +44 -11
  47. data/lib/geocoder/lookups/google_places_details.rb +31 -17
  48. data/lib/geocoder/lookups/google_places_search.rb +76 -0
  49. data/lib/geocoder/lookups/google_premier.rb +15 -1
  50. data/lib/geocoder/lookups/here.rb +38 -27
  51. data/lib/geocoder/lookups/ip2location.rb +71 -0
  52. data/lib/geocoder/lookups/ipapi_com.rb +82 -0
  53. data/lib/geocoder/lookups/ipbase.rb +49 -0
  54. data/lib/geocoder/lookups/ipdata_co.rb +62 -0
  55. data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  56. data/lib/geocoder/lookups/ipinfo_io.rb +44 -0
  57. data/lib/geocoder/lookups/ipqualityscore.rb +50 -0
  58. data/lib/geocoder/lookups/ipregistry.rb +68 -0
  59. data/lib/geocoder/lookups/ipstack.rb +63 -0
  60. data/lib/geocoder/lookups/latlon.rb +58 -0
  61. data/lib/geocoder/lookups/location_iq.rb +54 -0
  62. data/lib/geocoder/lookups/mapbox.rb +59 -0
  63. data/lib/geocoder/lookups/mapquest.rb +7 -9
  64. data/lib/geocoder/lookups/maxmind.rb +7 -7
  65. data/lib/geocoder/lookups/maxmind_geoip2.rb +70 -0
  66. data/lib/geocoder/lookups/maxmind_local.rb +16 -3
  67. data/lib/geocoder/lookups/melissa_street.rb +41 -0
  68. data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  69. data/lib/geocoder/lookups/nominatim.rb +18 -6
  70. data/lib/geocoder/lookups/opencagedata.rb +16 -9
  71. data/lib/geocoder/lookups/osmnames.rb +57 -0
  72. data/lib/geocoder/lookups/pc_miler.rb +85 -0
  73. data/lib/geocoder/lookups/pelias.rb +63 -0
  74. data/lib/geocoder/lookups/photon.rb +89 -0
  75. data/lib/geocoder/lookups/pickpoint.rb +41 -0
  76. data/lib/geocoder/lookups/pointpin.rb +14 -13
  77. data/lib/geocoder/lookups/postcode_anywhere_uk.rb +7 -8
  78. data/lib/geocoder/lookups/postcodes_io.rb +31 -0
  79. data/lib/geocoder/lookups/smarty_streets.rb +29 -6
  80. data/lib/geocoder/lookups/telize.rb +42 -7
  81. data/lib/geocoder/lookups/tencent.rb +59 -0
  82. data/lib/geocoder/lookups/test.rb +5 -0
  83. data/lib/geocoder/lookups/twogis.rb +58 -0
  84. data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  85. data/lib/geocoder/lookups/yandex.rb +20 -13
  86. data/lib/geocoder/models/active_record.rb +4 -3
  87. data/lib/geocoder/models/mongo_base.rb +0 -2
  88. data/lib/geocoder/query.rb +15 -1
  89. data/lib/geocoder/railtie.rb +1 -1
  90. data/lib/geocoder/request.rb +103 -14
  91. data/lib/geocoder/results/abstract_api.rb +146 -0
  92. data/lib/geocoder/results/amap.rb +87 -0
  93. data/lib/geocoder/results/amazon_location_service.rb +57 -0
  94. data/lib/geocoder/results/baidu.rb +10 -14
  95. data/lib/geocoder/results/ban_data_gouv_fr.rb +282 -0
  96. data/lib/geocoder/results/base.rb +13 -1
  97. data/lib/geocoder/results/bing.rb +5 -1
  98. data/lib/geocoder/results/db_ip_com.rb +58 -0
  99. data/lib/geocoder/results/esri.rb +35 -8
  100. data/lib/geocoder/results/freegeoip.rb +2 -7
  101. data/lib/geocoder/results/geoapify.rb +179 -0
  102. data/lib/geocoder/results/geocoder_ca.rb +3 -3
  103. data/lib/geocoder/results/geocodio.rb +15 -3
  104. data/lib/geocoder/results/geoip2.rb +37 -25
  105. data/lib/geocoder/results/geoportail_lu.rb +71 -0
  106. data/lib/geocoder/results/google.rb +26 -0
  107. data/lib/geocoder/results/google_places_details.rb +4 -0
  108. data/lib/geocoder/results/google_places_search.rb +52 -0
  109. data/lib/geocoder/results/here.rb +30 -15
  110. data/lib/geocoder/results/ip2location.rb +22 -0
  111. data/lib/geocoder/results/ipapi_com.rb +45 -0
  112. data/lib/geocoder/results/ipbase.rb +40 -0
  113. data/lib/geocoder/results/ipdata_co.rb +40 -0
  114. data/lib/geocoder/results/ipgeolocation.rb +59 -0
  115. data/lib/geocoder/results/ipinfo_io.rb +48 -0
  116. data/lib/geocoder/results/ipqualityscore.rb +54 -0
  117. data/lib/geocoder/results/ipregistry.rb +304 -0
  118. data/lib/geocoder/results/ipstack.rb +60 -0
  119. data/lib/geocoder/results/latlon.rb +71 -0
  120. data/lib/geocoder/results/location_iq.rb +6 -0
  121. data/lib/geocoder/results/mapbox.rb +63 -0
  122. data/lib/geocoder/results/mapquest.rb +5 -8
  123. data/lib/geocoder/results/maxmind.rb +0 -5
  124. data/lib/geocoder/results/maxmind_geoip2.rb +9 -0
  125. data/lib/geocoder/results/maxmind_local.rb +0 -5
  126. data/lib/geocoder/results/melissa_street.rb +46 -0
  127. data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  128. data/lib/geocoder/results/nominatim.rb +41 -14
  129. data/lib/geocoder/results/opencagedata.rb +20 -2
  130. data/lib/geocoder/results/osmnames.rb +56 -0
  131. data/lib/geocoder/results/pc_miler.rb +98 -0
  132. data/lib/geocoder/results/pelias.rb +58 -0
  133. data/lib/geocoder/results/photon.rb +119 -0
  134. data/lib/geocoder/results/pickpoint.rb +6 -0
  135. data/lib/geocoder/results/pointpin.rb +0 -4
  136. data/lib/geocoder/results/postcodes_io.rb +40 -0
  137. data/lib/geocoder/results/smarty_streets.rb +55 -19
  138. data/lib/geocoder/results/telize.rb +0 -5
  139. data/lib/geocoder/results/tencent.rb +72 -0
  140. data/lib/geocoder/results/test.rb +1 -1
  141. data/lib/geocoder/results/twogis.rb +76 -0
  142. data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  143. data/lib/geocoder/results/yandex.rb +240 -32
  144. data/lib/geocoder/sql.rb +9 -6
  145. data/lib/geocoder/stores/active_record.rb +49 -10
  146. data/lib/geocoder/stores/base.rb +2 -14
  147. data/lib/geocoder/stores/mongo_base.rb +0 -31
  148. data/lib/geocoder/util.rb +29 -0
  149. data/lib/geocoder/version.rb +1 -1
  150. data/lib/geocoder.rb +2 -1
  151. data/lib/maxmind_database.rb +9 -9
  152. data/lib/tasks/geocoder.rake +29 -4
  153. data/lib/tasks/maxmind.rake +1 -1
  154. metadata +91 -169
  155. data/.gitignore +0 -6
  156. data/.travis.yml +0 -31
  157. data/Rakefile +0 -25
  158. data/examples/autoexpire_cache_dalli.rb +0 -62
  159. data/examples/autoexpire_cache_redis.rb +0 -28
  160. data/gemfiles/Gemfile.mongoid-2.4.x +0 -16
  161. data/lib/geocoder/lookups/geocoder_us.rb +0 -39
  162. data/lib/geocoder/lookups/okf.rb +0 -43
  163. data/lib/geocoder/lookups/ovi.rb +0 -62
  164. data/lib/geocoder/lookups/yahoo.rb +0 -88
  165. data/lib/geocoder/results/geocoder_us.rb +0 -39
  166. data/lib/geocoder/results/okf.rb +0 -106
  167. data/lib/geocoder/results/ovi.rb +0 -62
  168. data/lib/geocoder/results/yahoo.rb +0 -55
  169. data/lib/hash_recursive_merge.rb +0 -74
  170. data/lib/oauth_util.rb +0 -112
  171. data/test/fixtures/baidu_invalid_key +0 -1
  172. data/test/fixtures/baidu_ip_202_198_16_3 +0 -19
  173. data/test/fixtures/baidu_ip_invalid_key +0 -1
  174. data/test/fixtures/baidu_ip_no_results +0 -1
  175. data/test/fixtures/baidu_no_results +0 -1
  176. data/test/fixtures/baidu_reverse +0 -1
  177. data/test/fixtures/baidu_shanghai_pearl_tower +0 -12
  178. data/test/fixtures/bing_invalid_key +0 -1
  179. data/test/fixtures/bing_madison_square_garden +0 -40
  180. data/test/fixtures/bing_no_results +0 -16
  181. data/test/fixtures/bing_reverse +0 -42
  182. data/test/fixtures/cloudmade_invalid_key +0 -1
  183. data/test/fixtures/cloudmade_madison_square_garden +0 -1
  184. data/test/fixtures/cloudmade_no_results +0 -1
  185. data/test/fixtures/esri_madison_square_garden +0 -59
  186. data/test/fixtures/esri_no_results +0 -8
  187. data/test/fixtures/esri_reverse +0 -21
  188. data/test/fixtures/freegeoip_74_200_247_59 +0 -12
  189. data/test/fixtures/freegeoip_no_results +0 -1
  190. data/test/fixtures/geocoder_ca_madison_square_garden +0 -1
  191. data/test/fixtures/geocoder_ca_no_results +0 -1
  192. data/test/fixtures/geocoder_ca_reverse +0 -34
  193. data/test/fixtures/geocoder_us_madison_square_garden +0 -1
  194. data/test/fixtures/geocoder_us_no_results +0 -1
  195. data/test/fixtures/geocodio_1101_pennsylvania_ave +0 -1
  196. data/test/fixtures/geocodio_bad_api_key +0 -3
  197. data/test/fixtures/geocodio_invalid +0 -4
  198. data/test/fixtures/geocodio_no_results +0 -1
  199. data/test/fixtures/geocodio_over_query_limit +0 -4
  200. data/test/fixtures/google_garbage +0 -456
  201. data/test/fixtures/google_madison_square_garden +0 -57
  202. data/test/fixtures/google_no_city_data +0 -44
  203. data/test/fixtures/google_no_locality +0 -51
  204. data/test/fixtures/google_no_results +0 -4
  205. data/test/fixtures/google_over_limit +0 -4
  206. data/test/fixtures/google_places_details_invalid_request +0 -4
  207. data/test/fixtures/google_places_details_madison_square_garden +0 -120
  208. data/test/fixtures/google_places_details_no_results +0 -4
  209. data/test/fixtures/google_places_details_no_reviews +0 -60
  210. data/test/fixtures/google_places_details_no_types +0 -66
  211. data/test/fixtures/here_madison_square_garden +0 -72
  212. data/test/fixtures/here_no_results +0 -8
  213. data/test/fixtures/mapquest_error +0 -16
  214. data/test/fixtures/mapquest_invalid_api_key +0 -16
  215. data/test/fixtures/mapquest_invalid_request +0 -16
  216. data/test/fixtures/mapquest_madison_square_garden +0 -52
  217. data/test/fixtures/mapquest_no_results +0 -16
  218. data/test/fixtures/maxmind_24_24_24_21 +0 -1
  219. data/test/fixtures/maxmind_24_24_24_22 +0 -1
  220. data/test/fixtures/maxmind_24_24_24_23 +0 -1
  221. data/test/fixtures/maxmind_24_24_24_24 +0 -1
  222. data/test/fixtures/maxmind_74_200_247_59 +0 -1
  223. data/test/fixtures/maxmind_invalid_key +0 -1
  224. data/test/fixtures/maxmind_no_results +0 -1
  225. data/test/fixtures/nominatim_madison_square_garden +0 -150
  226. data/test/fixtures/nominatim_no_results +0 -1
  227. data/test/fixtures/nominatim_over_limit +0 -1
  228. data/test/fixtures/okf_kirstinmaki +0 -67
  229. data/test/fixtures/okf_no_results +0 -4
  230. data/test/fixtures/opencagedata_invalid_api_key +0 -25
  231. data/test/fixtures/opencagedata_invalid_request +0 -26
  232. data/test/fixtures/opencagedata_madison_square_garden +0 -73
  233. data/test/fixtures/opencagedata_no_results +0 -29
  234. data/test/fixtures/opencagedata_over_limit +0 -31
  235. data/test/fixtures/ovi_madison_square_garden +0 -72
  236. data/test/fixtures/ovi_no_results +0 -8
  237. data/test/fixtures/pointpin_10_10_10_10 +0 -1
  238. data/test/fixtures/pointpin_555_555_555_555 +0 -1
  239. data/test/fixtures/pointpin_80_111_555_555 +0 -1
  240. data/test/fixtures/pointpin_no_results +0 -1
  241. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_WR26NJ +0 -1
  242. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_generic_error +0 -1
  243. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire +0 -1
  244. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_key_limit_exceeded +0 -1
  245. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_no_results +0 -1
  246. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_romsey +0 -1
  247. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_unknown_key +0 -1
  248. data/test/fixtures/smarty_streets_11211 +0 -1
  249. data/test/fixtures/smarty_streets_madison_square_garden +0 -47
  250. data/test/fixtures/smarty_streets_no_results +0 -1
  251. data/test/fixtures/telize_10_10_10_10 +0 -1
  252. data/test/fixtures/telize_555_555_555_555 +0 -4
  253. data/test/fixtures/telize_74_200_247_59 +0 -1
  254. data/test/fixtures/telize_no_results +0 -1
  255. data/test/fixtures/yahoo_error +0 -1
  256. data/test/fixtures/yahoo_invalid_key +0 -2
  257. data/test/fixtures/yahoo_madison_square_garden +0 -52
  258. data/test/fixtures/yahoo_no_results +0 -10
  259. data/test/fixtures/yahoo_over_limit +0 -2
  260. data/test/fixtures/yandex_canada_rue_dupuis_14 +0 -446
  261. data/test/fixtures/yandex_invalid_key +0 -1
  262. data/test/fixtures/yandex_kremlin +0 -48
  263. data/test/fixtures/yandex_new_york +0 -1
  264. data/test/fixtures/yandex_no_city_and_town +0 -112
  265. data/test/fixtures/yandex_no_results +0 -16
  266. data/test/integration/http_client_test.rb +0 -31
  267. data/test/mongoid_test_helper.rb +0 -43
  268. data/test/test_helper.rb +0 -416
  269. data/test/unit/active_record_test.rb +0 -16
  270. data/test/unit/cache_test.rb +0 -37
  271. data/test/unit/calculations_test.rb +0 -220
  272. data/test/unit/configuration_test.rb +0 -55
  273. data/test/unit/error_handling_test.rb +0 -56
  274. data/test/unit/geocoder_test.rb +0 -78
  275. data/test/unit/https_test.rb +0 -17
  276. data/test/unit/ip_address_test.rb +0 -27
  277. data/test/unit/lookup_test.rb +0 -153
  278. data/test/unit/lookups/bing_test.rb +0 -68
  279. data/test/unit/lookups/dstk_test.rb +0 -26
  280. data/test/unit/lookups/esri_test.rb +0 -48
  281. data/test/unit/lookups/freegeoip_test.rb +0 -27
  282. data/test/unit/lookups/geocoder_ca_test.rb +0 -17
  283. data/test/unit/lookups/geocodio_test.rb +0 -55
  284. data/test/unit/lookups/geoip2_test.rb +0 -27
  285. data/test/unit/lookups/google_places_details_test.rb +0 -122
  286. data/test/unit/lookups/google_premier_test.rb +0 -22
  287. data/test/unit/lookups/google_test.rb +0 -84
  288. data/test/unit/lookups/mapquest_test.rb +0 -60
  289. data/test/unit/lookups/maxmind_local_test.rb +0 -28
  290. data/test/unit/lookups/maxmind_test.rb +0 -63
  291. data/test/unit/lookups/nominatim_test.rb +0 -31
  292. data/test/unit/lookups/okf_test.rb +0 -38
  293. data/test/unit/lookups/opencagedata_test.rb +0 -64
  294. data/test/unit/lookups/pointpin_test.rb +0 -30
  295. data/test/unit/lookups/postcode_anywhere_uk_test.rb +0 -70
  296. data/test/unit/lookups/smarty_streets_test.rb +0 -71
  297. data/test/unit/lookups/telize_test.rb +0 -36
  298. data/test/unit/lookups/yahoo_test.rb +0 -35
  299. data/test/unit/method_aliases_test.rb +0 -26
  300. data/test/unit/model_test.rb +0 -38
  301. data/test/unit/mongoid_test.rb +0 -47
  302. data/test/unit/near_test.rb +0 -87
  303. data/test/unit/oauth_util_test.rb +0 -31
  304. data/test/unit/proxy_test.rb +0 -37
  305. data/test/unit/query_test.rb +0 -52
  306. data/test/unit/rake_task_test.rb +0 -21
  307. data/test/unit/request_test.rb +0 -35
  308. data/test/unit/result_test.rb +0 -72
  309. data/test/unit/test_mode_test.rb +0 -70
@@ -0,0 +1,59 @@
1
+ require 'geocoder/lookups/base'
2
+ require 'geocoder/results/uk_ordnance_survey_names'
3
+
4
+ module Geocoder::Lookup
5
+ class UkOrdnanceSurveyNames < Base
6
+
7
+ def name
8
+ 'Ordance Survey Names'
9
+ end
10
+
11
+ def supported_protocols
12
+ [:https]
13
+ end
14
+
15
+ def base_query_url(query)
16
+ "#{protocol}://api.os.uk/search/names/v1/find?"
17
+ end
18
+
19
+ def required_api_key_parts
20
+ ["key"]
21
+ end
22
+
23
+ def query_url(query)
24
+ base_query_url(query) + url_query_string(query)
25
+ end
26
+
27
+ private # -------------------------------------------------------------
28
+
29
+ def results(query)
30
+ return [] unless doc = fetch_data(query)
31
+ return [] if doc['header']['totalresults'].zero?
32
+ return doc['results'].map { |r| r['GAZETTEER_ENTRY'] }
33
+ end
34
+
35
+ def query_url_params(query)
36
+ {
37
+ query: query.sanitized_text,
38
+ key: configuration.api_key,
39
+ fq: filter
40
+ }.merge(super)
41
+ end
42
+
43
+ def local_types
44
+ %w[
45
+ City
46
+ Hamlet
47
+ Other_Settlement
48
+ Town
49
+ Village
50
+ Postcode
51
+ ]
52
+ end
53
+
54
+ def filter
55
+ local_types.map { |t| "local_type:#{t}" }.join(' ')
56
+ end
57
+
58
+ end
59
+ end
@@ -12,27 +12,30 @@ module Geocoder::Lookup
12
12
  "http://maps.yandex.ru/?ll=#{coordinates.reverse.join(',')}"
13
13
  end
14
14
 
15
- def query_url(query)
16
- "#{protocol}://geocode-maps.yandex.ru/1.x/?" + url_query_string(query)
15
+ def supported_protocols
16
+ [:https]
17
17
  end
18
18
 
19
19
  private # ---------------------------------------------------------------
20
20
 
21
+ def base_query_url(query)
22
+ "#{protocol}://geocode-maps.yandex.ru/1.x/?"
23
+ end
24
+
21
25
  def results(query)
22
26
  return [] unless doc = fetch_data(query)
23
- if err = doc['error']
24
- if err["status"] == 401 and err["message"] == "invalid key"
25
- raise_error(Geocoder::InvalidApiKey) || warn("Invalid API key.")
27
+ if [400, 403].include? doc['statusCode']
28
+ if doc['statusCode'] == 403 and doc['message'] == 'Invalid key'
29
+ raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
26
30
  else
27
- warn "Yandex Geocoding API error: #{err['status']} (#{err['message']})."
31
+ Geocoder.log(:warn, "Yandex Geocoding API error: #{doc['statusCode']} (#{doc['message']}).")
28
32
  end
29
33
  return []
30
34
  end
31
35
  if doc = doc['response']['GeoObjectCollection']
32
- meta = doc['metaDataProperty']['GeocoderResponseMetaData']
33
- return meta['found'].to_i > 0 ? doc['featureMember'] : []
36
+ return doc['featureMember'].to_a
34
37
  else
35
- warn "Yandex Geocoding API error: unexpected response format."
38
+ Geocoder.log(:warn, "Yandex Geocoding API error: unexpected response format.")
36
39
  return []
37
40
  end
38
41
  end
@@ -43,12 +46,16 @@ module Geocoder::Lookup
43
46
  else
44
47
  q = query.sanitized_text
45
48
  end
46
- {
49
+ params = {
47
50
  :geocode => q,
48
51
  :format => "json",
49
- :plng => "#{query.language || configuration.language}", # supports ru, uk, be
50
- :key => configuration.api_key
51
- }.merge(super)
52
+ :lang => "#{query.language || configuration.language}", # supports ru, uk, be, default -> ru
53
+ :apikey => configuration.api_key
54
+ }
55
+ unless (bounds = query.options[:bounds]).nil?
56
+ params[:bbox] = bounds.map{ |point| "%f,%f" % point }.join('~')
57
+ end
58
+ params.merge(super)
52
59
  end
53
60
  end
54
61
  end
@@ -18,7 +18,8 @@ module Geocoder
18
18
  :units => options[:units],
19
19
  :method => options[:method],
20
20
  :lookup => options[:lookup],
21
- :language => options[:language]
21
+ :language => options[:language],
22
+ :params => options[:params]
22
23
  )
23
24
  end
24
25
 
@@ -35,7 +36,8 @@ module Geocoder
35
36
  :units => options[:units],
36
37
  :method => options[:method],
37
38
  :lookup => options[:lookup],
38
- :language => options[:language]
39
+ :language => options[:language],
40
+ :params => options[:params]
39
41
  )
40
42
  end
41
43
 
@@ -47,4 +49,3 @@ module Geocoder
47
49
  end
48
50
  end
49
51
  end
50
-
@@ -1,5 +1,3 @@
1
- require 'geocoder'
2
-
3
1
  module Geocoder
4
2
 
5
3
  ##
@@ -32,7 +32,7 @@ module Geocoder
32
32
  # appropriate to the Query text.
33
33
  #
34
34
  def lookup
35
- if ip_address?
35
+ if !options[:street_address] and (options[:ip_address] or ip_address?)
36
36
  name = options[:ip_lookup] || Configuration.ip_lookup || Geocoder::Lookup.ip_services.first
37
37
  else
38
38
  name = options[:lookup] || Configuration.lookup || Geocoder::Lookup.street_services.first
@@ -66,6 +66,13 @@ module Geocoder
66
66
  IpAddress.new(text).valid? rescue false
67
67
  end
68
68
 
69
+ ##
70
+ # Is the Query text a loopback or private IP address?
71
+ #
72
+ def internal_ip_address?
73
+ ip_address? && IpAddress.new(text).internal?
74
+ end
75
+
69
76
  ##
70
77
  # Is the Query text a loopback IP address?
71
78
  #
@@ -73,6 +80,13 @@ module Geocoder
73
80
  ip_address? && IpAddress.new(text).loopback?
74
81
  end
75
82
 
83
+ ##
84
+ # Is the Query text a private IP address?
85
+ #
86
+ def private_ip_address?
87
+ ip_address? && IpAddress.new(text).private?
88
+ end
89
+
76
90
  ##
77
91
  # Does the given string look like latitude/longitude coordinates?
78
92
  #
@@ -4,7 +4,7 @@ module Geocoder
4
4
  if defined? Rails::Railtie
5
5
  require 'rails'
6
6
  class Railtie < Rails::Railtie
7
- initializer 'geocoder.insert_into_active_record' do
7
+ initializer 'geocoder.insert_into_active_record', before: :load_config_initializers do
8
8
  ActiveSupport.on_load :active_record do
9
9
  Geocoder::Railtie.insert
10
10
  end
@@ -1,25 +1,114 @@
1
+ require 'ipaddr'
2
+
1
3
  module Geocoder
2
4
  module Request
3
5
 
6
+ # The location() method is vulnerable to trivial IP spoofing.
7
+ # Don't use it in authorization/authentication code, or any
8
+ # other security-sensitive application. Use safe_location
9
+ # instead.
4
10
  def location
5
- @location ||= begin
6
- detected_ip = env['HTTP_X_REAL_IP'] || (
7
- env['HTTP_X_FORWARDED_FOR'] &&
8
- env['HTTP_X_FORWARDED_FOR'].split(",").first.strip
9
- )
10
- detected_ip = IpAddress.new(detected_ip.to_s)
11
- if detected_ip.valid? and !detected_ip.loopback?
12
- real_ip = detected_ip.to_s
11
+ @location ||= Geocoder.search(geocoder_spoofable_ip, ip_address: true).first
12
+ end
13
+
14
+ # This safe_location() protects you from trivial IP spoofing.
15
+ # For requests that go through a proxy that you haven't
16
+ # whitelisted as trusted in your Rack config, you will get the
17
+ # location for the IP of the last untrusted proxy in the chain,
18
+ # not the original client IP. You WILL NOT get the location
19
+ # corresponding to the original client IP for any request sent
20
+ # through a non-whitelisted proxy.
21
+ def safe_location
22
+ @safe_location ||= Geocoder.search(ip, ip_address: true).first
23
+ end
24
+
25
+ # There's a whole zoo of nonstandard headers added by various
26
+ # proxy softwares to indicate original client IP.
27
+ # ANY of these can be trivially spoofed!
28
+ # (except REMOTE_ADDR, which should by set by your server,
29
+ # and is included at the end as a fallback.
30
+ # Order does matter: we're following the convention established in
31
+ # ActionDispatch::RemoteIp::GetIp::calculate_ip()
32
+ # https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/remote_ip.rb
33
+ # where the forwarded_for headers, possibly containing lists,
34
+ # are arbitrarily preferred over headers expected to contain a
35
+ # single address.
36
+ GEOCODER_CANDIDATE_HEADERS = ['HTTP_X_FORWARDED_FOR',
37
+ 'HTTP_X_FORWARDED',
38
+ 'HTTP_FORWARDED_FOR',
39
+ 'HTTP_FORWARDED',
40
+ 'HTTP_X_CLIENT_IP',
41
+ 'HTTP_CLIENT_IP',
42
+ 'HTTP_X_REAL_IP',
43
+ 'HTTP_X_CLUSTER_CLIENT_IP',
44
+ 'REMOTE_ADDR']
45
+
46
+ def geocoder_spoofable_ip
47
+
48
+ # We could use a more sophisticated IP-guessing algorithm here,
49
+ # in which we'd try to resolve the use of different headers by
50
+ # different proxies. The idea is that by comparing IPs repeated
51
+ # in different headers, you can sometimes decide which header
52
+ # was used by a proxy further along in the chain, and thus
53
+ # prefer the headers used earlier. However, the gains might not
54
+ # be worth the performance tradeoff, since this method is likely
55
+ # to be called on every request in a lot of applications.
56
+ GEOCODER_CANDIDATE_HEADERS.each do |header|
57
+ if @env.has_key? header
58
+ addrs = geocoder_split_ip_addresses(@env[header])
59
+ addrs = geocoder_remove_port_from_addresses(addrs)
60
+ addrs = geocoder_reject_non_ipv4_addresses(addrs)
61
+ addrs = geocoder_reject_trusted_ip_addresses(addrs)
62
+ return addrs.first if addrs.any?
63
+ end
64
+ end
65
+
66
+ @env['REMOTE_ADDR']
67
+ end
68
+
69
+ private
70
+
71
+ def geocoder_split_ip_addresses(ip_addresses)
72
+ ip_addresses ? ip_addresses.strip.split(/[,\s]+/) : []
73
+ end
74
+
75
+ # use Rack's trusted_proxy?() method to filter out IPs that have
76
+ # been configured as trusted; includes private ranges by
77
+ # default. (we don't want every lookup to return the location
78
+ # of our own proxy/load balancer)
79
+ def geocoder_reject_trusted_ip_addresses(ip_addresses)
80
+ ip_addresses.reject { |ip| trusted_proxy?(ip) }
81
+ end
82
+
83
+ def geocoder_remove_port_from_addresses(ip_addresses)
84
+ ip_addresses.map do |ip|
85
+ # IPv4
86
+ if ip.count('.') > 0
87
+ ip.split(':').first
88
+ # IPv6 bracket notation
89
+ elsif match = ip.match(/\[(\S+)\]/)
90
+ match.captures.first
91
+ # IPv6 bare notation
13
92
  else
14
- real_ip = self.ip
93
+ ip
15
94
  end
16
- Geocoder.search(real_ip).first
17
95
  end
18
- @location
96
+ end
97
+
98
+ def geocoder_reject_non_ipv4_addresses(ip_addresses)
99
+ ips = []
100
+ for ip in ip_addresses
101
+ begin
102
+ valid_ip = IPAddr.new(ip)
103
+ rescue
104
+ valid_ip = false
105
+ end
106
+ ips << valid_ip.to_s if valid_ip
107
+ end
108
+ return ips.any? ? ips : ip_addresses
19
109
  end
20
110
  end
21
111
  end
22
112
 
23
- if defined?(Rack) and defined?(Rack::Request)
24
- Rack::Request.send :include, Geocoder::Request
25
- end
113
+ ActionDispatch::Request.__send__(:include, Geocoder::Request) if defined?(ActionDispatch::Request)
114
+ Rack::Request.__send__(:include, Geocoder::Request) if defined?(Rack::Request)
@@ -0,0 +1,146 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder
4
+ module Result
5
+ class AbstractApi < Base
6
+
7
+ ##
8
+ # Geolocation
9
+
10
+ def state
11
+ @data['region']
12
+ end
13
+
14
+ def state_code
15
+ @data['region_iso_code']
16
+ end
17
+
18
+ def city
19
+ @data["city"]
20
+ end
21
+
22
+ def city_geoname_id
23
+ @data["city_geoname_id"]
24
+ end
25
+
26
+ def region_geoname_id
27
+ @data["region_geoname_id"]
28
+ end
29
+
30
+ def postal_code
31
+ @data["postal_code"]
32
+ end
33
+
34
+ def country
35
+ @data["country"]
36
+ end
37
+
38
+ def country_code
39
+ @data["country_code"]
40
+ end
41
+
42
+ def country_geoname_id
43
+ @data["country_geoname_id"]
44
+ end
45
+
46
+ def country_is_eu
47
+ @data["country_is_eu"]
48
+ end
49
+
50
+ def continent
51
+ @data["continent"]
52
+ end
53
+
54
+ def continent_code
55
+ @data["continent_code"]
56
+ end
57
+
58
+ def continent_geoname_id
59
+ @data["continent_geoname_id"]
60
+ end
61
+
62
+ ##
63
+ # Security
64
+
65
+ def is_vpn?
66
+ @data.dig "security", "is_vpn"
67
+ end
68
+
69
+ ##
70
+ # Timezone
71
+
72
+ def timezone_name
73
+ @data.dig "timezone", "name"
74
+ end
75
+
76
+ def timezone_abbreviation
77
+ @data.dig "timezone", "abbreviation"
78
+ end
79
+
80
+ def timezone_gmt_offset
81
+ @data.dig "timezone", "gmt_offset"
82
+ end
83
+
84
+ def timezone_current_time
85
+ @data.dig "timezone", "current_time"
86
+ end
87
+
88
+ def timezone_is_dst
89
+ @data.dig "timezone", "is_dst"
90
+ end
91
+
92
+ ##
93
+ # Flag
94
+
95
+ def flag_emoji
96
+ @data.dig "flag", "emoji"
97
+ end
98
+
99
+ def flag_unicode
100
+ @data.dig "flag", "unicode"
101
+ end
102
+
103
+ def flag_png
104
+ @data.dig "flag", "png"
105
+ end
106
+
107
+ def flag_svg
108
+ @data.dig "flag", "svg"
109
+ end
110
+
111
+ ##
112
+ # Currency
113
+
114
+ def currency_currency_name
115
+ @data.dig "currency", "currency_name"
116
+ end
117
+
118
+ def currency_currency_code
119
+ @data.dig "currency", "currency_code"
120
+ end
121
+
122
+ ##
123
+ # Connection
124
+
125
+ def connection_autonomous_system_number
126
+ @data.dig "connection", "autonomous_system_number"
127
+ end
128
+
129
+ def connection_autonomous_system_organization
130
+ @data.dig "connection", "autonomous_system_organization"
131
+ end
132
+
133
+ def connection_connection_type
134
+ @data.dig "connection", "connection_type"
135
+ end
136
+
137
+ def connection_isp_name
138
+ @data.dig "connection", "isp_name"
139
+ end
140
+
141
+ def connection_organization_name
142
+ @data.dig "connection", "organization_name"
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,87 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Amap < Base
5
+
6
+ def coordinates
7
+ location = @data['location'] || @data['roadinters'].try(:first).try(:[], 'location') \
8
+ || address_components.try(:[], 'streetNumber').try(:[], 'location')
9
+ location.to_s.split(",").reverse.map(&:to_f)
10
+ end
11
+
12
+ def address
13
+ formatted_address
14
+ end
15
+
16
+ def state
17
+ province
18
+ end
19
+
20
+ def province
21
+ address_components['province']
22
+ end
23
+
24
+ def city
25
+ address_components['city'] == [] ? province : address_components["city"]
26
+ end
27
+
28
+ def district
29
+ address_components['district']
30
+ end
31
+
32
+ def street
33
+ if address_components["neighborhood"]["name"] != []
34
+ return address_components["neighborhood"]["name"]
35
+ elsif address_components['township'] != []
36
+ return address_components["township"]
37
+ else
38
+ return @data['street'] || address_components['streetNumber'].try(:[], 'street')
39
+ end
40
+ end
41
+
42
+ def street_number
43
+ @data['number'] || address_components['streetNumber'].try(:[], 'number')
44
+ end
45
+
46
+ def formatted_address
47
+ @data['formatted_address']
48
+ end
49
+
50
+ def address_components
51
+ @data['addressComponent'] || @data
52
+ end
53
+
54
+ def state_code
55
+ ""
56
+ end
57
+
58
+ def postal_code
59
+ ""
60
+ end
61
+
62
+ def country
63
+ "China"
64
+ end
65
+
66
+ def country_code
67
+ "CN"
68
+ end
69
+
70
+ ##
71
+ # Get address components of a given type. Valid types are defined in
72
+ # Baidu's Geocoding API documentation and include (among others):
73
+ #
74
+ # :business
75
+ # :cityCode
76
+ #
77
+ def self.response_attributes
78
+ %w[roads pois roadinters]
79
+ end
80
+
81
+ response_attributes.each do |a|
82
+ define_method a do
83
+ @data[a]
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,57 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class AmazonLocationService < Base
5
+ def initialize(result)
6
+ @place = result
7
+ end
8
+
9
+ def coordinates
10
+ [@place.geometry.point[1], @place.geometry.point[0]]
11
+ end
12
+
13
+ def address
14
+ @place.label
15
+ end
16
+
17
+ def neighborhood
18
+ @place.neighborhood
19
+ end
20
+
21
+ def route
22
+ @place.street
23
+ end
24
+
25
+ def city
26
+ @place.municipality || @place.sub_region
27
+ end
28
+
29
+ def state
30
+ @place.region
31
+ end
32
+
33
+ def state_code
34
+ @place.region
35
+ end
36
+
37
+ def province
38
+ @place.region
39
+ end
40
+
41
+ def province_code
42
+ @place.region
43
+ end
44
+
45
+ def postal_code
46
+ @place.postal_code
47
+ end
48
+
49
+ def country
50
+ @place.country
51
+ end
52
+
53
+ def country_code
54
+ @place.country
55
+ end
56
+ end
57
+ end
@@ -7,38 +7,34 @@ module Geocoder::Result
7
7
  ['lat', 'lng'].map{ |i| @data['location'][i] }
8
8
  end
9
9
 
10
- def address
11
- @data['formatted_address']
12
- end
13
-
14
- def state
15
- province
16
- end
17
-
18
10
  def province
19
- @data['addressComponent']['province']
11
+ @data['addressComponent'] and @data['addressComponent']['province'] or ""
20
12
  end
21
13
 
14
+ alias_method :state, :province
15
+
22
16
  def city
23
- @data['addressComponent']['city']
17
+ @data['addressComponent'] and @data['addressComponent']['city'] or ""
24
18
  end
25
19
 
26
20
  def district
27
- @data['addressComponent']['district']
21
+ @data['addressComponent'] and @data['addressComponent']['district'] or ""
28
22
  end
29
23
 
30
24
  def street
31
- @data['addressComponent']['street']
25
+ @data['addressComponent'] and @data['addressComponent']['street'] or ""
32
26
  end
33
27
 
34
28
  def street_number
35
- @data['addressComponent']['street_number']
29
+ @data['addressComponent'] and @data['addressComponent']['street_number']
36
30
  end
37
31
 
38
32
  def formatted_address
39
- @data['formatted_address']
33
+ @data['formatted_address'] or ""
40
34
  end
41
35
 
36
+ alias_method :address, :formatted_address
37
+
42
38
  def address_components
43
39
  @data['addressComponent']
44
40
  end