geocoder 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of geocoder might be problematic. Click here for more details.
- data/CHANGELOG.rdoc +9 -0
- data/README.md +24 -10
- data/lib/geocoder/configuration.rb +1 -1
- data/lib/geocoder/lookups/base.rb +35 -16
- data/lib/geocoder/lookups/bing.rb +1 -1
- data/lib/geocoder/lookups/freegeoip.rb +1 -1
- data/lib/geocoder/lookups/geocoder_ca.rb +1 -1
- data/lib/geocoder/lookups/mapquest.rb +25 -4
- data/lib/geocoder/lookups/nominatim.rb +1 -1
- data/lib/geocoder/lookups/yahoo.rb +26 -42
- data/lib/geocoder/lookups/yandex.rb +1 -1
- data/lib/geocoder/models/mongoid.rb +1 -1
- data/lib/geocoder/query.rb +7 -1
- data/lib/geocoder/results/base.rb +8 -2
- data/lib/geocoder/results/mapquest.rb +46 -2
- data/lib/geocoder/results/yahoo.rb +9 -2
- data/lib/geocoder/stores/active_record.rb +14 -9
- data/lib/geocoder/stores/base.rb +1 -1
- data/lib/geocoder/version.rb +1 -1
- data/lib/oauth_util.rb +106 -0
- data/test/cache_test.rb +19 -0
- data/test/error_handling_test.rb +4 -0
- data/test/fixtures/mapquest_madison_square_garden.json +50 -25
- data/test/fixtures/mapquest_no_results.json +7 -1
- data/test/fixtures/yahoo_error.json +1 -0
- data/test/fixtures/yahoo_madison_square_garden.json +48 -48
- data/test/fixtures/yahoo_no_results.json +7 -7
- data/test/lookup_test.rb +1 -6
- data/test/near_test.rb +11 -0
- data/test/query_test.rb +5 -0
- data/test/result_test.rb +1 -0
- data/test/services_test.rb +43 -23
- data/test/test_helper.rb +46 -15
- metadata +8 -6
- data/test/fixtures/yahoo_v1_madison_square_garden.json +0 -46
- data/test/fixtures/yahoo_v1_no_results.json +0 -10
data/test/lookup_test.rb
CHANGED
@@ -6,6 +6,7 @@ class LookupTest < Test::Unit::TestCase
|
|
6
6
|
def test_search_returns_empty_array_when_no_results
|
7
7
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
8
8
|
lookup = Geocoder::Lookup.get(l)
|
9
|
+
set_api_key!(l)
|
9
10
|
assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
|
10
11
|
"Lookup #{l} does not return empty array when no results."
|
11
12
|
end
|
@@ -29,12 +30,6 @@ class LookupTest < Test::Unit::TestCase
|
|
29
30
|
assert_match "key=MY_KEY", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
30
31
|
end
|
31
32
|
|
32
|
-
def test_yahoo_app_id
|
33
|
-
Geocoder::Configuration.api_key = "MY_KEY"
|
34
|
-
g = Geocoder::Lookup::Yahoo.new
|
35
|
-
assert_match "appid=MY_KEY", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
|
36
|
-
end
|
37
|
-
|
38
33
|
def test_geocoder_ca_showpostal
|
39
34
|
Geocoder::Configuration.api_key = "MY_KEY"
|
40
35
|
g = Geocoder::Lookup::GeocoderCa.new
|
data/test/near_test.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NearTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_near_scope_options_without_sqlite_includes_bounding_box_condition
|
6
|
+
result = Event.send(:near_scope_options, 1.0, 2.0, 5)
|
7
|
+
|
8
|
+
assert_match /test_table_name.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND test_table_name.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /,
|
9
|
+
result[:conditions][0]
|
10
|
+
end
|
11
|
+
end
|
data/test/query_test.rb
CHANGED
@@ -20,6 +20,11 @@ class QueryTest < Test::Unit::TestCase
|
|
20
20
|
assert !Geocoder::Query.new("Москва").blank? # no ASCII characters
|
21
21
|
end
|
22
22
|
|
23
|
+
def test_blank_query_detection_for_coordinates
|
24
|
+
assert Geocoder::Query.new([nil,nil]).blank?
|
25
|
+
assert Geocoder::Query.new([87,nil]).blank?
|
26
|
+
end
|
27
|
+
|
23
28
|
def test_coordinates_detection
|
24
29
|
assert Geocoder::Query.new("51.178844,5").coordinates?
|
25
30
|
assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
|
data/test/result_test.rb
CHANGED
@@ -6,6 +6,7 @@ class ResultTest < Test::Unit::TestCase
|
|
6
6
|
def test_result_has_required_attributes
|
7
7
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
8
8
|
Geocoder::Configuration.lookup = l
|
9
|
+
set_api_key!(l)
|
9
10
|
result = Geocoder.search([45.423733, -75.676333]).first
|
10
11
|
assert_result_has_required_attributes(result)
|
11
12
|
end
|
data/test/services_test.rb
CHANGED
@@ -6,12 +6,13 @@ class ServicesTest < Test::Unit::TestCase
|
|
6
6
|
|
7
7
|
def test_query_url_contains_values_in_params_hash
|
8
8
|
Geocoder::Lookup.all_services_except_test.each do |l|
|
9
|
-
next if l == :google_premier # TODO: need to set keys to test
|
10
9
|
next if l == :freegeoip # does not use query string
|
10
|
+
set_api_key!(l)
|
11
11
|
url = Geocoder::Lookup.get(l).send(:query_url, Geocoder::Query.new(
|
12
12
|
"test", :params => {:one_in_the_hand => "two in the bush"}
|
13
13
|
))
|
14
|
-
|
14
|
+
# should be "+"s for all lookups except Yahoo
|
15
|
+
assert_match /one_in_the_hand=two(%20|\+)in(%20|\+)the(%20|\+)bush/, url,
|
15
16
|
"Lookup #{l} does not appear to support arbitrary params in URL"
|
16
17
|
end
|
17
18
|
end
|
@@ -53,6 +54,7 @@ class ServicesTest < Test::Unit::TestCase
|
|
53
54
|
|
54
55
|
def test_google_premier_result_components
|
55
56
|
Geocoder::Configuration.lookup = :google_premier
|
57
|
+
set_api_key!(:google_premier)
|
56
58
|
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
57
59
|
assert_equal "Manhattan",
|
58
60
|
result.address_components_of_type(:sublocality).first['long_name']
|
@@ -67,37 +69,29 @@ class ServicesTest < Test::Unit::TestCase
|
|
67
69
|
|
68
70
|
# --- Yahoo ---
|
69
71
|
|
70
|
-
def
|
72
|
+
def test_yahoo_no_results
|
71
73
|
Geocoder::Configuration.lookup = :yahoo
|
72
|
-
assert_equal [], Geocoder.search("no results
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_yahoo_v1_result_components
|
76
|
-
Geocoder::Configuration.lookup = :yahoo
|
77
|
-
result = Geocoder.search("madison square garden v1").first
|
78
|
-
assert_equal "10001", result.postal_code
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_yahoo_v1_address_formatting
|
82
|
-
Geocoder::Configuration.lookup = :yahoo
|
83
|
-
result = Geocoder.search("madison square garden v1").first
|
84
|
-
assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
|
74
|
+
assert_equal [], Geocoder.search("no results")
|
85
75
|
end
|
86
76
|
|
87
|
-
def
|
77
|
+
def test_yahoo_error
|
88
78
|
Geocoder::Configuration.lookup = :yahoo
|
89
|
-
|
79
|
+
# keep test output clean: suppress timeout warning
|
80
|
+
orig = $VERBOSE; $VERBOSE = nil
|
81
|
+
assert_equal [], Geocoder.search("error")
|
82
|
+
ensure
|
83
|
+
$VERBOSE = orig
|
90
84
|
end
|
91
85
|
|
92
|
-
def
|
86
|
+
def test_yahoo_result_components
|
93
87
|
Geocoder::Configuration.lookup = :yahoo
|
94
|
-
result = Geocoder.search("madison square garden
|
88
|
+
result = Geocoder.search("madison square garden").first
|
95
89
|
assert_equal "10001", result.postal_code
|
96
90
|
end
|
97
91
|
|
98
|
-
def
|
92
|
+
def test_yahoo_address_formatting
|
99
93
|
Geocoder::Configuration.lookup = :yahoo
|
100
|
-
result = Geocoder.search("madison square garden
|
94
|
+
result = Geocoder.search("madison square garden").first
|
101
95
|
assert_equal "Madison Square Garden, New York, NY 10001, United States", result.address
|
102
96
|
end
|
103
97
|
|
@@ -109,6 +103,7 @@ class ServicesTest < Test::Unit::TestCase
|
|
109
103
|
orig = $VERBOSE; $VERBOSE = nil
|
110
104
|
Geocoder::Configuration.lookup = :yandex
|
111
105
|
assert_equal [], Geocoder.search("invalid key")
|
106
|
+
ensure
|
112
107
|
$VERBOSE = orig
|
113
108
|
end
|
114
109
|
|
@@ -154,7 +149,7 @@ class ServicesTest < Test::Unit::TestCase
|
|
154
149
|
|
155
150
|
# --- Nominatim ---
|
156
151
|
|
157
|
-
|
152
|
+
def test_nominatim_result_components
|
158
153
|
Geocoder::Configuration.lookup = :nominatim
|
159
154
|
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
160
155
|
assert_equal "10001", result.postal_code
|
@@ -166,4 +161,29 @@ class ServicesTest < Test::Unit::TestCase
|
|
166
161
|
assert_equal "Madison Square Garden, West 31st Street, Long Island City, New York City, New York, 10001, United States of America",
|
167
162
|
result.address
|
168
163
|
end
|
164
|
+
# --- MapQuest ---
|
165
|
+
|
166
|
+
def test_api_route
|
167
|
+
Geocoder::Configuration.lookup = :mapquest
|
168
|
+
Geocoder::Configuration.api_key = "abc123"
|
169
|
+
|
170
|
+
lookup = Geocoder::Lookup::Mapquest.new
|
171
|
+
query = Geocoder::Query.new("Bluffton, SC")
|
172
|
+
res = lookup.send(:query_url, query)
|
173
|
+
assert_equal "http://www.mapquestapi.com/geocoding/v1/address?key=abc123&location=Bluffton%2C+SC",
|
174
|
+
res
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_mapquest_result_components
|
178
|
+
Geocoder::Configuration.lookup = :mapquest
|
179
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
180
|
+
assert_equal "10001", result.postal_code
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_mapquest_address_formatting
|
184
|
+
Geocoder::Configuration.lookup = :mapquest
|
185
|
+
result = Geocoder.search("Madison Square Garden, New York, NY").first
|
186
|
+
assert_equal "46 West 31st Street, New York, NY, 10001, US",
|
187
|
+
result.address
|
188
|
+
end
|
169
189
|
end
|
data/test/test_helper.rb
CHANGED
@@ -4,6 +4,12 @@ require 'test/unit'
|
|
4
4
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
6
|
|
7
|
+
class MysqlConnection
|
8
|
+
def adapter_name
|
9
|
+
"mysql"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
7
13
|
##
|
8
14
|
# Simulate enough of ActiveRecord::Base that objects can be used for testing.
|
9
15
|
#
|
@@ -28,6 +34,10 @@ module ActiveRecord
|
|
28
34
|
|
29
35
|
def self.scope(*args); end
|
30
36
|
|
37
|
+
def self.connection
|
38
|
+
MysqlConnection.new
|
39
|
+
end
|
40
|
+
|
31
41
|
def method_missing(name, *args, &block)
|
32
42
|
if name.to_s[-1..-1] == "="
|
33
43
|
write_attribute name.to_s[0...-1], *args
|
@@ -65,13 +75,19 @@ module Geocoder
|
|
65
75
|
class Base
|
66
76
|
private #-----------------------------------------------------------------
|
67
77
|
def read_fixture(file)
|
68
|
-
File.
|
78
|
+
filepath = File.join("test", "fixtures", file)
|
79
|
+
s = File.read(filepath).strip.gsub(/\n\s*/, "")
|
80
|
+
s.instance_eval do
|
81
|
+
def body; self; end
|
82
|
+
def code; "200"; end
|
83
|
+
end
|
84
|
+
s
|
69
85
|
end
|
70
86
|
end
|
71
87
|
|
72
88
|
class Google < Base
|
73
89
|
private #-----------------------------------------------------------------
|
74
|
-
def
|
90
|
+
def make_api_request(query)
|
75
91
|
raise TimeoutError if query.text == "timeout"
|
76
92
|
raise SocketError if query.text == "socket_error"
|
77
93
|
file = case query.text
|
@@ -89,14 +105,13 @@ module Geocoder
|
|
89
105
|
|
90
106
|
class Yahoo < Base
|
91
107
|
private #-----------------------------------------------------------------
|
92
|
-
def
|
108
|
+
def make_api_request(query)
|
93
109
|
raise TimeoutError if query.text == "timeout"
|
94
110
|
raise SocketError if query.text == "socket_error"
|
95
111
|
file = case query.text
|
96
|
-
when "no results
|
97
|
-
when "
|
98
|
-
|
99
|
-
else :madison_square_garden
|
112
|
+
when "no results"; :no_results
|
113
|
+
when "error"; :error
|
114
|
+
else :madison_square_garden
|
100
115
|
end
|
101
116
|
read_fixture "yahoo_#{file}.json"
|
102
117
|
end
|
@@ -104,7 +119,7 @@ module Geocoder
|
|
104
119
|
|
105
120
|
class Yandex < Base
|
106
121
|
private #-----------------------------------------------------------------
|
107
|
-
def
|
122
|
+
def make_api_request(query)
|
108
123
|
raise TimeoutError if query.text == "timeout"
|
109
124
|
raise SocketError if query.text == "socket_error"
|
110
125
|
file = case query.text
|
@@ -118,7 +133,7 @@ module Geocoder
|
|
118
133
|
|
119
134
|
class GeocoderCa < Base
|
120
135
|
private #-----------------------------------------------------------------
|
121
|
-
def
|
136
|
+
def make_api_request(query)
|
122
137
|
raise TimeoutError if query.text == "timeout"
|
123
138
|
raise SocketError if query.text == "socket_error"
|
124
139
|
if query.reverse_geocode?
|
@@ -135,7 +150,7 @@ module Geocoder
|
|
135
150
|
|
136
151
|
class Freegeoip < Base
|
137
152
|
private #-----------------------------------------------------------------
|
138
|
-
def
|
153
|
+
def make_api_request(query)
|
139
154
|
raise TimeoutError if query.text == "timeout"
|
140
155
|
raise SocketError if query.text == "socket_error"
|
141
156
|
file = case query.text
|
@@ -148,7 +163,7 @@ module Geocoder
|
|
148
163
|
|
149
164
|
class Bing < Base
|
150
165
|
private #-----------------------------------------------------------------
|
151
|
-
def
|
166
|
+
def make_api_request(query)
|
152
167
|
raise TimeoutError if query.text == "timeout"
|
153
168
|
raise SocketError if query.text == "socket_error"
|
154
169
|
if query.reverse_geocode?
|
@@ -165,7 +180,7 @@ module Geocoder
|
|
165
180
|
|
166
181
|
class Nominatim < Base
|
167
182
|
private #-----------------------------------------------------------------
|
168
|
-
def
|
183
|
+
def make_api_request(query)
|
169
184
|
raise TimeoutError if query.text == "timeout"
|
170
185
|
raise SocketError if query.text == "socket_error"
|
171
186
|
file = case query.text
|
@@ -176,9 +191,9 @@ module Geocoder
|
|
176
191
|
end
|
177
192
|
end
|
178
193
|
|
179
|
-
class Mapquest <
|
194
|
+
class Mapquest < Base
|
180
195
|
private #-----------------------------------------------------------------
|
181
|
-
def
|
196
|
+
def make_api_request(query)
|
182
197
|
raise TimeoutError if query.text == "timeout"
|
183
198
|
raise SocketError if query.text == "socket_error"
|
184
199
|
file = case query.text
|
@@ -309,5 +324,21 @@ class Test::Unit::TestCase
|
|
309
324
|
return false unless coordinates.size == 2 # Should have dimension 2
|
310
325
|
coordinates[0].nan? && coordinates[1].nan? # Both coordinates should be NaN
|
311
326
|
end
|
312
|
-
end
|
313
327
|
|
328
|
+
def set_api_key!(lookup_name)
|
329
|
+
if lookup_name == :google_premier
|
330
|
+
Geocoder::Configuration.api_key = [
|
331
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
332
|
+
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
|
333
|
+
'cccccccccccccccccccccccccccccc'
|
334
|
+
]
|
335
|
+
elsif lookup_name == :yahoo
|
336
|
+
Geocoder::Configuration.api_key = [
|
337
|
+
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
338
|
+
'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
|
339
|
+
]
|
340
|
+
else
|
341
|
+
Geocoder::Configuration.api_key = nil
|
342
|
+
end
|
343
|
+
end
|
344
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geocoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Provides object geocoding (by street or IP address), reverse geocoding
|
15
15
|
(coordinates to street address), distance queries for ActiveRecord and Mongoid,
|
@@ -77,8 +77,10 @@ files:
|
|
77
77
|
- lib/geocoder/stores/mongo_mapper.rb
|
78
78
|
- lib/geocoder/stores/mongoid.rb
|
79
79
|
- lib/geocoder/version.rb
|
80
|
+
- lib/oauth_util.rb
|
80
81
|
- lib/tasks/geocoder.rake
|
81
82
|
- test/active_record_test.rb
|
83
|
+
- test/cache_test.rb
|
82
84
|
- test/calculations_test.rb
|
83
85
|
- test/configuration_test.rb
|
84
86
|
- test/custom_block_test.rb
|
@@ -100,10 +102,9 @@ files:
|
|
100
102
|
- test/fixtures/mapquest_no_results.json
|
101
103
|
- test/fixtures/nominatim_madison_square_garden.json
|
102
104
|
- test/fixtures/nominatim_no_results.json
|
105
|
+
- test/fixtures/yahoo_error.json
|
103
106
|
- test/fixtures/yahoo_madison_square_garden.json
|
104
107
|
- test/fixtures/yahoo_no_results.json
|
105
|
-
- test/fixtures/yahoo_v1_madison_square_garden.json
|
106
|
-
- test/fixtures/yahoo_v1_no_results.json
|
107
108
|
- test/fixtures/yandex_invalid_key.json
|
108
109
|
- test/fixtures/yandex_kremlin.json
|
109
110
|
- test/fixtures/yandex_no_results.json
|
@@ -114,6 +115,7 @@ files:
|
|
114
115
|
- test/method_aliases_test.rb
|
115
116
|
- test/mongoid_test.rb
|
116
117
|
- test/mongoid_test_helper.rb
|
118
|
+
- test/near_test.rb
|
117
119
|
- test/proxy_test.rb
|
118
120
|
- test/query_test.rb
|
119
121
|
- test/result_test.rb
|
@@ -134,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
134
136
|
version: '0'
|
135
137
|
segments:
|
136
138
|
- 0
|
137
|
-
hash: -
|
139
|
+
hash: -57977969
|
138
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
141
|
none: false
|
140
142
|
requirements:
|
@@ -143,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
143
145
|
version: '0'
|
144
146
|
segments:
|
145
147
|
- 0
|
146
|
-
hash: -
|
148
|
+
hash: -57977969
|
147
149
|
requirements: []
|
148
150
|
rubyforge_project:
|
149
151
|
rubygems_version: 1.8.24
|
@@ -1,46 +0,0 @@
|
|
1
|
-
{
|
2
|
-
"ResultSet":{
|
3
|
-
"version":"1.0",
|
4
|
-
"Error":0,
|
5
|
-
"ErrorMessage":"No error",
|
6
|
-
"Locale":"us_US",
|
7
|
-
"Quality":90,
|
8
|
-
"Found":1,
|
9
|
-
"Results":[{
|
10
|
-
"quality":90,
|
11
|
-
"latitude":"40.750381",
|
12
|
-
"longitude":"-73.993988",
|
13
|
-
"offsetlat":"40.750381",
|
14
|
-
"offsetlon":"-73.993988",
|
15
|
-
"radius":100,
|
16
|
-
"name":"Madison Square Garden",
|
17
|
-
"line1":"Madison Square Garden",
|
18
|
-
"line2":"New York, NY 10001",
|
19
|
-
"line3":"",
|
20
|
-
"line4":"United States",
|
21
|
-
"house":"",
|
22
|
-
"street":"",
|
23
|
-
"xstreet":"",
|
24
|
-
"unittype":"",
|
25
|
-
"unit":"",
|
26
|
-
"postal":"10001",
|
27
|
-
"neighborhood":"",
|
28
|
-
"city":"New York",
|
29
|
-
"county":"New York County",
|
30
|
-
"state":"New York",
|
31
|
-
"country":"United States",
|
32
|
-
"countrycode":"US",
|
33
|
-
"statecode":"NY",
|
34
|
-
"countycode":"",
|
35
|
-
"uzip":"10001",
|
36
|
-
"hash":"",
|
37
|
-
"woeid":23617041,
|
38
|
-
"woetype":20,
|
39
|
-
"cross":"",
|
40
|
-
"timezone":"America/New_York",
|
41
|
-
"neighborhood":"Garment District|Midtown|Midtown West|Manhattan",
|
42
|
-
"areacode":"212",
|
43
|
-
"boundingbox":{"north":"40.750832","south":"40.749931","east":"-73.993393","west":"-73.994591"}
|
44
|
-
}]
|
45
|
-
}
|
46
|
-
}
|