geocoder-us 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright 2006 Eric Hodel, The Robot Co-op. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without
4
+ modification, are permitted provided that the following conditions
5
+ are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright
8
+ notice, this list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright
10
+ notice, this list of conditions and the following disclaimer in the
11
+ documentation and/or other materials provided with the distribution.
12
+ 3. Neither the names of the authors nor the names of their contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
17
+ OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
20
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
+ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22
+ OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23
+ BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
+ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
+
@@ -0,0 +1,7 @@
1
+ LICENSE
2
+ Manifest.txt
3
+ README
4
+ Rakefile
5
+ lib/geocoder_us.rb
6
+ test/test_geocoder_us.rb
7
+ test/uri_stub.rb
data/README ADDED
@@ -0,0 +1,42 @@
1
+ = geocoder-us
2
+
3
+ Rubyforge Project:
4
+
5
+ http://rubyforge.org/projects/rctools
6
+
7
+ Documentation:
8
+
9
+ http://dev.robotcoop.com/Libraries/geocoder-us
10
+
11
+ == About
12
+
13
+ geocoder-us allows you to look up coordinates for addresses using geocoder.us.
14
+
15
+ == Installing geocoder-us
16
+
17
+ Just install the gem:
18
+
19
+ $ sudo gem install geocoder-us
20
+
21
+ == Using geocoder-us
22
+
23
+ The easy way:
24
+
25
+ require 'rubygems'
26
+ require 'geocoder-us'
27
+
28
+ p GeocoderUs.new.locate('1924 E Denny Way, Seattle, WA')
29
+
30
+ If you like you can use a geocoder.us account. You can sign up for an account
31
+ here:
32
+
33
+ http://geocoder.us/user/signup
34
+
35
+ And here's how you use your account:
36
+
37
+ require 'rubygems'
38
+ require 'geocoder-us'
39
+
40
+ gu = GeocoderUs.new 'username', 'password'
41
+ p gu.locate('1924 E Denny Way, Seattle, WA')
42
+
@@ -0,0 +1,66 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ $VERBOSE = nil
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'geocoder-us'
11
+ s.version = '1.0.0'
12
+ s.summary = 'Geocoder.us API Library'
13
+ s.description = 'Map addresses to latitude and longitude using geocoder.us'
14
+ s.author = 'Eric Hodel'
15
+ s.email = 'eric@robotcoop.com'
16
+
17
+ s.has_rdoc = true
18
+ s.files = File.read('Manifest.txt').split($/)
19
+ s.require_path = 'lib'
20
+ end
21
+
22
+ desc 'Run tests'
23
+ task :default => [ :test ]
24
+
25
+ Rake::TestTask.new('test') do |t|
26
+ t.libs << 'test'
27
+ t.pattern = 'test/test_*.rb'
28
+ t.verbose = true
29
+ end
30
+
31
+ desc 'Update Manifest.txt'
32
+ task :update_manifest do
33
+ sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
34
+ end
35
+
36
+ desc 'Generate RDoc'
37
+ Rake::RDocTask.new :rdoc do |rd|
38
+ rd.rdoc_dir = 'doc'
39
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
40
+ rd.main = 'README'
41
+ rd.options << '-d' if `which dot` =~ /\/dot/
42
+ rd.options << '-t Geocoder.us'
43
+ end
44
+
45
+ desc 'Generate RDoc for dev.robotcoop.com'
46
+ Rake::RDocTask.new :dev_rdoc do |rd|
47
+ rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/geocoder-us'
48
+ rd.rdoc_files.add 'lib', 'README', 'LICENSE'
49
+ rd.main = 'README'
50
+ rd.options << '-d' if `which dot` =~ /\/dot/
51
+ rd.options << '-t Geocoder.us'
52
+ end
53
+
54
+ desc 'Build Gem'
55
+ Rake::GemPackageTask.new spec do |pkg|
56
+ pkg.need_tar = true
57
+ end
58
+
59
+ desc 'Clean up'
60
+ task :clean => [ :clobber_rdoc, :clobber_package ]
61
+
62
+ desc 'Clean up'
63
+ task :clobber => [ :clean ]
64
+
65
+ # vim: syntax=Ruby
66
+
@@ -0,0 +1,64 @@
1
+ require 'open-uri'
2
+
3
+ ##
4
+ # A library for lookup up coordinates with geocoder.us' API.
5
+ #
6
+ # http://geocoder.us/help/
7
+
8
+ class GeocoderUs
9
+
10
+ ##
11
+ # Base error class
12
+
13
+ class Error < RuntimeError; end
14
+
15
+ ##
16
+ # Raised if you give an unlocatable address.
17
+
18
+ class AddressError < Error; end
19
+
20
+ ##
21
+ # Raised if you give a bad username/password pair.
22
+
23
+ class LoginError < Error; end
24
+
25
+ ##
26
+ # Creates a new GeocoderUs object optionally using +username+ and
27
+ # +password+.
28
+ #
29
+ # You can sign up for a geocoder.us account here:
30
+ #
31
+ # http://geocoder.us/user/signup
32
+
33
+ def initialize(user = nil, password = nil)
34
+ if user and password then
35
+ @url = URI.parse 'http://geocoder.us/member/service/csv'
36
+ @url.user = user
37
+ @url.password = password
38
+ else
39
+ @url = URI.parse 'http://rpc.geocoder.us/service/csv'
40
+ end
41
+ end
42
+
43
+ ##
44
+ # Locates +address+ and returns the address' latitude and longitude or
45
+ # raises an AddressError.
46
+
47
+ 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 /
61
+ end
62
+
63
+ end
64
+
@@ -0,0 +1,57 @@
1
+ require 'test/unit'
2
+ require 'test/uri_stub'
3
+ require 'geocoder_us'
4
+
5
+ class TestGeocoderUs < Test::Unit::TestCase
6
+
7
+ def setup
8
+ URI::HTTP.responses = []
9
+ URI::HTTP.uris = []
10
+
11
+ @gu = GeocoderUs.new
12
+ end
13
+
14
+ def test_locate
15
+ URI::HTTP.responses << <<-EOF
16
+ 47.618620,-122.306942,1924 E Denny Way,Seattle,WA,98122
17
+ EOF
18
+
19
+ assert_equal [47.618620, -122.306942],
20
+ @gu.locate('1924 E Denny Way, Seattle, WA')
21
+ assert_equal true, URI::HTTP.responses.empty?
22
+ assert_equal 1, URI::HTTP.uris.length
23
+ assert_equal 'http://rpc.geocoder.us/service/csv?address=1924%20E%20Denny%20Way,%20Seattle,%20WA',
24
+ URI::HTTP.uris.first
25
+ end
26
+
27
+ def test_locate_with_account
28
+ URI::HTTP.responses << <<-EOF
29
+ 47.618620,-122.306942,1924 E Denny Way,Seattle,WA,98122
30
+ EOF
31
+
32
+ gu = GeocoderUs.new 'username', 'password'
33
+ assert_equal [47.618620, -122.306942],
34
+ gu.locate('1924 E Denny Way, Seattle, WA')
35
+ assert_equal true, URI::HTTP.responses.empty?
36
+ 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',
38
+ URI::HTTP.uris.first
39
+ end
40
+
41
+ def test_locate_bad_address
42
+ URI::HTTP.responses << '2: couldn\'t find this address! sorry'
43
+
44
+ @gu.locate('yuck')
45
+ rescue GeocoderUs::AddressError => e
46
+ assert_equal 'couldn\'t find this address! sorry', e.message
47
+
48
+ assert_equal true, URI::HTTP.responses.empty?
49
+ assert_equal 1, URI::HTTP.uris.length
50
+ assert_equal 'http://rpc.geocoder.us/service/csv?address=yuck',
51
+ URI::HTTP.uris.first
52
+ else
53
+ flunk 'Expected GeocoderUs::AddressError'
54
+ end
55
+
56
+ end
57
+
@@ -0,0 +1,17 @@
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
+
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11.15
3
+ specification_version: 1
4
+ name: geocoder-us
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2006-06-13 00:00:00 -07:00
8
+ summary: Geocoder.us API Library
9
+ require_paths:
10
+ - lib
11
+ email: eric@robotcoop.com
12
+ homepage:
13
+ rubyforge_project:
14
+ description: Map addresses to latitude and longitude using geocoder.us
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Eric Hodel
31
+ files:
32
+ - LICENSE
33
+ - Manifest.txt
34
+ - README
35
+ - Rakefile
36
+ - lib/geocoder_us.rb
37
+ - test/test_geocoder_us.rb
38
+ - test/uri_stub.rb
39
+ test_files: []
40
+
41
+ rdoc_options: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ executables: []
46
+
47
+ extensions: []
48
+
49
+ requirements: []
50
+
51
+ dependencies: []
52
+