class Zoop::Request
Constants
- DEFAULT_HEADERS
Attributes
full_api_url[RW]
headers[RW]
method[RW]
parameters[RW]
path[RW]
query[RW]
Public Class Methods
delete(url, options={})
click to toggle source
# File lib/zoop/request.rb, line 65 def self.delete(url, options={}) self.new url, 'DELETE', options end
get(url, options={})
click to toggle source
# File lib/zoop/request.rb, line 53 def self.get(url, options={}) self.new url, 'GET', options end
new(path, method, options={})
click to toggle source
# File lib/zoop/request.rb, line 15 def initialize(path, method, options={}) raise Zoop::RequestError, 'You need to configure a Zoop.marketplace_id (ZOOP_MARKETPLACE_ID), Zoop.user_auth (ZOOP_USER_AUTH), Zoop.password_auth (ZOOP_PASSWORD_AUTH) before performing requests.' unless Zoop.marketplace_id && Zoop.user_auth && Zoop.password_auth @path = path @method = method @full_api_url = options[:full_api_url] @query = options[:query] || Hash.new @parameters = options[:params] || Hash.new @headers = options[:headers] || Hash.new end
post(url, options={})
click to toggle source
# File lib/zoop/request.rb, line 57 def self.post(url, options={}) self.new url, 'POST', options end
put(url, options={})
click to toggle source
# File lib/zoop/request.rb, line 61 def self.put(url, options={}) self.new url, 'PUT', options end
Public Instance Methods
call()
click to toggle source
# File lib/zoop/request.rb, line 49 def call ZoopObject.convert run end
run()
click to toggle source
# File lib/zoop/request.rb, line 26 def run response = RestClient::Request.execute request_params MultiJson.decode response.body rescue RestClient::Exception => error begin parsed_error = MultiJson.decode error.http_body if error.is_a? RestClient::ResourceNotFound raise Zoop::NotFound.new(parsed_error, request_params, error) else raise Zoop::ResponseError.new(request_params, parsed_error) end rescue MultiJson::ParseError raise Zoop::ResponseError.new(request_params, error.http_body) end rescue MultiJson::ParseError raise Zoop::ResponseError.new(request_params, response.body) rescue SocketError raise Zoop::ConnectionError.new $! rescue RestClient::ServerBrokeConnection raise Zoop::ConnectionError.new $! end
Protected Instance Methods
formated_parameters()
click to toggle source
# File lib/zoop/request.rb, line 94 def formated_parameters if parameters.include?(:file) parameters elsif parameters.present? MultiJson.encode(parameters) end end
full_api_url_with_marketplace()
click to toggle source
# File lib/zoop/request.rb, line 84 def full_api_url_with_marketplace url = Zoop.api_endpoint + "/marketplaces/#{Zoop.marketplace_id}" + path if query.present? url += '?' + URI.encode_www_form(query) end url end
request_params()
click to toggle source
# File lib/zoop/request.rb, line 71 def request_params { method: method, user: Zoop.user_auth, password: Zoop.password_auth, url: full_api_url || full_api_url_with_marketplace, payload: formated_parameters, open_timeout: Zoop.open_timeout, timeout: Zoop.timeout, headers: DEFAULT_HEADERS.merge(headers) }.deep_compact end