Class: GeoPlanet::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/geoplanet/base.rb

Direct Known Subclasses

Place

Class Method Summary collapse

Class Method Details

.build_url(resource_path, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geoplanet/base.rb', line 4

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]}')" 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

.get(url) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/geoplanet/base.rb', line 28

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