forked from carlosparamio/geoplanet
/
base.rb
75 lines (61 loc) · 2.9 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
module GeoPlanet
class Base
class << self
def build_url(resource_path, options = {})
check_options_for(resource_path, options)
filters = extract_filters(options)
matrix_params = extract_matrix_params(options)
query_params = extract_query_params(options)
query_params[:appid] ||= GeoPlanet.appid # use default appid if not provided
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]
query_string = q && type ? "$and(#{q},#{type})" : "#{q}#{type}"
matrix_params = ";#{matrix_params.map{|k,v| "#{k}=#{v}"}.join(';')}" if matrix_params.any?
query_params = "?#{query_params.map{|k,v| "#{k}=#{v}"}.join('&')}" if query_params.any?
query_string += "#{matrix_params}#{query_params}"
"#{GeoPlanet::API_URL}#{resource_path}#{query_string}"
end
def get(url)
RestClient.get(url)
rescue RestClient::RequestFailed
raise BadRequest, "appid, q filter or format invalid"
rescue RestClient::ResourceNotFound
raise NotFound, "woeid or URI invalid"
end
protected
def supported_options_for(resource_path)
case resource_path
when 'places'
%w(q type start count lang format callback select appid)
when /^place\/\d+\/parent$/, /^place\/\d+\/ancestors$/, /^place\/\d+$/, 'placetype'
%w(lang format callback select appid)
when /^place\/\d+\/belongtos$/, /^place\/\d+\/children$/, 'placetypes'
%w(type start count lang format callback select appid)
when /^place\/\d+\/neighbors$/, /^place\/\d+\/siblings$/
%w(start count lang format callback select appid)
else
raise NotFound, "URI invalid"
end
end
def check_options_for(resource_path, options)
supported = supported_options_for(resource_path)
unless options.keys.all?{|o| supported.include?(o.to_s)}
raise ArgumentError, "invalid option(s) for #{resource_path}. Supported are: #{supported.join(', ')}. You used: #{options.keys.join(', ')}"
end
end
def extract_filters(options)
filters = %w(q type)
Hash[*(options.select{|k,v| filters.include?(k.to_s)}).flatten(1)]
end
def extract_matrix_params(options)
matrix_params = %w(start count)
Hash[*(options.select{|k,v| matrix_params.include?(k.to_s)}).flatten]
end
def extract_query_params(options)
query_params = %w(lang format callback select appid)
Hash[*(options.select{|k,v| query_params.include?(k.to_s)}).flatten]
end
end
end
end