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.
@@ -4,4 +4,3 @@ README
4
4
  Rakefile
5
5
  lib/geocoder_us.rb
6
6
  test/test_geocoder_us.rb
7
- test/uri_stub.rb
data/README CHANGED
@@ -35,7 +35,7 @@ http://geocoder.us/user/signup
35
35
  And here's how you use your account:
36
36
 
37
37
  require 'rubygems'
38
- require 'geocoder-us'
38
+ require 'geocoder_us'
39
39
 
40
40
  gu = GeocoderUs.new 'username', 'password'
41
41
  p gu.locate('1924 E Denny Way, Seattle, WA')
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.0.0'
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 << 'test'
28
+ t.libs << '../rc-rest/lib'
27
29
  t.pattern = 'test/test_*.rb'
28
30
  t.verbose = true
29
31
  end
@@ -1,16 +1,18 @@
1
- require 'open-uri'
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 < RuntimeError; end
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/csv'
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/csv'
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
- url = @url.dup
49
- url.query = "address=#{URI.escape address}"
50
-
51
- url.open do |f|
52
- response = f.read
53
- raise AddressError, $' if response =~ /^2: /
54
- lat, lon, = response.split ',', 3
55
- lat = lat.to_f
56
- lon = lon.to_f
57
- return lat, lon
58
- end
59
- rescue OpenURI::HTTPError => e
60
- raise LoginError if e.message =~ /^401 /
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
@@ -1,5 +1,5 @@
1
1
  require 'test/unit'
2
- require 'test/uri_stub'
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
- 47.618620,-122.306942,1924 E Denny Way,Seattle,WA,98122
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
- assert_equal [47.618620, -122.306942],
20
- @gu.locate('1924 E Denny Way, Seattle, WA')
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/csv?address=1924%20E%20Denny%20Way,%20Seattle,%20WA',
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
- 47.618620,-122.306942,1924 E Denny Way,Seattle,WA,98122
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
- assert_equal [47.618620, -122.306942],
34
- gu.locate('1924 E Denny Way, Seattle, WA')
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/csv?address=1924%20E%20Denny%20Way,%20Seattle,%20WA',
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 << '2: couldn\'t find this address! sorry'
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/csv?address=yuck',
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.11.15
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.0.0
7
- date: 2006-06-13 00:00:00 -07:00
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:
@@ -1,17 +0,0 @@
1
- require 'open-uri'
2
-
3
- class URI::HTTP
4
-
5
- class << self
6
- attr_accessor :responses, :uris
7
- end
8
-
9
- alias original_open open
10
-
11
- def open
12
- self.class.uris << self.to_s
13
- yield StringIO.new(self.class.responses.shift)
14
- end
15
-
16
- end
17
-