mattt-yahoo-geoplanet 0.1.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/README +91 -0
- data/lib/rest.rb +49 -0
- data/lib/yahoo-geoplanet.rb +24 -0
- data/lib/yahoo-geoplanet/base.rb +48 -0
- data/lib/yahoo-geoplanet/place.rb +73 -0
- data/lib/yahoo-geoplanet/version.rb +11 -0
- data/yahoo-geoplanet.gemspec +27 -0
- metadata +77 -0
data/README
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
= yahoo-geoplanet
|
|
2
|
+
|
|
3
|
+
A Ruby wrapper for the Yahoo! GeoPlanet APIs.
|
|
4
|
+
|
|
5
|
+
== Example Usage
|
|
6
|
+
|
|
7
|
+
=== Searching for a Location:
|
|
8
|
+
|
|
9
|
+
require 'yahoo-geoplanet'
|
|
10
|
+
include Yahoo::GeoPlanet
|
|
11
|
+
Yahoo::GeoPlanet.app_id = [Your App ID Here]
|
|
12
|
+
|
|
13
|
+
# Returns first 20 results by default
|
|
14
|
+
Place.search("Springfield")
|
|
15
|
+
|
|
16
|
+
# ...but let's say you want _all_ of them
|
|
17
|
+
Place.search("Springfield", :count => 0) # Returns all 110
|
|
18
|
+
|
|
19
|
+
# You can pass in any Matrix or Query parameters this way too
|
|
20
|
+
# For more details see the following URLs:
|
|
21
|
+
# http://developer.yahoo.com/geo/guide/resources_and_collections.html#matrix_parameters
|
|
22
|
+
# http://developer.yahoo.com/geo/guide/resources_and_collections.html#query_parameters
|
|
23
|
+
|
|
24
|
+
=== Initializing by Where On Earth ID && Associations
|
|
25
|
+
|
|
26
|
+
require 'yahoo-geoplanet'
|
|
27
|
+
include Yahoo::GeoPlanet
|
|
28
|
+
Yahoo::GeoPlanet.app_id = [Your App ID Here]
|
|
29
|
+
|
|
30
|
+
cmu = Place.new(23511275) # WoE ID for Carnegie Mellon University
|
|
31
|
+
|
|
32
|
+
# Everything you get back from the API you have direct access to
|
|
33
|
+
# through the Place object. For example:
|
|
34
|
+
|
|
35
|
+
puts cmu.type # "Point of Interest"
|
|
36
|
+
puts cmu.county # "Allegheny"
|
|
37
|
+
puts cmu.latitude # 40.444
|
|
38
|
+
# Latitude and Longitude are values at Centroid
|
|
39
|
+
puts cmu.bounding_box # [[40.44445, -79.943314], [40.44355, -79.944511]]
|
|
40
|
+
# Bounding box are NW / SE coordinates in array
|
|
41
|
+
|
|
42
|
+
# We unlock the true power of GeoPlanet with association collections
|
|
43
|
+
# Check out this truly amazing stuff:
|
|
44
|
+
|
|
45
|
+
# The containing GeoPlanet entity for CMU
|
|
46
|
+
puts cmu.parent # "Squirrel Hill North"
|
|
47
|
+
|
|
48
|
+
# A list of other points of interest in the area
|
|
49
|
+
cmu.siblings.each{|s| puts s}
|
|
50
|
+
|
|
51
|
+
# A complete hierarchy, from country down to suburb
|
|
52
|
+
cmu.ancestors.each{|s| puts s}
|
|
53
|
+
|
|
54
|
+
== REQUIREMENTS:
|
|
55
|
+
|
|
56
|
+
To use this library, you must have a valid Yahoo! App ID.
|
|
57
|
+
You can get one at http://developer.yahoo.com/wsregapp/
|
|
58
|
+
|
|
59
|
+
Additionally, yahoo-geoplanet has the following gem dependencies:
|
|
60
|
+
|
|
61
|
+
* Hpricot >= 0.6
|
|
62
|
+
* ActiveSupport >= 2.1.0
|
|
63
|
+
|
|
64
|
+
== INSTALL:
|
|
65
|
+
|
|
66
|
+
* sudo gem install yahoo-geoplanet
|
|
67
|
+
|
|
68
|
+
== LICENSE:
|
|
69
|
+
|
|
70
|
+
(The MIT License)
|
|
71
|
+
|
|
72
|
+
Copyright (c) 2008 Mattt Thompson
|
|
73
|
+
|
|
74
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
75
|
+
a copy of this software and associated documentation files (the
|
|
76
|
+
'Software'), to deal in the Software without restriction, including
|
|
77
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
78
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
79
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
80
|
+
the following conditions:
|
|
81
|
+
|
|
82
|
+
The above copyright notice and this permission notice shall be
|
|
83
|
+
included in all copies or substantial portions of the Software.
|
|
84
|
+
|
|
85
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
88
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
89
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
90
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
91
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/rest.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Ported from John Nunemaker's Scrobbler Gem (http://scrobbler.rubyforge.org/)
|
|
2
|
+
|
|
3
|
+
require 'net/https'
|
|
4
|
+
|
|
5
|
+
module REST
|
|
6
|
+
class Connection
|
|
7
|
+
def initialize(base_url, args = {})
|
|
8
|
+
@base_url = base_url
|
|
9
|
+
@username = args['username']
|
|
10
|
+
@password = args['password']
|
|
11
|
+
@app_id = args['app_id']
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def get(resource, args = nil)
|
|
15
|
+
request(resource, "get", args)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def post(resource, args = nil)
|
|
19
|
+
request(resource, "post", args)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def request(resource, method = "get", args = {})
|
|
23
|
+
url = URI.join(@base_url, resource)
|
|
24
|
+
|
|
25
|
+
if args = args.update('appid' => @app_id)
|
|
26
|
+
# TODO: What about keys without value?
|
|
27
|
+
url.query = args.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)] }.join("&")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
case method
|
|
31
|
+
when "get"
|
|
32
|
+
req = Net::HTTP::Get.new(url.request_uri)
|
|
33
|
+
when "post"
|
|
34
|
+
req = Net::HTTP::Post.new(url.request_uri)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
if @username and @password
|
|
38
|
+
req.basic_auth(@username, @password)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
http = Net::HTTP.new(url.host, url.port)
|
|
42
|
+
http.use_ssl = (url.port == 443)
|
|
43
|
+
|
|
44
|
+
res = http.start() { |conn| conn.request(req) }
|
|
45
|
+
puts url
|
|
46
|
+
res.body
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
%w{rubygems cgi hpricot activesupport}.each { |x| require x }
|
|
2
|
+
|
|
3
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
|
4
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
|
5
|
+
|
|
6
|
+
require 'rest'
|
|
7
|
+
require 'yahoo-geoplanet/base'
|
|
8
|
+
require 'yahoo-geoplanet/version'
|
|
9
|
+
|
|
10
|
+
require 'yahoo-geoplanet/place'
|
|
11
|
+
|
|
12
|
+
module Yahoo
|
|
13
|
+
module GeoPlanet
|
|
14
|
+
LOCALE = "us"
|
|
15
|
+
API_VERSION = "v1"
|
|
16
|
+
API_URL = "http://where.yahooapis.com/v1/"
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def app_id=(_id)
|
|
20
|
+
Yahoo::GeoPlanet::Base::connection = REST::Connection.new(API_URL, 'app_id' => _id)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module Yahoo
|
|
2
|
+
module GeoPlanet
|
|
3
|
+
class Base
|
|
4
|
+
class << self
|
|
5
|
+
cattr_accessor :connection
|
|
6
|
+
|
|
7
|
+
def name_with_demodulization
|
|
8
|
+
self.name_without_demodulization.demodulize
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
alias_method_chain :name, :demodulization
|
|
12
|
+
|
|
13
|
+
def fetch_and_parse(resource, options = {})
|
|
14
|
+
raise YahooWebServiceError, "No App ID specified" if connection.nil?
|
|
15
|
+
return Hpricot::XML(connection.get(resource, options))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def api_path(resource, id, collection, *args)
|
|
19
|
+
parameters = [resource, id, collection, *args].compact
|
|
20
|
+
return parameters.collect!{|param| CGI::escape(param.to_s).downcase}.join('/')
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize(xml)
|
|
25
|
+
raise ArgumentError unless xml.kind_of?(Hpricot)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize_with_polymorphism(arg)
|
|
29
|
+
case arg
|
|
30
|
+
when Integer
|
|
31
|
+
initialize_without_polymorphism(query_by_id(arg))
|
|
32
|
+
when Hpricot
|
|
33
|
+
initialize_without_polymorphism(arg)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
alias_method_chain :initialize, :polymorphism
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
def query_by_id(id)
|
|
41
|
+
xml = self.class.fetch_and_parse(self.class.api_path(self.class.name, id, nil))
|
|
42
|
+
return xml.at(self.class.name.downcase)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
class YahooWebServiceError < StandardError; end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
module Yahoo
|
|
2
|
+
module GeoPlanet
|
|
3
|
+
class Place < Base
|
|
4
|
+
attr_reader :woe_id, :type, :name
|
|
5
|
+
attr_reader :latitude, :longitude, :bounding_box
|
|
6
|
+
alias_method :lat, :latitude
|
|
7
|
+
alias_method :lon, :longitude
|
|
8
|
+
|
|
9
|
+
def initialize_without_polymorphism(xml)
|
|
10
|
+
super
|
|
11
|
+
|
|
12
|
+
@woe_id = xml.at("woeid").inner_text
|
|
13
|
+
@type = xml.at("placeTypeName").inner_text
|
|
14
|
+
@name = xml.at("name").inner_text
|
|
15
|
+
|
|
16
|
+
["admin1", "admin2", "admin3", "locality1", "locality2", "postal"].each do |optional|
|
|
17
|
+
begin
|
|
18
|
+
element = xml.at(optional)
|
|
19
|
+
type = element.attributes["type"].downcase.gsub(/\s+/, '_')
|
|
20
|
+
value = element.inner_text
|
|
21
|
+
|
|
22
|
+
self.class.class_eval %(attr_accessor :#{type})
|
|
23
|
+
self.instance_variable_set("@#{type}", value)
|
|
24
|
+
rescue
|
|
25
|
+
next
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
element = xml.at("centroid")
|
|
30
|
+
@latitude = element.at("latitude").inner_text.to_f
|
|
31
|
+
@longitude = element.at("longitude").inner_text.to_f
|
|
32
|
+
|
|
33
|
+
element = xml.at("boundingBox")
|
|
34
|
+
@bounding_box = ["northEast","southWest"].collect do |corner|
|
|
35
|
+
corner = element.at(corner)
|
|
36
|
+
[corner.at("latitude"), corner.at("longitude")].collect{|e| e.inner_text.to_f}
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Association Collections
|
|
41
|
+
["parent", "ancestors", "belongtos", "neighbors", "siblings", "children"].each do |association|
|
|
42
|
+
define_method(association.to_sym) do
|
|
43
|
+
xml = self.class.fetch_and_parse(self.class.api_path(self.class.name, @woe_id, association), :select => :long)
|
|
44
|
+
value = xml.search(self.class.name.downcase).collect{|elem| self.class.new(elem)}
|
|
45
|
+
return association.singularize == association ? value.first : value
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_s
|
|
50
|
+
self.name
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def to_i
|
|
54
|
+
self.woe_id.to_i
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class << self
|
|
58
|
+
def search(query, options = {'count' => '20'})
|
|
59
|
+
query = URI.encode(query.gsub(/\s+/, '+'))
|
|
60
|
+
if options[:type]
|
|
61
|
+
type = options.delete(:type).gsub(/\s+/, '+')
|
|
62
|
+
term = "$and(.q('#{query}'),.type('#{type}'));"
|
|
63
|
+
else
|
|
64
|
+
term = ".q('#{query}');"
|
|
65
|
+
end
|
|
66
|
+
term << options.collect{|k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v.to_s)]}.join(";")
|
|
67
|
+
xml = fetch_and_parse('places' + term)
|
|
68
|
+
return xml.search(self.name.downcase).collect{|elem| self.new(elem)}
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Gem::Specification.new do |s|
|
|
2
|
+
s.name = "yahoo-geoplanet"
|
|
3
|
+
s.version = "0.1.0"
|
|
4
|
+
s.date = "2008-11-13"
|
|
5
|
+
s.summary = "A Ruby wrapper for the Yahoo! GeoPlanet API."
|
|
6
|
+
s.email = "mail@matttthompson.com"
|
|
7
|
+
s.homepage = "http://github.com/mattt/yahoo-geoplanet/"
|
|
8
|
+
s.description = "A Ruby wrapper for the Yahoo! GeoPlanet API.
|
|
9
|
+
See http://developer.yahoo.com/geo/ for more information about the API."
|
|
10
|
+
s.authors = ["Mattt Thompson"]
|
|
11
|
+
|
|
12
|
+
s.files = [
|
|
13
|
+
"README",
|
|
14
|
+
"yahoo-geoplanet.gemspec",
|
|
15
|
+
"lib/yahoo-geoplanet.rb",
|
|
16
|
+
"lib/rest.rb",
|
|
17
|
+
"lib/yahoo-geoplanet/base.rb",
|
|
18
|
+
"lib/yahoo-geoplanet/place.rb",
|
|
19
|
+
"lib/yahoo-geoplanet/version.rb"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
s.add_dependency("hpricot", ["> 0.6"])
|
|
23
|
+
s.add_dependency("activesupport", ["> 2.1.0"])
|
|
24
|
+
|
|
25
|
+
s.has_rdoc = false
|
|
26
|
+
s.rdoc_options = ["--main", "README"]
|
|
27
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mattt-yahoo-geoplanet
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mattt Thompson
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-11-13 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: hpricot
|
|
17
|
+
version_requirement:
|
|
18
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
19
|
+
requirements:
|
|
20
|
+
- - ">"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: "0.6"
|
|
23
|
+
version:
|
|
24
|
+
- !ruby/object:Gem::Dependency
|
|
25
|
+
name: activesupport
|
|
26
|
+
version_requirement:
|
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - ">"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: 2.1.0
|
|
32
|
+
version:
|
|
33
|
+
description: A Ruby wrapper for the Yahoo! GeoPlanet API. See http://developer.yahoo.com/geo/ for more information about the API.
|
|
34
|
+
email: mail@matttthompson.com
|
|
35
|
+
executables: []
|
|
36
|
+
|
|
37
|
+
extensions: []
|
|
38
|
+
|
|
39
|
+
extra_rdoc_files: []
|
|
40
|
+
|
|
41
|
+
files:
|
|
42
|
+
- README
|
|
43
|
+
- yahoo-geoplanet.gemspec
|
|
44
|
+
- lib/yahoo-geoplanet.rb
|
|
45
|
+
- lib/rest.rb
|
|
46
|
+
- lib/yahoo-geoplanet/base.rb
|
|
47
|
+
- lib/yahoo-geoplanet/place.rb
|
|
48
|
+
- lib/yahoo-geoplanet/version.rb
|
|
49
|
+
has_rdoc: false
|
|
50
|
+
homepage: http://github.com/mattt/yahoo-geoplanet/
|
|
51
|
+
post_install_message:
|
|
52
|
+
rdoc_options:
|
|
53
|
+
- --main
|
|
54
|
+
- README
|
|
55
|
+
require_paths:
|
|
56
|
+
- lib
|
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: "0"
|
|
62
|
+
version:
|
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
|
+
requirements:
|
|
65
|
+
- - ">="
|
|
66
|
+
- !ruby/object:Gem::Version
|
|
67
|
+
version: "0"
|
|
68
|
+
version:
|
|
69
|
+
requirements: []
|
|
70
|
+
|
|
71
|
+
rubyforge_project:
|
|
72
|
+
rubygems_version: 1.2.0
|
|
73
|
+
signing_key:
|
|
74
|
+
specification_version: 2
|
|
75
|
+
summary: A Ruby wrapper for the Yahoo! GeoPlanet API.
|
|
76
|
+
test_files: []
|
|
77
|
+
|