Class: PagarMe::Request

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

Constant Summary collapse

SSL_CA_FILEPATH =
File.join File.dirname(__FILE__), '..', '..', 'certs', 'cabundle.pem'
DEFAULT_HEADERS =
{
  'Content-Type' => 'application/json; charset=utf8',
  'Accept'       => 'application/json',
  'User-Agent'   => "pagarme-ruby/#{PagarMe::VERSION}",
  'X-PagarMe-User-Agent'   => "pagarme-ruby/#{PagarMe::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.



17
18
19
20
21
22
23
24
25
# File 'lib/pagarme/request.rb', line 17

def initialize(path, method, options={})
  raise PagarMe::RequestError, 'You need to configure a API key before performing requests.' unless PagarMe.api_key

  @path       = path
  @method     = method
  @parameters = options[:params]  || Hash.new
  @query      = options[:query]   || Hash.new
  @headers    = options[:headers] || Hash.new
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



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

def method
  @method
end

#parametersObject

Returns the value of attribute parameters.



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

def parameters
  @parameters
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Class Method Details

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



74
75
76
# File 'lib/pagarme/request.rb', line 74

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

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



62
63
64
# File 'lib/pagarme/request.rb', line 62

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

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



66
67
68
# File 'lib/pagarme/request.rb', line 66

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

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



70
71
72
# File 'lib/pagarme/request.rb', line 70

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

Instance Method Details

#callObject



58
59
60
# File 'lib/pagarme/request.rb', line 58

def call
  PagarMeObject.convert run
end

#runObject



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
# File 'lib/pagarme/request.rb', line 27

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
      if parsed_error['errors']
        raise PagarMe::NotFound.new(parsed_error, request_params, error)
      else
        raise PagarMe::NotFound.new(nil, request_params, error)
      end
    else
      if parsed_error['errors']
        raise PagarMe::ValidationError.new parsed_error
      else
        raise PagarMe::ResponseError.new(request_params, error)
      end
    end
  rescue MultiJson::ParseError
    raise PagarMe::ResponseError.new(request_params, error)
  end
rescue MultiJson::ParseError
  raise PagarMe::ResponseError.new(request_params, response)
rescue SocketError
  raise PagarMe::ConnectionError.new $!
rescue RestClient::ServerBrokeConnection
  raise PagarMe::ConnectionError.new $!
end