Skip to content

Commit

Permalink
add support for indenpendent association queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Paramio committed Mar 23, 2009
1 parent 30580e0 commit dfcdae5
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
6 changes: 6 additions & 0 deletions README.rdoc
Expand Up @@ -66,6 +66,12 @@ but this version supports better usage of matrix and query parameters, uses JSON

a = GeoPlanet::Place.new(752067, :lang => :es)
a.country # España

# It is also possible to query for any association directly using a WOE ID, without
# create a Place object. Append a '_of' suffix to the association name to build the
# method name to execute. The first argument is the WOE ID.

GeoPlanet::Place.belongtos_of(752067, :type => [12, 9])

=== Debug Mode

Expand Down
4 changes: 2 additions & 2 deletions geoplanet.gemspec
@@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.name = "geoplanet"
s.version = "0.1.3"
s.date = "2009-02-26"
s.version = "0.2.0"
s.date = "2009-03-23"
s.summary = "A Ruby wrapper for the Yahoo! GeoPlanet API."
s.email = "carlosparamio@gmail.com"
s.homepage = "http://github.com/carlosparamio/geoplanet/"
Expand Down
49 changes: 29 additions & 20 deletions lib/geoplanet/place.rb
Expand Up @@ -14,6 +14,34 @@ class Place < Base
alias_method :lat, :latitude
alias_method :lon, :longitude

# Class methods
def self.search(text, options = {})
text = URI.encode(text)
url = build_url('places', options.merge(:q => text, :format => 'json'))
puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
get_then_parse(url)
end

def self.get_then_parse(url)
results = JSON.parse get(url)
return results['places']['place'].map{|attrs| Place.new attrs} if results['places']
return Place.new(results['place']) if results['place']
nil
rescue
nil
end

%w(parent ancestors belongtos neighbors siblings children).each do |association|
self.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{association}_of(woeid, options = {})
url = build_url("place/\#{woeid}/#{association}", options.merge(:format => "json"))
puts "Yahoo GeoPlanet: GET \#{url}" if GeoPlanet.debug
get_then_parse(url)
end
RUBY
end

# Instance methods
def initialize(woe_or_attrs, options = {})
case woe_or_attrs
when Integer then initialize_with_woe(woe_or_attrs, options)
Expand Down Expand Up @@ -74,9 +102,7 @@ def initialize_with_attrs(attrs)
%w(parent ancestors belongtos neighbors siblings children).each do |association|
self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{association}(options = {})
url = Place.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
puts "Yahoo GeoPlanet: GET \#{url}" if GeoPlanet.debug
Place.get_then_parse(url)
Place.send("#{association}_of", self.woeid, options)
end
RUBY
end
Expand All @@ -89,22 +115,5 @@ def to_i
self.woeid.to_i
end

class << self
def search(text, options = {})
text = URI.encode(text)
url = build_url('places', options.merge(:q => text, :format => 'json'))
puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
get_then_parse(url)
end

def get_then_parse(url)
results = JSON.parse get(url)
return results['places']['place'].map{|attrs| Place.new attrs} if results['places']
return Place.new(results['place']) if results['place']
nil
rescue
nil
end
end
end
end
4 changes: 2 additions & 2 deletions lib/geoplanet/version.rb
@@ -1,8 +1,8 @@
module GeoPlanet
module VERSION #:nodoc:
MAJOR = 0
MINOR = 1
TINY = 3
MINOR = 2
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.')
end
Expand Down

0 comments on commit dfcdae5

Please sign in to comment.