yahoo-geocode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +27 -0
- data/Manifest.txt +6 -0
- data/README +35 -0
- data/Rakefile +73 -0
- data/lib/yahoo/geocode.rb +103 -0
- data/test/test_geocode.rb +61 -0
- metadata +59 -0
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
|
+
|
data/Manifest.txt
ADDED
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
= yahoo-geocode
|
2
|
+
|
3
|
+
Rubyforge Project:
|
4
|
+
|
5
|
+
http://rubyforge.org/projects/rctools/
|
6
|
+
|
7
|
+
Documentation:
|
8
|
+
|
9
|
+
http://dev.robotcoop.com/Libraries/yahoo-geocode/
|
10
|
+
|
11
|
+
== About
|
12
|
+
|
13
|
+
yahoo-geocode implements version 1 of Yahoo's Maps Web Services Geocoding API.
|
14
|
+
|
15
|
+
== Installing yahoo-geocode
|
16
|
+
|
17
|
+
Just install the gem:
|
18
|
+
|
19
|
+
$ sudo gem install yahoo-geocode
|
20
|
+
|
21
|
+
== Using yahoo-geocode
|
22
|
+
|
23
|
+
First you'll need a Yahoo Application ID. You can register for one here:
|
24
|
+
|
25
|
+
http://api.search.yahoo.com/webservices/register_application
|
26
|
+
|
27
|
+
Then you create a Yahoo::Geocode object and start locating addresses:
|
28
|
+
|
29
|
+
require 'rubygems'
|
30
|
+
require 'yahoo/geocode'
|
31
|
+
|
32
|
+
yg = Yahoo::Geocode.new application_id
|
33
|
+
locations = yg.locate '701 First Street, Sunnyvale, CA'
|
34
|
+
p location.first.coordinates
|
35
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,73 @@
|
|
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 = 'yahoo-geocode'
|
11
|
+
s.version = '1.0.0'
|
12
|
+
s.summary = 'A Ruby Yahoo Geocoder Library'
|
13
|
+
s.description = 'An interface to Yahoo\'s Geocoder service.
|
14
|
+
|
15
|
+
http://developer.yahoo.com/maps/rest/V1/geocode.html'
|
16
|
+
|
17
|
+
s.author = 'Eric Hodel'
|
18
|
+
s.email = 'eric@robotcoop.com'
|
19
|
+
|
20
|
+
s.has_rdoc = true
|
21
|
+
s.files = File.read('Manifest.txt').split($/)
|
22
|
+
s.require_path = 'lib'
|
23
|
+
|
24
|
+
s.add_dependency 'yahoo', '1.0.0'
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Run tests'
|
28
|
+
task :default => [ :test ]
|
29
|
+
|
30
|
+
Rake::TestTask.new('test') do |t|
|
31
|
+
t.libs << 'test'
|
32
|
+
t.libs << '../yahoo'
|
33
|
+
t.libs << '../yahoo/lib'
|
34
|
+
t.pattern = 'test/test_*.rb'
|
35
|
+
t.verbose = true
|
36
|
+
end
|
37
|
+
|
38
|
+
desc 'Update Manifest.txt'
|
39
|
+
task :update_manifest do
|
40
|
+
sh "find . -type f | sed -e 's%./%%' | egrep -v 'svn|swp|~' | egrep -v '^(doc|pkg)/' | sort > Manifest.txt"
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Generate RDoc'
|
44
|
+
Rake::RDocTask.new :rdoc do |rd|
|
45
|
+
rd.rdoc_dir = 'doc'
|
46
|
+
rd.rdoc_files.add 'lib', 'README', 'LICENSE'
|
47
|
+
rd.main = 'README'
|
48
|
+
rd.options << '-d' if `which dot` =~ /\/dot/
|
49
|
+
rd.options << '-t Yahoo Maps Web Services Geocoding API'
|
50
|
+
end
|
51
|
+
|
52
|
+
desc 'Generate RDoc for dev.robotcoop.com'
|
53
|
+
Rake::RDocTask.new :dev_rdoc do |rd|
|
54
|
+
rd.rdoc_dir = '../../../www/trunk/dev/html/Libraries/yahoo-geocode'
|
55
|
+
rd.rdoc_files.add 'lib', 'README', 'LICENSE'
|
56
|
+
rd.main = 'README'
|
57
|
+
rd.options << '-d' if `which dot` =~ /\/dot/
|
58
|
+
rd.options << '-t Yahoo Maps Web Services Geocoding API'
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Build Gem'
|
62
|
+
Rake::GemPackageTask.new spec do |pkg|
|
63
|
+
pkg.need_tar = true
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Clean up'
|
67
|
+
task :clean => [ :clobber_rdoc, :clobber_package ]
|
68
|
+
|
69
|
+
desc 'Clean up'
|
70
|
+
task :clobber => [ :clean ]
|
71
|
+
|
72
|
+
# vim: syntax=Ruby
|
73
|
+
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'yahoo'
|
2
|
+
|
3
|
+
##
|
4
|
+
# Yahoo geocoding API.
|
5
|
+
#
|
6
|
+
# http://developer.yahoo.com/maps/rest/V1/geocode.html
|
7
|
+
|
8
|
+
class Yahoo::Geocode < Yahoo
|
9
|
+
|
10
|
+
##
|
11
|
+
# Location result Struct.
|
12
|
+
|
13
|
+
Location = Struct.new :latitude, :longitude, :address, :city, :state, :zip,
|
14
|
+
:country, :precision, :warning
|
15
|
+
|
16
|
+
def initialize(*args) # :nodoc:
|
17
|
+
@host = 'api.local.yahoo.com'
|
18
|
+
@service_name = 'MapsService'
|
19
|
+
@version = 'V1'
|
20
|
+
@method = 'geocode'
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
24
|
+
##
|
25
|
+
# Returns a Location for +address+.
|
26
|
+
#
|
27
|
+
# The +address+ can be any of:
|
28
|
+
# * city, state
|
29
|
+
# * city, state, zip
|
30
|
+
# * zip
|
31
|
+
# * street, city, state
|
32
|
+
# * street, city, state, zip
|
33
|
+
# * street, zip
|
34
|
+
|
35
|
+
def locate(address)
|
36
|
+
get :location => address
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_response(xml) # :nodoc:
|
40
|
+
addresses = []
|
41
|
+
|
42
|
+
xml.elements['ResultSet'].each do |r|
|
43
|
+
address = Location.new
|
44
|
+
|
45
|
+
address.precision = r.attributes['precision']
|
46
|
+
|
47
|
+
if r.attributes.include? 'warning' then
|
48
|
+
address.warning = r.attributes['warning']
|
49
|
+
end
|
50
|
+
|
51
|
+
address.latitude = r.elements['Latitude'].text.to_f
|
52
|
+
address.longitude = r.elements['Longitude'].text.to_f
|
53
|
+
|
54
|
+
address.address = r.elements['Address'].text
|
55
|
+
address.city = r.elements['City'].text
|
56
|
+
address.state = r.elements['State'].text
|
57
|
+
address.zip = r.elements['Zip'].text
|
58
|
+
address.country = r.elements['Country'].text
|
59
|
+
|
60
|
+
addresses << address
|
61
|
+
end
|
62
|
+
|
63
|
+
return addresses
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
##
|
69
|
+
# A Location contains the following fields:
|
70
|
+
#
|
71
|
+
# +latitude+:: Latitude of the location
|
72
|
+
# +longitude+:: Longitude of the location
|
73
|
+
# +address+:: Street address of the result, if a specific location could be
|
74
|
+
# determined
|
75
|
+
# +city+:: City in which the result is located
|
76
|
+
# +state+:: State in which the result is located
|
77
|
+
# +zip+:: Zip code, if known
|
78
|
+
# +country+:: Country in which the result is located
|
79
|
+
# +precision+:: The precision of the address used for geocoding
|
80
|
+
# +warning+:: If the exact address was not found, the closest available match
|
81
|
+
#
|
82
|
+
# Precision can be one of the following, from most specific to most general:
|
83
|
+
#
|
84
|
+
# * address
|
85
|
+
# * street
|
86
|
+
# * zip+4
|
87
|
+
# * zip+2
|
88
|
+
# * zip
|
89
|
+
# * city
|
90
|
+
# * state
|
91
|
+
# * country
|
92
|
+
|
93
|
+
class Yahoo::Geocode::Location
|
94
|
+
|
95
|
+
##
|
96
|
+
# Returns an Array with latitude and longitude.
|
97
|
+
|
98
|
+
def coordinates
|
99
|
+
[latitude, longitude]
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'test/uri_stub'
|
3
|
+
require 'yahoo/geocode'
|
4
|
+
|
5
|
+
class Yahoo::TestGeocode < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
URI::HTTP.responses = []
|
9
|
+
URI::HTTP.uris = []
|
10
|
+
|
11
|
+
@g = Yahoo::Geocode.new 'APP_ID'
|
12
|
+
@location = '701 First Street, Sunnyvale, CA'
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_locate
|
16
|
+
URI::HTTP.responses << <<-EOF.strip
|
17
|
+
<?xml version="1.0"?>
|
18
|
+
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd"><Result precision="address" warning="The exact location could not be found, here is the closest match: 701 First Ave, Sunnyvale, CA 94089"><Latitude>37.416384</Latitude><Longitude>-122.024853</Longitude><Address>701 FIRST AVE</Address><City>SUNNYVALE</City><State>CA</State><Zip>94089-1019</Zip><Country>US</Country></Result></ResultSet>
|
19
|
+
<!-- ws02.search.scd.yahoo.com compressed/chunked Thu Jun 8 15:20:46 PDT 2006 -->
|
20
|
+
EOF
|
21
|
+
|
22
|
+
assert_equal [37.416384, -122.024853],
|
23
|
+
@g.locate('701 First Street, Sunnyvale, CA').first.coordinates
|
24
|
+
assert_equal true, URI::HTTP.responses.empty?
|
25
|
+
assert_equal 1, URI::HTTP.uris.length
|
26
|
+
assert_equal 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=APP_ID&location=701%20First%20Street,%20Sunnyvale,%20CA&output=xml',
|
27
|
+
URI::HTTP.uris.first
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_locate_bad_address
|
31
|
+
URI::HTTP.responses << <<-EOF.strip
|
32
|
+
<?xml version="1.0" encoding="UTF-8"?> <Error xmlns="urn:yahoo:api">
|
33
|
+
The following errors were detected:
|
34
|
+
<Message>unable to parse location</Message></Error>
|
35
|
+
<!-- ws02.search.scd.yahoo.com uncompressed/chunked Thu Jun 8 15:53:20 PDT 2006 -->
|
36
|
+
EOF
|
37
|
+
|
38
|
+
assert_raise Yahoo::Error do
|
39
|
+
@g.locate 'yucksthoeusthaoeusnhtaosu'
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_equal true, URI::HTTP.responses.empty?
|
43
|
+
assert_equal 1, URI::HTTP.uris.length
|
44
|
+
assert_equal 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=APP_ID&location=yucksthoeusthaoeusnhtaosu&output=xml',
|
45
|
+
URI::HTTP.uris.first
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class Yahoo::Geocode::TestLocation < Test::Unit::TestCase
|
51
|
+
|
52
|
+
def test_coordinates
|
53
|
+
loc = Yahoo::Geocode::Location.new
|
54
|
+
loc.latitude = Math::E
|
55
|
+
loc.longitude = Math::PI
|
56
|
+
|
57
|
+
assert_equal [Math::E, Math::PI], loc.coordinates
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11.15
|
3
|
+
specification_version: 1
|
4
|
+
name: yahoo-geocode
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-06-10 00:00:00 -07:00
|
8
|
+
summary: A Ruby Yahoo Geocoder Library
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: eric@robotcoop.com
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description: An interface to Yahoo's Geocoder service. http://developer.yahoo.com/maps/rest/V1/geocode.html
|
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/yahoo/geocode.rb
|
37
|
+
- test/test_geocode.rb
|
38
|
+
test_files: []
|
39
|
+
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
executables: []
|
45
|
+
|
46
|
+
extensions: []
|
47
|
+
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
dependencies:
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: yahoo
|
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:
|