geocoder 1.4.3 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +64 -0
  3. data/LICENSE +1 -1
  4. data/README.md +365 -883
  5. data/examples/autoexpire_cache_redis.rb +5 -3
  6. data/lib/generators/geocoder/config/templates/initializer.rb +3 -2
  7. data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +2 -0
  8. data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +2 -0
  9. data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +1 -1
  10. data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +1 -1
  11. data/lib/generators/geocoder/migration_version.rb +15 -0
  12. data/lib/geocoder/cache.rb +6 -2
  13. data/lib/geocoder/calculations.rb +1 -1
  14. data/lib/geocoder/cli.rb +2 -2
  15. data/lib/geocoder/configuration.rb +2 -2
  16. data/lib/geocoder/exceptions.rb +1 -1
  17. data/lib/geocoder/ip_address.rb +14 -1
  18. data/lib/geocoder/lookup.rb +13 -6
  19. data/lib/geocoder/lookups/amap.rb +63 -0
  20. data/lib/geocoder/lookups/baidu.rb +14 -10
  21. data/lib/geocoder/lookups/baidu_ip.rb +7 -36
  22. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +17 -4
  23. data/lib/geocoder/lookups/base.rb +28 -5
  24. data/lib/geocoder/lookups/bing.rb +15 -13
  25. data/lib/geocoder/lookups/db_ip_com.rb +52 -0
  26. data/lib/geocoder/lookups/dstk.rb +4 -2
  27. data/lib/geocoder/lookups/esri.rb +39 -29
  28. data/lib/geocoder/lookups/freegeoip.rb +16 -7
  29. data/lib/geocoder/lookups/geocoder_ca.rb +4 -4
  30. data/lib/geocoder/lookups/geocodio.rb +5 -5
  31. data/lib/geocoder/lookups/geoportail_lu.rb +7 -7
  32. data/lib/geocoder/lookups/google.rb +13 -9
  33. data/lib/geocoder/lookups/google_places_details.rb +4 -4
  34. data/lib/geocoder/lookups/google_places_search.rb +4 -4
  35. data/lib/geocoder/lookups/google_premier.rb +11 -1
  36. data/lib/geocoder/lookups/here.rb +29 -23
  37. data/lib/geocoder/lookups/ip2location.rb +67 -0
  38. data/lib/geocoder/lookups/ipapi_com.rb +9 -13
  39. data/lib/geocoder/lookups/ipdata_co.rb +62 -0
  40. data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  41. data/lib/geocoder/lookups/ipinfo_io.rb +11 -29
  42. data/lib/geocoder/lookups/ipregistry.rb +68 -0
  43. data/lib/geocoder/lookups/ipstack.rb +63 -0
  44. data/lib/geocoder/lookups/latlon.rb +4 -4
  45. data/lib/geocoder/lookups/location_iq.rb +26 -8
  46. data/lib/geocoder/lookups/mapbox.rb +12 -6
  47. data/lib/geocoder/lookups/mapquest.rb +4 -5
  48. data/lib/geocoder/lookups/maxmind.rb +6 -6
  49. data/lib/geocoder/lookups/maxmind_geoip2.rb +8 -7
  50. data/lib/geocoder/lookups/nominatim.rb +17 -5
  51. data/lib/geocoder/lookups/opencagedata.rb +7 -6
  52. data/lib/geocoder/lookups/osmnames.rb +57 -0
  53. data/lib/geocoder/lookups/pelias.rb +8 -9
  54. data/lib/geocoder/lookups/pickpoint.rb +41 -0
  55. data/lib/geocoder/lookups/pointpin.rb +10 -9
  56. data/lib/geocoder/lookups/postcode_anywhere_uk.rb +4 -5
  57. data/lib/geocoder/lookups/postcodes_io.rb +31 -0
  58. data/lib/geocoder/lookups/smarty_streets.rb +20 -10
  59. data/lib/geocoder/lookups/telize.rb +26 -6
  60. data/lib/geocoder/lookups/tencent.rb +59 -0
  61. data/lib/geocoder/lookups/yandex.rb +12 -8
  62. data/lib/geocoder/models/active_record.rb +4 -3
  63. data/lib/geocoder/query.rb +14 -0
  64. data/lib/geocoder/railtie.rb +1 -1
  65. data/lib/geocoder/request.rb +32 -0
  66. data/lib/geocoder/results/amap.rb +87 -0
  67. data/lib/geocoder/results/baidu.rb +10 -14
  68. data/lib/geocoder/results/ban_data_gouv_fr.rb +1 -1
  69. data/lib/geocoder/results/base.rb +13 -1
  70. data/lib/geocoder/results/bing.rb +1 -1
  71. data/lib/geocoder/results/db_ip_com.rb +58 -0
  72. data/lib/geocoder/results/freegeoip.rb +0 -5
  73. data/lib/geocoder/results/geocoder_ca.rb +3 -3
  74. data/lib/geocoder/results/geoip2.rb +24 -10
  75. data/lib/geocoder/results/geoportail_lu.rb +5 -3
  76. data/lib/geocoder/results/google.rb +16 -5
  77. data/lib/geocoder/results/here.rb +12 -1
  78. data/lib/geocoder/results/ip2location.rb +22 -0
  79. data/lib/geocoder/results/ipdata_co.rb +40 -0
  80. data/lib/geocoder/results/ipgeolocation.rb +59 -0
  81. data/lib/geocoder/results/ipregistry.rb +308 -0
  82. data/lib/geocoder/results/ipstack.rb +60 -0
  83. data/lib/geocoder/results/maxmind.rb +0 -5
  84. data/lib/geocoder/results/maxmind_local.rb +0 -5
  85. data/lib/geocoder/results/nominatim.rb +12 -0
  86. data/lib/geocoder/results/opencagedata.rb +12 -2
  87. data/lib/geocoder/results/osmnames.rb +56 -0
  88. data/lib/geocoder/results/pickpoint.rb +6 -0
  89. data/lib/geocoder/results/postcodes_io.rb +40 -0
  90. data/lib/geocoder/results/smarty_streets.rb +55 -19
  91. data/lib/geocoder/results/telize.rb +0 -5
  92. data/lib/geocoder/results/tencent.rb +72 -0
  93. data/lib/geocoder/results/test.rb +1 -1
  94. data/lib/geocoder/sql.rb +4 -4
  95. data/lib/geocoder/stores/active_record.rb +16 -5
  96. data/lib/geocoder/stores/base.rb +1 -2
  97. data/lib/geocoder/version.rb +1 -1
  98. data/lib/hash_recursive_merge.rb +1 -2
  99. data/lib/maxmind_database.rb +3 -3
  100. data/lib/tasks/geocoder.rake +11 -3
  101. metadata +30 -14
  102. data/lib/geocoder/lookups/geocoder_us.rb +0 -43
  103. data/lib/geocoder/lookups/mapzen.rb +0 -15
  104. data/lib/geocoder/lookups/okf.rb +0 -44
  105. data/lib/geocoder/lookups/ovi.rb +0 -62
  106. data/lib/geocoder/results/geocoder_us.rb +0 -39
  107. data/lib/geocoder/results/mapzen.rb +0 -5
  108. data/lib/geocoder/results/okf.rb +0 -106
  109. data/lib/geocoder/results/ovi.rb +0 -71
@@ -1,43 +0,0 @@
1
- require 'geocoder/lookups/base'
2
- require "geocoder/results/geocoder_us"
3
-
4
- module Geocoder::Lookup
5
- class GeocoderUs < Base
6
-
7
- def name
8
- "Geocoder.us"
9
- end
10
-
11
- def supported_protocols
12
- [:http]
13
- end
14
-
15
- def query_url(query)
16
- if configuration.api_key
17
- "#{protocol}://#{configuration.api_key}@geocoder.us/member/service/csv/geocode?" + url_query_string(query)
18
- else
19
- "#{protocol}://geocoder.us/service/csv/geocode?" + url_query_string(query)
20
- end
21
- end
22
-
23
- private
24
-
25
- def results(query)
26
- return [] unless doc = fetch_data(query)
27
- if doc[0].to_s =~ /^(\d+)\:/
28
- return []
29
- else
30
- return [doc.size == 5 ? ((doc[0..1] << nil) + doc[2..4]) : doc]
31
- end
32
- end
33
-
34
- def query_url_params(query)
35
- (query.text =~ /^\d{5}(?:-\d{4})?$/ ? {:zip => query} : {:address => query.sanitized_text}).merge(super)
36
- end
37
-
38
- def parse_raw_data(raw_data)
39
- raw_data.chomp.split(',')
40
- end
41
- end
42
- end
43
-
@@ -1,15 +0,0 @@
1
- require 'geocoder/lookups/pelias'
2
- require 'geocoder/results/mapzen'
3
-
4
- # https://mapzen.com/documentation/search/search/ for more information
5
- module Geocoder::Lookup
6
- class Mapzen < Pelias
7
- def name
8
- 'Mapzen'
9
- end
10
-
11
- def endpoint
12
- configuration[:endpoint] || 'search.mapzen.com'
13
- end
14
- end
15
- end
@@ -1,44 +0,0 @@
1
- require 'geocoder/lookups/base'
2
- require "geocoder/results/okf"
3
-
4
- module Geocoder::Lookup
5
- class Okf < Base
6
-
7
- def name
8
- "Okf"
9
- end
10
-
11
- def query_url(query)
12
- "#{protocol}://data.okf.fi/gis/1/geocode/json?" + url_query_string(query)
13
- end
14
-
15
- private # ---------------------------------------------------------------
16
-
17
- def valid_response?(response)
18
- json = parse_json(response.body)
19
- status = json["status"] if json
20
- super(response) and ['OK', 'ZERO_RESULTS'].include?(status)
21
- end
22
-
23
- def results(query)
24
- return [] unless doc = fetch_data(query)
25
- case doc['status']; when "OK" # OK status implies >0 results
26
- return doc['results']
27
- end
28
- return []
29
- end
30
-
31
- def query_url_okf_params(query)
32
- params = {
33
- (query.reverse_geocode? ? :latlng : :address) => query.sanitized_text,
34
- :sensor => "false",
35
- :language => (query.language || configuration.language)
36
- }
37
- params
38
- end
39
-
40
- def query_url_params(query)
41
- query_url_okf_params(query).merge(super)
42
- end
43
- end
44
- end
@@ -1,62 +0,0 @@
1
- require 'geocoder/lookups/base'
2
- require 'geocoder/results/ovi'
3
-
4
- module Geocoder::Lookup
5
- class Ovi < Base
6
-
7
- def name
8
- "Ovi"
9
- end
10
-
11
- def required_api_key_parts
12
- []
13
- end
14
-
15
- def query_url(query)
16
- "#{protocol}://lbs.ovi.com/search/6.2/#{if query.reverse_geocode? then 'reverse' end}geocode.json?" + url_query_string(query)
17
- end
18
-
19
- private # ---------------------------------------------------------------
20
-
21
- def results(query)
22
- return [] unless doc = fetch_data(query)
23
- return [] unless doc['Response'] && doc['Response']['View']
24
- if r=doc['Response']['View']
25
- return [] if r.nil? || !r.is_a?(Array) || r.empty?
26
- return r.first['Result']
27
- end
28
- []
29
- end
30
-
31
- def query_url_params(query)
32
- options = {
33
- :gen=>1,
34
- :app_id=>api_key,
35
- :app_code=>api_code
36
- }
37
-
38
- if query.reverse_geocode?
39
- super.merge(options).merge(
40
- :prox=>query.sanitized_text,
41
- :mode=>:retrieveAddresses
42
- )
43
- else
44
- super.merge(options).merge(
45
- :searchtext=>query.sanitized_text
46
- )
47
- end
48
- end
49
-
50
- def api_key
51
- if a=configuration.api_key
52
- return a.first if a.is_a?(Array)
53
- end
54
- end
55
-
56
- def api_code
57
- if a=configuration.api_key
58
- return a.last if a.is_a?(Array)
59
- end
60
- end
61
- end
62
- end
@@ -1,39 +0,0 @@
1
- require 'geocoder/results/base'
2
-
3
- module Geocoder::Result
4
- class GeocoderUs < Base
5
- def coordinates
6
- [@data[0].to_f, @data[1].to_f]
7
- end
8
-
9
- def address(format = :full)
10
- "#{street_address}, #{city}, #{state} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
11
- end
12
-
13
- def street_address
14
- @data[2]
15
- end
16
-
17
- def city
18
- @data[3]
19
- end
20
-
21
- def state
22
- @data[4]
23
- end
24
-
25
- alias_method :state_code, :state
26
-
27
- def postal_code
28
- @data[5]
29
- end
30
-
31
- def country
32
- 'United States'
33
- end
34
-
35
- def country_code
36
- 'US'
37
- end
38
- end
39
- end
@@ -1,5 +0,0 @@
1
- require 'geocoder/results/pelias'
2
-
3
- module Geocoder::Result
4
- class Mapzen < Pelias; end
5
- end
@@ -1,106 +0,0 @@
1
- require 'geocoder/results/base'
2
-
3
- module Geocoder::Result
4
- class Okf < Base
5
-
6
- def coordinates
7
- ['lat', 'lng'].map{ |i| geometry['location'][i] }
8
- end
9
-
10
- def address(format = :full)
11
- formatted_address
12
- end
13
-
14
- def city
15
- fields = [:locality, :sublocality,
16
- :administrative_area_level_3,
17
- :administrative_area_level_2]
18
- fields.each do |f|
19
- if entity = address_components_of_type(f).first
20
- return entity['long_name']
21
- end
22
- end
23
- return nil # no appropriate components found
24
- end
25
-
26
- def state
27
- ""
28
- end
29
-
30
- def sub_state
31
- ""
32
- end
33
-
34
- def state_code
35
- ""
36
- end
37
-
38
- def country
39
- if country = address_components_of_type(:country).first
40
- country['long_name']
41
- end
42
- end
43
-
44
- def country_code
45
- if country = address_components_of_type(:country).first
46
- country['short_name']
47
- end
48
- end
49
-
50
- def postal_code
51
- if postal = address_components_of_type(:postal_code).first
52
- postal['long_name']
53
- end
54
- end
55
-
56
- def route
57
- if route = address_components_of_type(:route).first
58
- route['long_name']
59
- end
60
- end
61
-
62
- def street_number
63
- if street_number = address_components_of_type(:street_number).first
64
- street_number['long_name']
65
- end
66
- end
67
-
68
- def street_address
69
- [route, street_number].compact.join(' ')
70
- end
71
-
72
- def types
73
- @data['types']
74
- end
75
-
76
- def formatted_address
77
- @data['formatted_address']
78
- end
79
-
80
- def address_components
81
- @data['address_components']
82
- end
83
-
84
- ##
85
- # Get address components of a given type. Valid types are defined in
86
- # Google's Geocoding API documentation and include (among others):
87
- #
88
- # :street_number
89
- # :locality
90
- # :neighborhood
91
- # :route
92
- # :postal_code
93
- #
94
- def address_components_of_type(type)
95
- address_components.select{ |c| c['types'].include?(type.to_s) }
96
- end
97
-
98
- def geometry
99
- @data['geometry']
100
- end
101
-
102
- def precision
103
- geometry['location_type'] if geometry
104
- end
105
- end
106
- end
@@ -1,71 +0,0 @@
1
- require 'geocoder/results/base'
2
-
3
- module Geocoder::Result
4
- class Ovi < Base
5
-
6
- ##
7
- # A string in the given format.
8
- #
9
- def address(format = :full)
10
- address_data['Label']
11
- end
12
-
13
- ##
14
- # A two-element array: [lat, lon].
15
- #
16
- def coordinates
17
- fail unless d = @data['Location']['DisplayPosition']
18
- [d['Latitude'].to_f, d['Longitude'].to_f]
19
- end
20
-
21
- def state
22
- address_data['County']
23
- end
24
-
25
- def province
26
- address_data['County']
27
- end
28
-
29
- def postal_code
30
- address_data['PostalCode']
31
- end
32
-
33
- def city
34
- address_data['City']
35
- end
36
-
37
- def state_code
38
- address_data['State']
39
- end
40
-
41
- def province_code
42
- address_data['State']
43
- end
44
-
45
- def country
46
- fail unless d = address_data['AdditionalData']
47
- if v = d.find{|ad| ad['key']=='CountryName'}
48
- return v['value']
49
- end
50
- end
51
-
52
- def country_code
53
- address_data['Country']
54
- end
55
-
56
- def viewport
57
- map_view = data['Location']['MapView'] || fail
58
- south = map_view['BottomRight']['Latitude']
59
- west = map_view['TopLeft']['Longitude']
60
- north = map_view['TopLeft']['Latitude']
61
- east = map_view['BottomRight']['Longitude']
62
- [south, west, north, east]
63
- end
64
-
65
- private # ----------------------------------------------------------------
66
-
67
- def address_data
68
- @data['Location']['Address'] || fail
69
- end
70
- end
71
- end