Skip to content

Commit

Permalink
enabled custom options for single place resource (e.g. lang option), …
Browse files Browse the repository at this point in the history
…disabled debug output by default and added a debug flag
  • Loading branch information
Carlos Paramio committed Feb 26, 2009
1 parent 29f8d8d commit fe4bce6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
13 changes: 13 additions & 0 deletions README.rdoc
Expand Up @@ -63,6 +63,19 @@ but this version supports better usage of matrix and query parameters, uses JSON

# You can specify the language you want for the results.
a.belongtos(:type => 12, :lang => 'es_ES').first.name # España

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

=== Debug Mode

If you want to look at the requests that are being executed against the Yahoo GeoPlanet API, you can enable the debug mode. It uses the standard output.

GeoPlanet.debug = true
GeoPlanet::Place.new(752067, :lang => :es)
# outputs:
# Yahoo GeoPlanet: GET http://where.yahooapis.com/v1/place/752067?appid=[your_appid]&format=json&lang=es


== REQUIREMENTS:

Expand Down
2 changes: 1 addition & 1 deletion lib/geoplanet.rb
Expand Up @@ -9,7 +9,7 @@ module GeoPlanet
API_URL = "http://where.yahooapis.com/#{API_VERSION}/"

class << self
attr_accessor :appid
attr_accessor :appid, :debug
end

class BadRequest < StandardError; end
Expand Down
6 changes: 2 additions & 4 deletions lib/geoplanet/base.rb
Expand Up @@ -10,7 +10,7 @@ def build_url(resource_path, options = {})

query_params[:appid] ||= GeoPlanet.appid # use default appid if not provided

raise ArgumentError if query_params[:appid].nil? || resource_path == 'places' && filters[:q].nil? # required
raise ArgumentError, "appid or q filter missing" if query_params[:appid].nil? || resource_path == 'places' && filters[:q].nil? # required

q = ".q('#{filters[:q]}')" if filters[:q]
type = ".type('#{filters[:type].is_a?(Array) ? filters[:type].to_a.join(',') : filters[:type]}')" if filters[:type]
Expand All @@ -28,11 +28,9 @@ def build_url(resource_path, options = {})
def get(url)
RestClient.get(url)
rescue RestClient::RequestFailed
raise BadRequest, "appid or q filter invalid"
raise BadRequest, "appid, q filter or format invalid"
rescue RestClient::ResourceNotFound
raise NotFound, "woeid or URI invalid"
rescue RestClient::RequestFailed
raise NotAcceptable, "format invalid"
end

protected
Expand Down
14 changes: 7 additions & 7 deletions lib/geoplanet/place.rb
Expand Up @@ -14,18 +14,18 @@ class Place < Base
alias_method :lat, :latitude
alias_method :lon, :longitude

def initialize(woe_or_attrs)
def initialize(woe_or_attrs, options = {})
case woe_or_attrs
when Integer then initialize_with_woe(woe_or_attrs)
when Integer then initialize_with_woe(woe_or_attrs, options)
when Hash then initialize_with_attrs(woe_or_attrs)
else
raise ArgumentError
end
end

def initialize_with_woe(woe)
url = self.class.build_url("place/#{woe}", :format => "json")
puts "Yahoo GeoPlanet: GET #{url}"
def initialize_with_woe(woe, options = {})
url = self.class.build_url("place/#{woe}", options.merge(:format => "json"))
puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
initialize_with_attrs JSON.parse(Place.get(url))['place']
end

Expand Down Expand Up @@ -75,7 +75,7 @@ def initialize_with_attrs(attrs)
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}"
puts "Yahoo GeoPlanet: GET \#{url}" if GeoPlanet.debug
Place.get_then_parse(url)
end
RUBY
Expand All @@ -93,7 +93,7 @@ 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}"
puts "Yahoo GeoPlanet: GET #{url}" if GeoPlanet.debug
get_then_parse(url)
end

Expand Down

0 comments on commit fe4bce6

Please sign in to comment.