Class: Zoop::Request

Inherits:
Object show all
Defined in:
lib/zoop/request.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json',
  'Accept'       => 'application/json',
  'User-Agent'   => "zoop-ruby/#{Zoop::VERSION}"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, method, options = {}) ⇒ Request

Returns a new instance of Request.

Raises:



15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#full_api_urlObject

Returns the value of attribute full_api_url.



7
8
9
# File 'lib/zoop/request.rb', line 7

def full_api_url
  @full_api_url
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/zoop/request.rb', line 7

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



7
8
9
# File 'lib/zoop/request.rb', line 7

def method
  @method
end

#parametersObject

Returns the value of attribute parameters.



7
8
9
# File 'lib/zoop/request.rb', line 7

def parameters
  @parameters
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/zoop/request.rb', line 7

def path
  @path
end

#queryObject

Returns the value of attribute query.



7
8
9
# File 'lib/zoop/request.rb', line 7

def query
  @query
end

Class Method Details

.delete(url, options = {}) ⇒ Object



65
66
67
# File 'lib/zoop/request.rb', line 65

def self.delete(url, options={})
  self.new url, 'DELETE', options
end

.get(url, options = {}) ⇒ Object



53
54
55
# File 'lib/zoop/request.rb', line 53

def self.get(url, options={})
  self.new url, 'GET', options
end

.post(url, options = {}) ⇒ Object



57
58
59
# File 'lib/zoop/request.rb', line 57

def self.post(url, options={})
  self.new url, 'POST', options
end

.put(url, options = {}) ⇒ Object



61
62
63
# File 'lib/zoop/request.rb', line 61

def self.put(url, options={})
  self.new url, 'PUT', options
end

Instance Method Details

#callObject



49
50
51
# File 'lib/zoop/request.rb', line 49

def call
  ZoopObject.convert run
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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