carlosparamio-geoplanet 0.1.0 → 0.1.1

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/geoplanet.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "geoplanet"
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
  s.date = "2009-02-25"
5
5
  s.summary = "A Ruby wrapper for the Yahoo! GeoPlanet API."
6
6
  s.email = "carlosparamio@gmail.com"
@@ -1,14 +1,16 @@
1
1
  module GeoPlanet
2
2
  class Base
3
3
  class << self
4
- def build_url(resource_name, options = {})
4
+ def build_url(resource_path, options = {})
5
+ check_options_for(resource_path, options)
6
+
5
7
  filters = extract_filters(options)
6
8
  matrix_params = extract_matrix_params(options)
7
9
  query_params = extract_query_params(options)
8
10
 
9
11
  query_params[:appid] ||= GeoPlanet.appid # use default appid if not provided
10
12
 
11
- raise ArgumentError if query_params[:appid].nil? || resource_name == 'places' && filters[:q].nil? # required
13
+ raise ArgumentError if query_params[:appid].nil? || resource_path == 'places' && filters[:q].nil? # required
12
14
 
13
15
  q = ".q('#{filters[:q]}')" if filters[:q]
14
16
  type = ".type('#{filters[:type].is_a?(Array) ? filters[:type].to_a.join(',') : filters[:type]}')" if filters[:type]
@@ -20,10 +22,42 @@ module GeoPlanet
20
22
 
21
23
  query_string += "#{matrix_params}#{query_params}"
22
24
 
23
- "#{GeoPlanet::API_URL}#{resource_name}#{query_string}"
25
+ "#{GeoPlanet::API_URL}#{resource_path}#{query_string}"
26
+ end
27
+
28
+ def get(url)
29
+ RestClient.get(url)
30
+ rescue RestClient::RequestFailed
31
+ raise BadRequest, "appid or q filter invalid"
32
+ rescue RestClient::ResourceNotFound
33
+ raise NotFound, "woeid or URI invalid"
34
+ rescue RestClient::RequestFailed
35
+ raise NotAcceptable, "format invalid"
24
36
  end
25
37
 
26
38
  protected
39
+ def supported_options_for(resource_path)
40
+ case resource_path
41
+ when 'places'
42
+ %w(q type start count lang format callback select appid)
43
+ when /^place\/\d+\/parent$/, /^place\/\d+\/ancestors$/, /^place\/\d+$/, 'placetype'
44
+ %w(lang format callback select appid)
45
+ when /^place\/\d+\/belongtos$/, /^place\/\d+\/children$/, 'placetypes'
46
+ %w(type start count lang format callback select appid)
47
+ when /^place\/\d+\/neighbors$/, /^place\/\d+\/siblings$/
48
+ %w(start count lang format callback select appid)
49
+ else
50
+ raise NotFound, "URI invalid"
51
+ end
52
+ end
53
+
54
+ def check_options_for(resource_path, options)
55
+ supported = supported_options_for(resource_path)
56
+ unless options.keys.all?{|o| supported.include?(o.to_s)}
57
+ raise ArgumentError, "invalid option(s) for #{resource_path}. Supported are: #{supported.join(', ')}. You used: #{options.keys.join(', ')}"
58
+ end
59
+ end
60
+
27
61
  def extract_filters(options)
28
62
  filters = %w(q type)
29
63
  Hash[*(options.select{|k,v| filters.include?(k.to_s)}).flatten(1)]
@@ -15,18 +15,22 @@ module GeoPlanet
15
15
  alias_method :lon, :longitude
16
16
 
17
17
  def initialize(woe_or_attrs)
18
- attrs =
19
- case woe_or_attrs
20
- when Integer
21
- url = self.class.build_url("place/#{woe_or_attrs}", :format => "json")
22
- puts "Yahoo GeoPlanet: GET #{url}"
23
- JSON.parse(RestClient.get(url))['place'] rescue nil
24
- when Hash
25
- woe_or_attrs
26
- else
27
- raise ArgumentError
28
- end
29
-
18
+ case woe_or_attrs
19
+ when Integer then initialize_with_woe(woe_or_attrs)
20
+ when Hash then initialize_with_attrs(woe_or_attrs)
21
+ else
22
+ raise ArgumentError
23
+ end
24
+ end
25
+
26
+ def initialize_with_woe(woe)
27
+ url = self.class.build_url("place/#{woe}", :format => "json")
28
+ puts "Yahoo GeoPlanet: GET #{url}"
29
+ initialize_with_attrs JSON.parse(Place.get(url))['place']
30
+ end
31
+
32
+
33
+ def initialize_with_attrs(attrs)
30
34
  @version = attrs['centroid'] ? 'long' : 'short'
31
35
 
32
36
  # short
@@ -70,12 +74,9 @@ module GeoPlanet
70
74
  %w(parent ancestors belongtos neighbors siblings children).each do |association|
71
75
  self.class_eval <<-RUBY, __FILE__, __LINE__ + 1
72
76
  def #{association}(options = {})
73
- url = self.class.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
77
+ url = Place.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
74
78
  puts "Yahoo GeoPlanet: GET \#{url}"
75
- results = JSON.parse RestClient.get(url)
76
- results['places']['place'].map{|r| Place.new(r)} rescue nil
77
- rescue
78
- raise GeoPlanet::Error
79
+ Place.get_then_parse(url)
79
80
  end
80
81
  RUBY
81
82
  end
@@ -90,12 +91,17 @@ module GeoPlanet
90
91
 
91
92
  class << self
92
93
  def search(text, options = {})
93
- url = build_url('places', options.merge(:q => text, :format => "json"))
94
+ text = URI.encode(text)
95
+ url = build_url('places', options.merge(:q => text, :format => 'json'))
94
96
  puts "Yahoo GeoPlanet: GET #{url}"
95
- results = JSON.parse RestClient.get(url)
96
- results['places']['place'].map{|r| Place.new(r)} rescue nil
97
- rescue
98
- raise GeoPlanet::Error
97
+ get_then_parse(url)
98
+ end
99
+
100
+ def get_then_parse(url)
101
+ results = JSON.parse get(url)
102
+ return results['places']['place'].map{|attrs| Place.new attrs} if results['places']
103
+ return Place.new(results['place']) if results['place']
104
+ nil
99
105
  end
100
106
  end
101
107
  end
@@ -2,7 +2,7 @@ module GeoPlanet
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/geoplanet.rb CHANGED
@@ -12,5 +12,7 @@ module GeoPlanet
12
12
  attr_accessor :appid
13
13
  end
14
14
 
15
- class Error < StandardError; end
15
+ class BadRequest < StandardError; end
16
+ class NotFound < StandardError; end
17
+ class NotAcceptable < StandardError; end
16
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carlosparamio-geoplanet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Paramio