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 +1 -1
- data/lib/geoplanet/base.rb +37 -3
- data/lib/geoplanet/place.rb +28 -22
- data/lib/geoplanet/version.rb +1 -1
- data/lib/geoplanet.rb +3 -1
- metadata +1 -1
data/geoplanet.gemspec
CHANGED
data/lib/geoplanet/base.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module GeoPlanet
|
2
2
|
class Base
|
3
3
|
class << self
|
4
|
-
def build_url(
|
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? ||
|
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}#{
|
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)]
|
data/lib/geoplanet/place.rb
CHANGED
@@ -15,18 +15,22 @@ module GeoPlanet
|
|
15
15
|
alias_method :lon, :longitude
|
16
16
|
|
17
17
|
def initialize(woe_or_attrs)
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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 =
|
77
|
+
url = Place.build_url("place/\#{self.woeid}/#{association}", options.merge(:format => "json"))
|
74
78
|
puts "Yahoo GeoPlanet: GET \#{url}"
|
75
|
-
|
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
|
-
|
94
|
+
text = URI.encode(text)
|
95
|
+
url = build_url('places', options.merge(:q => text, :format => 'json'))
|
94
96
|
puts "Yahoo GeoPlanet: GET #{url}"
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
data/lib/geoplanet/version.rb
CHANGED
data/lib/geoplanet.rb
CHANGED