Module: Query

Includes:
ConfigParams
Defined in:
lib/api_object/query.rb

Constant Summary

Constants included from ConfigParams

ConfigParams::FALLBACK_TIMEOUT, ConfigParams::IPV4_REGEXP, ConfigParams::TIMEOUT

Instance Method Summary collapse

Methods included from ConfigParams

#fallback_timeout, #fallback_timeout=, #timeout, #timeout=

Instance Method Details

#get_url(url, action = nil, mode = nil, url_options = {}) ⇒ Object



19
20
21
22
# File 'lib/api_object/query.rb', line 19

def get_url url, action = nil, mode = nil, url_options = {}
  cmd_url = url.chomp("/") + "/" + (action.nil? ? '':"#{action}?") # url + action
  cmd_url + (mode.nil? ? '' : "#{mode}&") + url_options.to_a.map{|opt| opt[0].to_s + "=" + opt[1]}.join("&") # add parameters
end

#query_api(url, action = nil, mode = nil, url_options = {}, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/api_object/query.rb', line 9

def query_api url, action = nil, mode = nil, url_options = {}, options = {} 
  result = nil
  request_url = get_url(url, action, mode, url_options)
  Timeout.timeout(self.fallback_timeout) do
    result_raw = RestClient::Request.execute(:method => :get, :url => request_url, :timeout => self.timeout)
    result = result_raw.start_with?('<?xml') ? Nori.parse(result_raw) : JSON.parse(result_raw)
  end
  {:result => result, :success => !result.nil?, :url => request_url}
end