geocodio 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/geocodio/address.rb +6 -0
- data/lib/geocodio/address_set.rb +7 -0
- data/lib/geocodio/client.rb +18 -13
- data/lib/geocodio/utils.rb +17 -2
- data/lib/geocodio/version.rb +1 -1
- data/spec/client_spec.rb +24 -0
- data/spec/vcr_cassettes/batch_geocode_with_bad_address.yml +46 -0
- data/spec/vcr_cassettes/geocode_bad_address.yml +40 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e41eb68631540823c0d3087613a9f93449bfd6c
|
4
|
+
data.tar.gz: 8fac1f8f09a1079ae0708fc5b73eed400968f85b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ab47ef498df83514389dd82e9bf4dcfa6d01c0b85520e82dfd680b78e06dc9031b4bcd2cdedb2383dfc6f8e7274195f6dad3f3ee462bc310c1b07743af10c76
|
7
|
+
data.tar.gz: e56d5f65d3ef2e7e3d6a1ba35297305f49eb130e7341f6f510f408d30692d8c96bf0804c38ca6c52a158732786931dcfc320f97d3e272b161cdf4bb5911582c7
|
data/lib/geocodio/address.rb
CHANGED
@@ -91,5 +91,11 @@ module Geocodio
|
|
91
91
|
|
92
92
|
@timezone = Timezone.new(timezone)
|
93
93
|
end
|
94
|
+
|
95
|
+
def <=>(address)
|
96
|
+
return -1 if self.accuracy < address.accuracy
|
97
|
+
return 0 if self.accuracy == address.accuracy
|
98
|
+
return 1 if self.accuracy > address.accuracy
|
99
|
+
end
|
94
100
|
end
|
95
101
|
end
|
data/lib/geocodio/address_set.rb
CHANGED
@@ -33,5 +33,12 @@ module Geocodio
|
|
33
33
|
def size
|
34
34
|
@addresses.size
|
35
35
|
end
|
36
|
+
|
37
|
+
# Returns whether or not there are any addresses in this result set.
|
38
|
+
#
|
39
|
+
# @return [Boolean] if there were any results returned by Geocodio
|
40
|
+
def empty?
|
41
|
+
@addresses.empty?
|
42
|
+
end
|
36
43
|
end
|
37
44
|
end
|
data/lib/geocodio/client.rb
CHANGED
@@ -72,8 +72,11 @@ module Geocodio
|
|
72
72
|
#
|
73
73
|
# @param address [String] the full or partial address to parse
|
74
74
|
# @return [Geocodio::Address] a parsed and formatted Address
|
75
|
-
def parse(address)
|
76
|
-
|
75
|
+
def parse(address, options = {})
|
76
|
+
params, options = normalize_params_and_options(options)
|
77
|
+
params[:q] = address
|
78
|
+
|
79
|
+
Address.new get('/parse', params, options).body
|
77
80
|
end
|
78
81
|
|
79
82
|
private
|
@@ -85,39 +88,40 @@ module Geocodio
|
|
85
88
|
end
|
86
89
|
|
87
90
|
def geocode_single(address, options = {})
|
88
|
-
params =
|
89
|
-
params[:
|
91
|
+
params, options = normalize_params_and_options(options)
|
92
|
+
params[:q] = address
|
90
93
|
|
91
|
-
response = get '/geocode', params
|
94
|
+
response = get '/geocode', params, options
|
92
95
|
addresses = parse_results(response)
|
93
96
|
|
94
97
|
AddressSet.new(address, *addresses)
|
95
98
|
end
|
96
99
|
|
97
100
|
def reverse_geocode_single(pair, options = {})
|
101
|
+
params, options = normalize_params_and_options(options)
|
98
102
|
pair = normalize_coordinates(pair)
|
99
|
-
params =
|
100
|
-
params[:fields] = options[:fields].join(',') if options[:fields]
|
103
|
+
params[:q] = pair
|
101
104
|
|
102
|
-
response = get '/reverse', params
|
105
|
+
response = get '/reverse', params, options
|
103
106
|
addresses = parse_results(response)
|
104
107
|
|
105
108
|
AddressSet.new(pair, *addresses)
|
106
109
|
end
|
107
110
|
|
108
111
|
def geocode_batch(addresses, options = {})
|
109
|
-
options
|
112
|
+
params, options = normalize_params_and_options(options)
|
113
|
+
options[:body] = addresses
|
110
114
|
|
111
|
-
response = post '/geocode',
|
115
|
+
response = post '/geocode', params, options
|
112
116
|
|
113
117
|
parse_nested_results(response)
|
114
118
|
end
|
115
119
|
|
116
120
|
def reverse_geocode_batch(pairs, options = {})
|
117
|
-
|
118
|
-
options[:
|
121
|
+
params, options = normalize_params_and_options(options)
|
122
|
+
options[:body] = pairs.map { |pair| normalize_coordinates(pair) }
|
119
123
|
|
120
|
-
response = post '/reverse',
|
124
|
+
response = post '/reverse', params, options
|
121
125
|
|
122
126
|
parse_nested_results(response)
|
123
127
|
end
|
@@ -138,6 +142,7 @@ module Geocodio
|
|
138
142
|
end
|
139
143
|
|
140
144
|
http = Net::HTTP.new HOST, PORT
|
145
|
+
http.read_timeout = options[:timeout] if options[:timeout]
|
141
146
|
res = http.start { http.request(req) }
|
142
147
|
|
143
148
|
case res
|
data/lib/geocodio/utils.rb
CHANGED
@@ -9,8 +9,8 @@ module Geocodio
|
|
9
9
|
results = response.body['results']
|
10
10
|
|
11
11
|
results.map do |result_set|
|
12
|
-
|
13
|
-
addresses
|
12
|
+
addresses = Array(result_set['response']['results'])
|
13
|
+
addresses.map! { |result| Address.new(result) }
|
14
14
|
|
15
15
|
query = result_set['query']
|
16
16
|
|
@@ -22,5 +22,20 @@ module Geocodio
|
|
22
22
|
return coordinates unless coordinates.is_a?(Hash)
|
23
23
|
coordinates.sort.map { |p| p[1] }.join(',')
|
24
24
|
end
|
25
|
+
|
26
|
+
def normalize_params_and_options(hash)
|
27
|
+
hash = Hash[hash.map { |k, v| [k.to_sym, v] }]
|
28
|
+
|
29
|
+
# The only supported parameter is fields
|
30
|
+
params = hash.select { |k, _| [:fields].include?(k) }
|
31
|
+
|
32
|
+
# Normalize this particular parameter to be a comma-separated string
|
33
|
+
params[:fields] = params[:fields].join(',') if params[:fields]
|
34
|
+
|
35
|
+
# The only supported option is `timeout`
|
36
|
+
options = hash.select { |k, _| [:timeout].include?(k) }
|
37
|
+
|
38
|
+
[params, options]
|
39
|
+
end
|
25
40
|
end
|
26
41
|
end
|
data/lib/geocodio/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -19,6 +19,11 @@ describe Geocodio::Client do
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'sets the read_timeout on the underlying Net::HTTP request if passed an option' do
|
23
|
+
expect_any_instance_of(Net::HTTP).to receive(:read_timeout=).with(60 * 5)
|
24
|
+
VCR.use_cassette('geocode') { geocodio.geocode([address], timeout: 60 * 5) }
|
25
|
+
end
|
26
|
+
|
22
27
|
describe '#geocode' do
|
23
28
|
it 'geocodes a single address' do
|
24
29
|
VCR.use_cassette('geocode') do
|
@@ -38,6 +43,12 @@ describe Geocodio::Client do
|
|
38
43
|
end
|
39
44
|
end
|
40
45
|
|
46
|
+
it 'raises a client error when geocoding a bad address' do
|
47
|
+
VCR.use_cassette('geocode_bad_address') do
|
48
|
+
expect { geocodio.geocode([' , , ']) }.to raise_error(Geocodio::Client::Error)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
41
52
|
it 'geocodes multiple addresses' do
|
42
53
|
VCR.use_cassette('batch_geocode') do
|
43
54
|
addresses = [
|
@@ -53,6 +64,19 @@ describe Geocodio::Client do
|
|
53
64
|
end
|
54
65
|
end
|
55
66
|
|
67
|
+
it 'will return empty AddressSets for bad addresses in a batch geocode' do
|
68
|
+
VCR.use_cassette('batch_geocode_with_bad_address') do
|
69
|
+
addresses = [
|
70
|
+
'1 Infinite Loop Cupertino CA 95014',
|
71
|
+
' , , '
|
72
|
+
]
|
73
|
+
|
74
|
+
addresses = geocodio.geocode(addresses)
|
75
|
+
|
76
|
+
expect(addresses.last).to be_empty
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
56
80
|
it 'geocodes multiple addresses with an options hash' do
|
57
81
|
VCR.use_cassette('batch_geocode_with_fields') do
|
58
82
|
addresses = [
|
@@ -0,0 +1,46 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: post
|
5
|
+
uri: http://api.geocod.io/v1/geocode?api_key=secret_api_key
|
6
|
+
body:
|
7
|
+
encoding: UTF-8
|
8
|
+
string: '["1 Infinite Loop Cupertino CA 95014"," , , "]'
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/json
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
User-Agent:
|
15
|
+
- Ruby
|
16
|
+
Content-Type:
|
17
|
+
- application/json
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- nginx/1.2.1
|
25
|
+
Content-Type:
|
26
|
+
- application/json
|
27
|
+
Transfer-Encoding:
|
28
|
+
- chunked
|
29
|
+
Connection:
|
30
|
+
- keep-alive
|
31
|
+
Cache-Control:
|
32
|
+
- no-cache
|
33
|
+
Date:
|
34
|
+
- Thu, 10 Apr 2014 15:55:59 GMT
|
35
|
+
Access-Control-Allow-Origin:
|
36
|
+
- "*"
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: '{"results":[{"query":"1 Infinite Loop Cupertino CA 95014","response":{"input":{"address_components":{"number":"1","street":"Infinite","suffix":"Loop","city":"Cupertino","state":"CA","zip":"95014"},"formatted_address":"1
|
40
|
+
Infinite Loop, Cupertino, CA 95014"},"results":[{"address_components":{"number":"1","street":"Infinite","suffix":"Loop","city":"Cupertino","county":"Santa
|
41
|
+
Clara County","state":"CA","zip":"95014"},"formatted_address":"1 Infinite
|
42
|
+
Loop, Cupertino, CA 95014","location":{"lat":37.331669,"lng":-122.03074},"accuracy":1}]}},{"query":"
|
43
|
+
, , ","response":{"error":"Could not parse address"}}]}'
|
44
|
+
http_version:
|
45
|
+
recorded_at: Thu, 10 Apr 2014 15:55:59 GMT
|
46
|
+
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,40 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: http://api.geocod.io/v1/geocode?api_key=secret_api_key&q=%20,%20,%20
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
Accept:
|
11
|
+
- application/json
|
12
|
+
Accept-Encoding:
|
13
|
+
- gzip;q=1.0,deflate;q=0.6,identity;q=0.3
|
14
|
+
User-Agent:
|
15
|
+
- Ruby
|
16
|
+
response:
|
17
|
+
status:
|
18
|
+
code: 422
|
19
|
+
message: Unprocessable Entity
|
20
|
+
headers:
|
21
|
+
Server:
|
22
|
+
- nginx/1.2.1
|
23
|
+
Content-Type:
|
24
|
+
- application/json
|
25
|
+
Transfer-Encoding:
|
26
|
+
- chunked
|
27
|
+
Connection:
|
28
|
+
- keep-alive
|
29
|
+
Cache-Control:
|
30
|
+
- no-cache
|
31
|
+
Date:
|
32
|
+
- Thu, 10 Apr 2014 15:54:12 GMT
|
33
|
+
Access-Control-Allow-Origin:
|
34
|
+
- "*"
|
35
|
+
body:
|
36
|
+
encoding: UTF-8
|
37
|
+
string: '{"error":"Could not parse address"}'
|
38
|
+
http_version:
|
39
|
+
recorded_at: Thu, 10 Apr 2014 15:54:12 GMT
|
40
|
+
recorded_with: VCR 2.8.0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geocodio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -110,10 +110,12 @@ files:
|
|
110
110
|
- spec/state_legislative_district_spec.rb
|
111
111
|
- spec/timezone_spec.rb
|
112
112
|
- spec/vcr_cassettes/batch_geocode.yml
|
113
|
+
- spec/vcr_cassettes/batch_geocode_with_bad_address.yml
|
113
114
|
- spec/vcr_cassettes/batch_geocode_with_fields.yml
|
114
115
|
- spec/vcr_cassettes/batch_reverse.yml
|
115
116
|
- spec/vcr_cassettes/batch_reverse_with_fields.yml
|
116
117
|
- spec/vcr_cassettes/geocode.yml
|
118
|
+
- spec/vcr_cassettes/geocode_bad_address.yml
|
117
119
|
- spec/vcr_cassettes/geocode_with_fields.yml
|
118
120
|
- spec/vcr_cassettes/invalid_key.yml
|
119
121
|
- spec/vcr_cassettes/parse.yml
|
@@ -153,10 +155,12 @@ test_files:
|
|
153
155
|
- spec/state_legislative_district_spec.rb
|
154
156
|
- spec/timezone_spec.rb
|
155
157
|
- spec/vcr_cassettes/batch_geocode.yml
|
158
|
+
- spec/vcr_cassettes/batch_geocode_with_bad_address.yml
|
156
159
|
- spec/vcr_cassettes/batch_geocode_with_fields.yml
|
157
160
|
- spec/vcr_cassettes/batch_reverse.yml
|
158
161
|
- spec/vcr_cassettes/batch_reverse_with_fields.yml
|
159
162
|
- spec/vcr_cassettes/geocode.yml
|
163
|
+
- spec/vcr_cassettes/geocode_bad_address.yml
|
160
164
|
- spec/vcr_cassettes/geocode_with_fields.yml
|
161
165
|
- spec/vcr_cassettes/invalid_key.yml
|
162
166
|
- spec/vcr_cassettes/parse.yml
|