diff --git a/README.rdoc b/README.rdoc index 0314891..b05611e 100644 --- a/README.rdoc +++ b/README.rdoc @@ -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 diff --git a/geoplanet.gemspec b/geoplanet.gemspec index ebd4ad0..af06039 100644 --- a/geoplanet.gemspec +++ b/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/" diff --git a/lib/geoplanet/place.rb b/lib/geoplanet/place.rb index 2ddf77d..a556698 100644 --- a/lib/geoplanet/place.rb +++ b/lib/geoplanet/place.rb @@ -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) @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/geoplanet/version.rb b/lib/geoplanet/version.rb index 84ab9be..2c344f7 100644 --- a/lib/geoplanet/version.rb +++ b/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