geocoder-us 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +0 -1
- data/README +1 -1
- data/Rakefile +4 -2
- data/lib/geocoder_us.rb +40 -18
- data/test/test_geocoder_us.rb +51 -13
- metadata +13 -6
- data/test/uri_stub.rb +0 -17
data/Manifest.txt
CHANGED
data/README
CHANGED
data/Rakefile
CHANGED
@@ -8,7 +8,7 @@ $VERBOSE = nil
|
|
8
8
|
|
9
9
|
spec = Gem::Specification.new do |s|
|
10
10
|
s.name = 'geocoder-us'
|
11
|
-
s.version = '1.
|
11
|
+
s.version = '1.1.0'
|
12
12
|
s.summary = 'Geocoder.us API Library'
|
13
13
|
s.description = 'Map addresses to latitude and longitude using geocoder.us'
|
14
14
|
s.author = 'Eric Hodel'
|
@@ -17,13 +17,15 @@ spec = Gem::Specification.new do |s|
|
|
17
17
|
s.has_rdoc = true
|
18
18
|
s.files = File.read('Manifest.txt').split($/)
|
19
19
|
s.require_path = 'lib'
|
20
|
+
|
21
|
+
s.add_dependency 'rc-rest', '>= 1.0.0'
|
20
22
|
end
|
21
23
|
|
22
24
|
desc 'Run tests'
|
23
25
|
task :default => [ :test ]
|
24
26
|
|
25
27
|
Rake::TestTask.new('test') do |t|
|
26
|
-
t.libs << '
|
28
|
+
t.libs << '../rc-rest/lib'
|
27
29
|
t.pattern = 'test/test_*.rb'
|
28
30
|
t.verbose = true
|
29
31
|
end
|
data/lib/geocoder_us.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
require '
|
1
|
+
require 'rc_rest'
|
2
2
|
|
3
3
|
##
|
4
4
|
# A library for lookup up coordinates with geocoder.us' API.
|
5
5
|
#
|
6
6
|
# http://geocoder.us/help/
|
7
7
|
|
8
|
-
class GeocoderUs
|
8
|
+
class GeocoderUs < RCRest
|
9
|
+
|
10
|
+
Location = Struct.new :latitude, :longitude, :address
|
9
11
|
|
10
12
|
##
|
11
13
|
# Base error class
|
12
14
|
|
13
|
-
class Error <
|
15
|
+
class Error < RCRest::Error; end
|
14
16
|
|
15
17
|
##
|
16
18
|
# Raised if you give an unlocatable address.
|
@@ -32,11 +34,11 @@ class GeocoderUs
|
|
32
34
|
|
33
35
|
def initialize(user = nil, password = nil)
|
34
36
|
if user and password then
|
35
|
-
@url = URI.parse 'http://geocoder.us/member/service/
|
37
|
+
@url = URI.parse 'http://geocoder.us/member/service/rest/geocode'
|
36
38
|
@url.user = user
|
37
39
|
@url.password = password
|
38
40
|
else
|
39
|
-
@url = URI.parse 'http://rpc.geocoder.us/service/
|
41
|
+
@url = URI.parse 'http://rpc.geocoder.us/service/rest/geocode'
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
@@ -45,19 +47,39 @@ class GeocoderUs
|
|
45
47
|
# raises an AddressError.
|
46
48
|
|
47
49
|
def locate(address)
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
50
|
+
get :address => address
|
51
|
+
end
|
52
|
+
|
53
|
+
def parse_response(xml)
|
54
|
+
location = Location.new
|
55
|
+
location.address = xml.elements['rdf:RDF/geo:Point/dc:description'].text
|
56
|
+
|
57
|
+
location.latitude = xml.elements['rdf:RDF/geo:Point/geo:lat'].text.to_f
|
58
|
+
location.longitude = xml.elements['rdf:RDF/geo:Point/geo:long'].text.to_f
|
59
|
+
|
60
|
+
return location
|
61
|
+
end
|
62
|
+
|
63
|
+
def check_error(xml)
|
64
|
+
raise AddressError, xml.text if xml.text == 'couldn\'t find this address! sorry'
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# A Location contains the following fields:
|
71
|
+
#
|
72
|
+
# +latitude+:: Latitude of the location
|
73
|
+
# +longitude+:: Longitude of the location
|
74
|
+
# +address+:: Street address of the result.
|
75
|
+
|
76
|
+
class GeocoderUs::Location
|
77
|
+
|
78
|
+
##
|
79
|
+
# The coordinates for this location.
|
80
|
+
|
81
|
+
def coordinates
|
82
|
+
return [latitude, longitude]
|
61
83
|
end
|
62
84
|
|
63
85
|
end
|
data/test/test_geocoder_us.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
require '
|
2
|
+
require 'rc_rest/uri_stub'
|
3
3
|
require 'geocoder_us'
|
4
4
|
|
5
5
|
class TestGeocoderUs < Test::Unit::TestCase
|
@@ -12,34 +12,72 @@ class TestGeocoderUs < Test::Unit::TestCase
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def test_locate
|
15
|
-
URI::HTTP.responses << <<-EOF
|
16
|
-
|
15
|
+
URI::HTTP.responses << <<-EOF.strip
|
16
|
+
<?xml version="1.0"?>
|
17
|
+
<rdf:RDF
|
18
|
+
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
19
|
+
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
|
20
|
+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
21
|
+
>
|
22
|
+
|
23
|
+
<geo:Point rdf:nodeID="aid86937982">
|
24
|
+
|
25
|
+
<dc:description>1600 Pennsylvania Ave NW, Washington DC 20502</dc:description>
|
26
|
+
<geo:long>-77.037684</geo:long>
|
27
|
+
<geo:lat>38.898748</geo:lat>
|
28
|
+
|
29
|
+
</geo:Point>
|
30
|
+
|
31
|
+
|
32
|
+
</rdf:RDF>
|
33
|
+
|
17
34
|
EOF
|
18
35
|
|
19
|
-
|
20
|
-
|
36
|
+
location = @gu.locate('1600 Pennsylvania Ave, Washington DC')
|
37
|
+
assert_equal [38.898748, -77.037684], location.coordinates
|
38
|
+
assert_equal '1600 Pennsylvania Ave NW, Washington DC 20502',
|
39
|
+
location.address
|
40
|
+
|
21
41
|
assert_equal true, URI::HTTP.responses.empty?
|
22
42
|
assert_equal 1, URI::HTTP.uris.length
|
23
|
-
assert_equal 'http://rpc.geocoder.us/service/
|
43
|
+
assert_equal 'http://rpc.geocoder.us/service/rest/geocode?address=1600%20Pennsylvania%20Ave,%20Washington%20DC',
|
24
44
|
URI::HTTP.uris.first
|
25
45
|
end
|
26
46
|
|
27
47
|
def test_locate_with_account
|
28
|
-
URI::HTTP.responses << <<-EOF
|
29
|
-
|
48
|
+
URI::HTTP.responses << <<-EOF.strip
|
49
|
+
<?xml version="1.0"?>
|
50
|
+
<rdf:RDF
|
51
|
+
xmlns:dc="http://purl.org/dc/elements/1.1/"
|
52
|
+
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
|
53
|
+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
54
|
+
>
|
55
|
+
|
56
|
+
<geo:Point rdf:nodeID="aid86937982">
|
57
|
+
|
58
|
+
<dc:description>1600 Pennsylvania Ave NW, Washington DC 20502</dc:description>
|
59
|
+
<geo:long>-77.037684</geo:long>
|
60
|
+
<geo:lat>38.898748</geo:lat>
|
61
|
+
|
62
|
+
</geo:Point>
|
63
|
+
|
64
|
+
|
65
|
+
</rdf:RDF>
|
66
|
+
|
30
67
|
EOF
|
31
68
|
|
32
69
|
gu = GeocoderUs.new 'username', 'password'
|
33
|
-
|
34
|
-
|
70
|
+
location = gu.locate('1600 Pennsylvania Ave, Washington DC')
|
71
|
+
assert_equal [38.898748, -77.037684], location.coordinates
|
72
|
+
|
35
73
|
assert_equal true, URI::HTTP.responses.empty?
|
36
74
|
assert_equal 1, URI::HTTP.uris.length
|
37
|
-
assert_equal 'http://username:password@geocoder.us/member/service/
|
75
|
+
assert_equal 'http://username:password@geocoder.us/member/service/rest/geocode?address=1600%20Pennsylvania%20Ave,%20Washington%20DC',
|
38
76
|
URI::HTTP.uris.first
|
39
77
|
end
|
40
78
|
|
41
79
|
def test_locate_bad_address
|
42
|
-
URI::HTTP.responses << '
|
80
|
+
URI::HTTP.responses << 'couldn\'t find this address! sorry'
|
43
81
|
|
44
82
|
@gu.locate('yuck')
|
45
83
|
rescue GeocoderUs::AddressError => e
|
@@ -47,7 +85,7 @@ class TestGeocoderUs < Test::Unit::TestCase
|
|
47
85
|
|
48
86
|
assert_equal true, URI::HTTP.responses.empty?
|
49
87
|
assert_equal 1, URI::HTTP.uris.length
|
50
|
-
assert_equal 'http://rpc.geocoder.us/service/
|
88
|
+
assert_equal 'http://rpc.geocoder.us/service/rest/geocode?address=yuck',
|
51
89
|
URI::HTTP.uris.first
|
52
90
|
else
|
53
91
|
flunk 'Expected GeocoderUs::AddressError'
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.8.
|
2
|
+
rubygems_version: 0.8.99
|
3
3
|
specification_version: 1
|
4
4
|
name: geocoder-us
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.
|
7
|
-
date: 2006-06-
|
6
|
+
version: 1.1.0
|
7
|
+
date: 2006-06-16 00:00:00 -07:00
|
8
8
|
summary: Geocoder.us API Library
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -35,7 +35,6 @@ files:
|
|
35
35
|
- Rakefile
|
36
36
|
- lib/geocoder_us.rb
|
37
37
|
- test/test_geocoder_us.rb
|
38
|
-
- test/uri_stub.rb
|
39
38
|
test_files: []
|
40
39
|
|
41
40
|
rdoc_options: []
|
@@ -48,5 +47,13 @@ extensions: []
|
|
48
47
|
|
49
48
|
requirements: []
|
50
49
|
|
51
|
-
dependencies:
|
52
|
-
|
50
|
+
dependencies:
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rc-rest
|
53
|
+
version_requirement:
|
54
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.0.0
|
59
|
+
version:
|
data/test/uri_stub.rb
DELETED