geocoder 1.2.7 → 1.2.8

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.

Potentially problematic release.


This version of geocoder might be problematic. Click here for more details.

Files changed (180) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -1
  3. data/README.md +31 -28
  4. data/examples/reverse_geocode_job.rb +40 -0
  5. data/lib/geocoder.rb +1 -0
  6. data/lib/geocoder/configuration.rb +5 -1
  7. data/lib/geocoder/exceptions.rb +3 -0
  8. data/lib/geocoder/ip_address.rb +2 -10
  9. data/lib/geocoder/logger.rb +55 -0
  10. data/lib/geocoder/lookup.rb +2 -1
  11. data/lib/geocoder/lookups/baidu.rb +5 -5
  12. data/lib/geocoder/lookups/baidu_ip.rb +5 -5
  13. data/lib/geocoder/lookups/base.rb +21 -16
  14. data/lib/geocoder/lookups/bing.rb +15 -2
  15. data/lib/geocoder/lookups/esri.rb +1 -1
  16. data/lib/geocoder/lookups/geocoder_ca.rb +1 -2
  17. data/lib/geocoder/lookups/geocodio.rb +3 -3
  18. data/lib/geocoder/lookups/google.rb +3 -3
  19. data/lib/geocoder/lookups/google_places_details.rb +3 -3
  20. data/lib/geocoder/lookups/mapquest.rb +3 -3
  21. data/lib/geocoder/lookups/maxmind.rb +1 -1
  22. data/lib/geocoder/lookups/maxmind_geoip2.rb +69 -0
  23. data/lib/geocoder/lookups/nominatim.rb +1 -1
  24. data/lib/geocoder/lookups/opencagedata.rb +4 -4
  25. data/lib/geocoder/lookups/pointpin.rb +4 -4
  26. data/lib/geocoder/lookups/postcode_anywhere_uk.rb +3 -3
  27. data/lib/geocoder/lookups/yahoo.rb +3 -3
  28. data/lib/geocoder/lookups/yandex.rb +3 -3
  29. data/lib/geocoder/query.rb +1 -1
  30. data/lib/geocoder/request.rb +70 -12
  31. data/lib/geocoder/results/geoip2.rb +8 -14
  32. data/lib/geocoder/results/maxmind_geoip2.rb +9 -0
  33. data/lib/geocoder/results/yandex.rb +5 -5
  34. data/lib/geocoder/version.rb +1 -1
  35. metadata +6 -147
  36. data/.gitignore +0 -6
  37. data/.travis.yml +0 -32
  38. data/Rakefile +0 -25
  39. data/examples/sidekiq_worker.rb +0 -16
  40. data/gemfiles/Gemfile.mongoid-2.4.x +0 -16
  41. data/test/fixtures/baidu_invalid_key +0 -1
  42. data/test/fixtures/baidu_ip_202_198_16_3 +0 -19
  43. data/test/fixtures/baidu_ip_invalid_key +0 -1
  44. data/test/fixtures/baidu_ip_no_results +0 -1
  45. data/test/fixtures/baidu_no_results +0 -1
  46. data/test/fixtures/baidu_reverse +0 -1
  47. data/test/fixtures/baidu_shanghai_pearl_tower +0 -12
  48. data/test/fixtures/bing_invalid_key +0 -1
  49. data/test/fixtures/bing_madison_square_garden +0 -40
  50. data/test/fixtures/bing_no_results +0 -16
  51. data/test/fixtures/bing_reverse +0 -42
  52. data/test/fixtures/cloudmade_invalid_key +0 -1
  53. data/test/fixtures/cloudmade_madison_square_garden +0 -1
  54. data/test/fixtures/cloudmade_no_results +0 -1
  55. data/test/fixtures/esri_madison_square_garden +0 -59
  56. data/test/fixtures/esri_no_results +0 -8
  57. data/test/fixtures/esri_reverse +0 -21
  58. data/test/fixtures/freegeoip_74_200_247_59 +0 -12
  59. data/test/fixtures/freegeoip_no_results +0 -1
  60. data/test/fixtures/geocoder_ca_madison_square_garden +0 -1
  61. data/test/fixtures/geocoder_ca_no_results +0 -1
  62. data/test/fixtures/geocoder_ca_reverse +0 -34
  63. data/test/fixtures/geocoder_us_madison_square_garden +0 -1
  64. data/test/fixtures/geocoder_us_no_results +0 -1
  65. data/test/fixtures/geocodio_1101_pennsylvania_ave +0 -1
  66. data/test/fixtures/geocodio_bad_api_key +0 -3
  67. data/test/fixtures/geocodio_invalid +0 -4
  68. data/test/fixtures/geocodio_no_results +0 -1
  69. data/test/fixtures/geocodio_over_query_limit +0 -4
  70. data/test/fixtures/google_garbage +0 -456
  71. data/test/fixtures/google_madison_square_garden +0 -57
  72. data/test/fixtures/google_no_city_data +0 -44
  73. data/test/fixtures/google_no_locality +0 -51
  74. data/test/fixtures/google_no_results +0 -4
  75. data/test/fixtures/google_over_limit +0 -4
  76. data/test/fixtures/google_places_details_invalid_request +0 -4
  77. data/test/fixtures/google_places_details_madison_square_garden +0 -120
  78. data/test/fixtures/google_places_details_no_results +0 -4
  79. data/test/fixtures/google_places_details_no_reviews +0 -60
  80. data/test/fixtures/google_places_details_no_types +0 -66
  81. data/test/fixtures/here_madison_square_garden +0 -72
  82. data/test/fixtures/here_no_results +0 -8
  83. data/test/fixtures/mapquest_error +0 -16
  84. data/test/fixtures/mapquest_invalid_api_key +0 -16
  85. data/test/fixtures/mapquest_invalid_request +0 -16
  86. data/test/fixtures/mapquest_madison_square_garden +0 -52
  87. data/test/fixtures/mapquest_no_results +0 -16
  88. data/test/fixtures/maxmind_24_24_24_21 +0 -1
  89. data/test/fixtures/maxmind_24_24_24_22 +0 -1
  90. data/test/fixtures/maxmind_24_24_24_23 +0 -1
  91. data/test/fixtures/maxmind_24_24_24_24 +0 -1
  92. data/test/fixtures/maxmind_74_200_247_59 +0 -1
  93. data/test/fixtures/maxmind_invalid_key +0 -1
  94. data/test/fixtures/maxmind_no_results +0 -1
  95. data/test/fixtures/nominatim_madison_square_garden +0 -150
  96. data/test/fixtures/nominatim_no_results +0 -1
  97. data/test/fixtures/nominatim_over_limit +0 -1
  98. data/test/fixtures/okf_kirstinmaki +0 -67
  99. data/test/fixtures/okf_no_results +0 -4
  100. data/test/fixtures/opencagedata_invalid_api_key +0 -25
  101. data/test/fixtures/opencagedata_invalid_request +0 -26
  102. data/test/fixtures/opencagedata_madison_square_garden +0 -73
  103. data/test/fixtures/opencagedata_no_results +0 -29
  104. data/test/fixtures/opencagedata_over_limit +0 -31
  105. data/test/fixtures/ovi_madison_square_garden +0 -72
  106. data/test/fixtures/ovi_no_results +0 -8
  107. data/test/fixtures/pointpin_10_10_10_10 +0 -1
  108. data/test/fixtures/pointpin_555_555_555_555 +0 -1
  109. data/test/fixtures/pointpin_80_111_555_555 +0 -1
  110. data/test/fixtures/pointpin_no_results +0 -1
  111. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_WR26NJ +0 -1
  112. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_generic_error +0 -1
  113. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_hampshire +0 -1
  114. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_key_limit_exceeded +0 -1
  115. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_no_results +0 -1
  116. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_romsey +0 -1
  117. data/test/fixtures/postcode_anywhere_uk_geocode_v2_00_unknown_key +0 -1
  118. data/test/fixtures/smarty_streets_11211 +0 -1
  119. data/test/fixtures/smarty_streets_madison_square_garden +0 -47
  120. data/test/fixtures/smarty_streets_no_results +0 -1
  121. data/test/fixtures/telize_10_10_10_10 +0 -1
  122. data/test/fixtures/telize_555_555_555_555 +0 -4
  123. data/test/fixtures/telize_74_200_247_59 +0 -1
  124. data/test/fixtures/telize_no_results +0 -1
  125. data/test/fixtures/yahoo_error +0 -1
  126. data/test/fixtures/yahoo_invalid_key +0 -2
  127. data/test/fixtures/yahoo_madison_square_garden +0 -52
  128. data/test/fixtures/yahoo_no_results +0 -10
  129. data/test/fixtures/yahoo_over_limit +0 -2
  130. data/test/fixtures/yandex_canada_rue_dupuis_14 +0 -446
  131. data/test/fixtures/yandex_invalid_key +0 -1
  132. data/test/fixtures/yandex_kremlin +0 -48
  133. data/test/fixtures/yandex_new_york +0 -1
  134. data/test/fixtures/yandex_no_administrative_area +0 -53
  135. data/test/fixtures/yandex_no_city_and_town +0 -112
  136. data/test/fixtures/yandex_no_results +0 -16
  137. data/test/integration/http_client_test.rb +0 -31
  138. data/test/mongoid_test_helper.rb +0 -43
  139. data/test/test_helper.rb +0 -423
  140. data/test/unit/active_record_test.rb +0 -16
  141. data/test/unit/cache_test.rb +0 -37
  142. data/test/unit/calculations_test.rb +0 -220
  143. data/test/unit/configuration_test.rb +0 -55
  144. data/test/unit/error_handling_test.rb +0 -79
  145. data/test/unit/geocoder_test.rb +0 -78
  146. data/test/unit/https_test.rb +0 -17
  147. data/test/unit/ip_address_test.rb +0 -27
  148. data/test/unit/lookup_test.rb +0 -153
  149. data/test/unit/lookups/bing_test.rb +0 -68
  150. data/test/unit/lookups/dstk_test.rb +0 -26
  151. data/test/unit/lookups/esri_test.rb +0 -48
  152. data/test/unit/lookups/freegeoip_test.rb +0 -27
  153. data/test/unit/lookups/geocoder_ca_test.rb +0 -17
  154. data/test/unit/lookups/geocodio_test.rb +0 -56
  155. data/test/unit/lookups/geoip2_test.rb +0 -27
  156. data/test/unit/lookups/google_places_details_test.rb +0 -122
  157. data/test/unit/lookups/google_premier_test.rb +0 -22
  158. data/test/unit/lookups/google_test.rb +0 -84
  159. data/test/unit/lookups/mapquest_test.rb +0 -60
  160. data/test/unit/lookups/maxmind_local_test.rb +0 -28
  161. data/test/unit/lookups/maxmind_test.rb +0 -63
  162. data/test/unit/lookups/nominatim_test.rb +0 -31
  163. data/test/unit/lookups/okf_test.rb +0 -38
  164. data/test/unit/lookups/opencagedata_test.rb +0 -64
  165. data/test/unit/lookups/pointpin_test.rb +0 -30
  166. data/test/unit/lookups/postcode_anywhere_uk_test.rb +0 -70
  167. data/test/unit/lookups/smarty_streets_test.rb +0 -71
  168. data/test/unit/lookups/telize_test.rb +0 -36
  169. data/test/unit/lookups/yahoo_test.rb +0 -35
  170. data/test/unit/method_aliases_test.rb +0 -26
  171. data/test/unit/model_test.rb +0 -38
  172. data/test/unit/mongoid_test.rb +0 -47
  173. data/test/unit/near_test.rb +0 -87
  174. data/test/unit/oauth_util_test.rb +0 -31
  175. data/test/unit/proxy_test.rb +0 -37
  176. data/test/unit/query_test.rb +0 -52
  177. data/test/unit/rake_task_test.rb +0 -21
  178. data/test/unit/request_test.rb +0 -35
  179. data/test/unit/result_test.rb +0 -81
  180. data/test/unit/test_mode_test.rb +0 -70
@@ -1,16 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class ActiveRecordTest < GeocoderTestCase
6
-
7
- def test_exclude_condition_when_model_has_a_custom_primary_key
8
- venue = PlaceWithCustomPrimaryKey.new(*geocoded_object_params(:msg))
9
-
10
- # just call private method directly so we don't have to stub .near scope
11
- conditions = venue.class.send(:add_exclude_condition, ["fake_condition"], venue)
12
-
13
- assert_match( /#{PlaceWithCustomPrimaryKey.primary_key}/, conditions.join)
14
- end
15
-
16
- end
@@ -1,37 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class CacheTest < GeocoderTestCase
6
-
7
- def test_second_occurrence_of_request_is_cache_hit
8
- Geocoder.configure(:cache => {})
9
- Geocoder::Lookup.all_services_except_test.each do |l|
10
- next if l == :maxmind_local || l == :geoip2 # local, does not use cache
11
- Geocoder.configure(:lookup => l)
12
- set_api_key!(l)
13
- results = Geocoder.search("Madison Square Garden")
14
- assert !results.first.cache_hit,
15
- "Lookup #{l} returned erroneously cached result."
16
- results = Geocoder.search("Madison Square Garden")
17
- assert results.first.cache_hit,
18
- "Lookup #{l} did not return cached result."
19
- end
20
- end
21
-
22
- def test_google_over_query_limit_does_not_hit_cache
23
- Geocoder.configure(:cache => {})
24
- Geocoder.configure(:lookup => :google)
25
- set_api_key!(:google)
26
- Geocoder.configure(:always_raise => :all)
27
- assert_raises Geocoder::OverQueryLimitError do
28
- Geocoder.search("over limit")
29
- end
30
- lookup = Geocoder::Lookup.get(:google)
31
- assert_equal false, lookup.instance_variable_get(:@cache_hit)
32
- assert_raises Geocoder::OverQueryLimitError do
33
- Geocoder.search("over limit")
34
- end
35
- assert_equal false, lookup.instance_variable_get(:@cache_hit)
36
- end
37
- end
@@ -1,220 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class CalculationsTest < GeocoderTestCase
6
- def setup
7
- Geocoder.configure(
8
- :units => :mi,
9
- :distances => :linear
10
- )
11
- end
12
-
13
- # --- degree distance ---
14
-
15
- def test_longitude_degree_distance_at_equator
16
- assert_equal 69, Geocoder::Calculations.longitude_degree_distance(0).round
17
- end
18
-
19
- def test_longitude_degree_distance_at_new_york
20
- assert_equal 53, Geocoder::Calculations.longitude_degree_distance(40).round
21
- end
22
-
23
- def test_longitude_degree_distance_at_north_pole
24
- assert_equal 0, Geocoder::Calculations.longitude_degree_distance(89.98).round
25
- end
26
-
27
-
28
- # --- distance between ---
29
-
30
- def test_distance_between_in_miles
31
- assert_equal 69, Geocoder::Calculations.distance_between([0,0], [0,1]).round
32
- la_to_ny = Geocoder::Calculations.distance_between([34.05,-118.25], [40.72,-74]).round
33
- assert (la_to_ny - 2444).abs < 10
34
- end
35
-
36
- def test_distance_between_in_kilometers
37
- assert_equal 111, Geocoder::Calculations.distance_between([0,0], [0,1], :units => :km).round
38
- la_to_ny = Geocoder::Calculations.distance_between([34.05,-118.25], [40.72,-74], :units => :km).round
39
- assert (la_to_ny - 3942).abs < 10
40
- end
41
-
42
- def test_distance_between_in_nautical_miles
43
- assert_equal 60, Geocoder::Calculations.distance_between([0,0], [0,1], :units => :nm).round
44
- la_to_ny = Geocoder::Calculations.distance_between([34.05,-118.25], [40.72,-74], :units => :nm).round
45
- assert (la_to_ny - 2124).abs < 10
46
- end
47
-
48
-
49
- # --- geographic center ---
50
-
51
- def test_geographic_center_with_arrays
52
- assert_equal [0.0, 0.5],
53
- Geocoder::Calculations.geographic_center([[0,0], [0,1]])
54
- assert_equal [0.0, 1.0],
55
- Geocoder::Calculations.geographic_center([[0,0], [0,1], [0,2]])
56
- end
57
-
58
- def test_geographic_center_with_mixed_arguments
59
- p1 = [0, 0]
60
- p2 = PlaceReverseGeocoded.new("Some Cold Place", 0, 1)
61
- assert_equal [0.0, 0.5], Geocoder::Calculations.geographic_center([p1, p2])
62
- end
63
-
64
-
65
- # --- bounding box ---
66
-
67
- def test_bounding_box_calculation_in_miles
68
- center = [51, 7] # Cologne, DE
69
- radius = 10 # miles
70
- corners = [50.86, 6.77, 51.14, 7.23]
71
- assert_equal corners.map{ |i| (i * 100).round },
72
- Geocoder::Calculations.bounding_box(center, radius).map{ |i| (i * 100).round }
73
- end
74
-
75
- def test_bounding_box_calculation_in_kilometers
76
- center = [51, 7] # Cologne, DE
77
- radius = 111 # kilometers (= 1 degree latitude)
78
- corners = [50, 5.41, 52, 8.59]
79
- assert_equal corners.map{ |i| (i * 100).round },
80
- Geocoder::Calculations.bounding_box(center, radius, :units => :km).map{ |i| (i * 100).round }
81
- end
82
-
83
- def test_bounding_box_calculation_with_object
84
- center = [51, 7] # Cologne, DE
85
- radius = 10 # miles
86
- corners = [50.86, 6.77, 51.14, 7.23]
87
- obj = PlaceReverseGeocoded.new("Cologne", center[0], center[1])
88
- assert_equal corners.map{ |i| (i * 100).round },
89
- Geocoder::Calculations.bounding_box(obj, radius).map{ |i| (i * 100).round }
90
- end
91
-
92
- def test_bounding_box_calculation_with_address_string
93
- assert_nothing_raised do
94
- Geocoder::Calculations.bounding_box("4893 Clay St, San Francisco, CA", 50)
95
- end
96
- end
97
-
98
- # --- random point ---
99
-
100
- def test_random_point_within_radius
101
- 20.times do
102
- center = [51, 7] # Cologne, DE
103
- radius = 10 # miles
104
- random_point = Geocoder::Calculations.random_point_near(center, radius)
105
- distance = Geocoder::Calculations.distance_between(center, random_point)
106
- assert distance <= radius
107
- end
108
- end
109
-
110
- # --- bearing ---
111
-
112
- def test_compass_points
113
- assert_equal "N", Geocoder::Calculations.compass_point(0)
114
- assert_equal "N", Geocoder::Calculations.compass_point(1.0)
115
- assert_equal "N", Geocoder::Calculations.compass_point(360)
116
- assert_equal "N", Geocoder::Calculations.compass_point(361)
117
- assert_equal "N", Geocoder::Calculations.compass_point(-22)
118
- assert_equal "NW", Geocoder::Calculations.compass_point(-23)
119
- assert_equal "S", Geocoder::Calculations.compass_point(180)
120
- assert_equal "S", Geocoder::Calculations.compass_point(181)
121
- end
122
-
123
- def test_bearing_between
124
- bearings = {
125
- :n => 0,
126
- :e => 90,
127
- :s => 180,
128
- :w => 270
129
- }
130
- points = {
131
- :n => [41, -75],
132
- :e => [40, -74],
133
- :s => [39, -75],
134
- :w => [40, -76]
135
- }
136
- directions = [:n, :e, :s, :w]
137
- methods = [:linear, :spherical]
138
-
139
- methods.each do |m|
140
- directions.each_with_index do |d,i|
141
- opp = directions[(i + 2) % 4] # opposite direction
142
- b = Geocoder::Calculations.bearing_between(
143
- points[d], points[opp], :method => m)
144
- assert (b - bearings[opp]).abs < 1,
145
- "Bearing (#{m}) should be close to #{bearings[opp]} but was #{b}."
146
- end
147
- end
148
- end
149
-
150
- def test_spherical_bearing_to
151
- l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
152
- assert_equal 324, l.bearing_to([50,-85], :method => :spherical).round
153
- end
154
-
155
- def test_spherical_bearing_from
156
- l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
157
- assert_equal 136, l.bearing_from([50,-85], :method => :spherical).round
158
- end
159
-
160
- def test_linear_bearing_from_and_to_are_exactly_opposite
161
- l = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
162
- assert_equal l.bearing_from([50,-86.1]), l.bearing_to([50,-86.1]) - 180
163
- end
164
-
165
- def test_extract_coordinates
166
- coords = [-23,47]
167
- l = PlaceReverseGeocoded.new("Madagascar", coords[0], coords[1])
168
- assert_equal coords, Geocoder::Calculations.extract_coordinates(l)
169
- assert_equal coords, Geocoder::Calculations.extract_coordinates(coords)
170
- end
171
-
172
- def test_extract_nan_coordinates
173
- result = Geocoder::Calculations.extract_coordinates([ nil, nil ])
174
- assert_nan_coordinates?(result)
175
-
176
- result = Geocoder::Calculations.extract_coordinates(nil)
177
- assert_nan_coordinates?(result)
178
-
179
- result = Geocoder::Calculations.extract_coordinates('')
180
- assert_nan_coordinates?(result)
181
-
182
- result = Geocoder::Calculations.extract_coordinates([ 'nix' ])
183
- assert_nan_coordinates?(result)
184
-
185
- o = Object.new
186
- result = Geocoder::Calculations.extract_coordinates(o)
187
- assert_nan_coordinates?(result)
188
- end
189
-
190
- def test_coordinates_present
191
- assert Geocoder::Calculations.coordinates_present?(3.23)
192
- assert !Geocoder::Calculations.coordinates_present?(nil)
193
- assert !Geocoder::Calculations.coordinates_present?(Geocoder::Calculations::NAN)
194
- assert !Geocoder::Calculations.coordinates_present?(3.23, nil)
195
- end
196
-
197
- private # ------------------------------------------------------------------
198
-
199
- def assert_nan_coordinates?(value)
200
- assert value.is_a?(Array) &&
201
- value.size == 2 &&
202
- value[0].nan? &&
203
- value[1].nan?,
204
- "Expected value to be [NaN, NaN] but was #{value}"
205
- end
206
-
207
- def test_endpoint
208
- # test 5 time with random coordinates and headings
209
- [0..5].each do |i|
210
- rheading = [*0..359].sample
211
- rdistance = [*0..100].sample
212
- startpoint = [45.0906, 7.6596]
213
- endpoint = Geocoder::Calculations.endpoint(startpoint, rheading, rdistance)
214
- assert_in_delta rdistance,
215
- Geocoder::Calculations.distance_between(startpoint, endpoint, :method => :spherical), 1E-5
216
- assert_in_delta rheading,
217
- Geocoder::Calculations.bearing_between(startpoint, endpoint, :method => :spherical), 1E-2
218
- end
219
- end
220
- end
@@ -1,55 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class ConfigurationTest < GeocoderTestCase
6
- def setup
7
- Geocoder::Configuration.set_defaults
8
- end
9
-
10
- def test_exception_raised_on_bad_lookup_config
11
- Geocoder.configure(:lookup => :stoopid)
12
- assert_raises Geocoder::ConfigurationError do
13
- Geocoder.search "something dumb"
14
- end
15
- end
16
-
17
- def test_setting_with_class_method
18
- Geocoder::Configuration.units = :test
19
- assert_equal :test, Geocoder.config.units
20
- end
21
-
22
- def test_setting_with_configure_method
23
- Geocoder.configure(:units => :test)
24
- assert_equal :test, Geocoder.config.units
25
- end
26
-
27
- def test_config_for_lookup
28
- Geocoder.configure(
29
- :timeout => 5,
30
- :api_key => "aaa",
31
- :google => {
32
- :timeout => 2
33
- }
34
- )
35
- assert_equal 2, Geocoder.config_for_lookup(:google).timeout
36
- assert_equal "aaa", Geocoder.config_for_lookup(:google).api_key
37
- end
38
-
39
- def test_configuration_chain
40
- v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
41
- v.latitude = 0
42
- v.longitude = 0
43
-
44
- # method option > global configuration
45
- Geocoder.configure(:units => :km)
46
- assert_equal 69, v.distance_to([0,1], :mi).round
47
-
48
- # per-model configuration > global configuration
49
- PlaceReverseGeocoded.reverse_geocoded_by :latitude, :longitude, method: :spherical, units: :mi
50
- assert_equal 69, v.distance_to([0,1]).round
51
-
52
- # method option > per-model configuration
53
- assert_equal 111, v.distance_to([0,1], :km).round
54
- end
55
- end
@@ -1,79 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class ErrorHandlingTest < GeocoderTestCase
6
-
7
- def teardown
8
- Geocoder.configure(:always_raise => [])
9
- end
10
-
11
- def test_does_not_choke_on_timeout
12
- silence_warnings do
13
- Geocoder::Lookup.all_services_except_test.each do |l|
14
- Geocoder.configure(:lookup => l)
15
- set_api_key!(l)
16
- assert_nothing_raised { Geocoder.search("timeout") }
17
- end
18
- end
19
- end
20
-
21
- def test_always_raise_response_parse_error
22
- Geocoder.configure(:always_raise => [Geocoder::ResponseParseError])
23
- [:freegeoip, :google, :okf].each do |l|
24
- lookup = Geocoder::Lookup.get(l)
25
- set_api_key!(l)
26
- assert_raises Geocoder::ResponseParseError do
27
- lookup.send(:results, Geocoder::Query.new("invalid_json"))
28
- end
29
- end
30
- end
31
-
32
- def test_never_raise_response_parse_error
33
- [:freegeoip, :google, :okf].each do |l|
34
- lookup = Geocoder::Lookup.get(l)
35
- set_api_key!(l)
36
- silence_warnings do
37
- assert_nothing_raised do
38
- lookup.send(:results, Geocoder::Query.new("invalid_json"))
39
- end
40
- end
41
- end
42
- end
43
-
44
- def test_always_raise_timeout_error
45
- Geocoder.configure(:always_raise => [TimeoutError])
46
- Geocoder::Lookup.all_services_except_test.each do |l|
47
- next if l == :maxmind_local || l == :geoip2 # local, does not use cache
48
- lookup = Geocoder::Lookup.get(l)
49
- set_api_key!(l)
50
- assert_raises TimeoutError do
51
- lookup.send(:results, Geocoder::Query.new("timeout"))
52
- end
53
- end
54
- end
55
-
56
- def test_always_raise_socket_error
57
- Geocoder.configure(:always_raise => [SocketError])
58
- Geocoder::Lookup.all_services_except_test.each do |l|
59
- next if l == :maxmind_local || l == :geoip2 # local, does not use cache
60
- lookup = Geocoder::Lookup.get(l)
61
- set_api_key!(l)
62
- assert_raises SocketError do
63
- lookup.send(:results, Geocoder::Query.new("socket_error"))
64
- end
65
- end
66
- end
67
-
68
- def test_always_raise_connection_refused_error
69
- Geocoder.configure(:always_raise => [Errno::ECONNREFUSED])
70
- Geocoder::Lookup.all_services_except_test.each do |l|
71
- next if l == :maxmind_local || l == :geoip2 # local, does not use cache
72
- lookup = Geocoder::Lookup.get(l)
73
- set_api_key!(l)
74
- assert_raises Errno::ECONNREFUSED do
75
- lookup.send(:results, Geocoder::Query.new("connection_refused"))
76
- end
77
- end
78
- end
79
- end
@@ -1,78 +0,0 @@
1
- # encoding: utf-8
2
- $: << File.join(File.dirname(__FILE__), "..")
3
- require 'test_helper'
4
-
5
- class GeocoderTest < GeocoderTestCase
6
-
7
- def test_distance_to_returns_float
8
- v = Place.new(*geocoded_object_params(:msg))
9
- v.latitude, v.longitude = [40.750354, -73.993371]
10
- assert (v.distance_to([30, -94])).is_a?(Float)
11
- end
12
-
13
- def test_coordinates_method_returns_array
14
- assert Geocoder.coordinates("Madison Square Garden, New York, NY").is_a?(Array)
15
- end
16
-
17
- def test_address_method_returns_string
18
- assert Geocoder.address([40.750354, -73.993371]).is_a?(String)
19
- end
20
-
21
- def test_geographic_center_doesnt_overwrite_argument_value
22
- # test for the presence of a bug that was introduced in version 0.9.11
23
- orig_points = [[52,8], [46,9], [42,5]]
24
- points = orig_points.clone
25
- Geocoder::Calculations.geographic_center(points)
26
- assert_equal orig_points, points
27
- end
28
-
29
- def test_geocode_assigns_and_returns_coordinates
30
- v = Place.new(*geocoded_object_params(:msg))
31
- coords = [40.750354, -73.993371]
32
- assert_equal coords, v.geocode
33
- assert_equal coords, [v.latitude, v.longitude]
34
- end
35
-
36
- def test_geocode_block_executed_when_no_results
37
- v = PlaceWithCustomResultsHandling.new("Nowhere", "no results")
38
- v.geocode
39
- assert_equal "NOT FOUND", v.coords_string
40
- end
41
-
42
- def test_reverse_geocode_assigns_and_returns_address
43
- v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
44
- address = "4 Penn Plaza, New York, NY 10001, USA"
45
- assert_equal address, v.reverse_geocode
46
- assert_equal address, v.address
47
- end
48
-
49
- def test_forward_and_reverse_geocoding_on_same_model_works
50
- g = PlaceWithForwardAndReverseGeocoding.new("Exxon")
51
- g.address = "404 New St, Middletown, CT"
52
- g.geocode
53
- assert_not_nil g.lat
54
- assert_not_nil g.lon
55
-
56
- assert_nil g.location
57
- g.reverse_geocode
58
- assert_not_nil g.location
59
- end
60
-
61
- def test_geocode_with_custom_lookup_param
62
- v = PlaceWithCustomLookup.new(*geocoded_object_params(:msg))
63
- v.geocode
64
- assert_equal Geocoder::Result::Nominatim, v.result_class
65
- end
66
-
67
- def test_geocode_with_custom_lookup_proc_param
68
- v = PlaceWithCustomLookupProc.new(*geocoded_object_params(:msg))
69
- v.geocode
70
- assert_equal Geocoder::Result::Nominatim, v.result_class
71
- end
72
-
73
- def test_reverse_geocode_with_custom_lookup_param
74
- v = PlaceReverseGeocodedWithCustomLookup.new(*reverse_geocoded_object_params(:msg))
75
- v.reverse_geocode
76
- assert_equal Geocoder::Result::Nominatim, v.result_class
77
- end
78
- end