lib/pagarme/request.rb in pagarme-2.0.2 vs lib/pagarme/request.rb in pagarme-2.1.0

- old
+ new

@@ -1,52 +1,102 @@ require 'uri' require 'rest_client' require 'multi_json' -require File.join(File.dirname(__FILE__), '.', 'util') -require File.join(File.dirname(__FILE__), '.', 'errors') module PagarMe - class Request - attr_accessor :path, :method, :parameters, :headers, :query + class Request + attr_accessor :path, :method, :parameters, :headers, :query - def initialize(path, method) - self.path = path - self.method = method - self.parameters = {} - self.query = {} - self.headers = {} - end + 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}" + } - def run - raise PagarMeError, "You need to configure a API key before performing requests." unless PagarMe.api_key + def initialize(path, method, options={}) + raise PagarMe::RequestError, 'You need to configure a API key before performing requests.' unless PagarMe.api_key - begin - response = RestClient::Request.execute({ - :method => self.method, - :user => PagarMe.api_key, - :password => 'x', - :url => PagarMe.full_api_url(self.path) + '?' + URI.encode_www_form(query), - :payload => MultiJson.encode(parameters), - :open_timeout => 30, - :timeout => 90, - :ssl_ca_file => File.join(File.dirname(__FILE__), '..', '..', 'certs', 'cabundle.pem'), - :headers => { - 'Content-Type' => 'application/json; charset=utf8', - 'Accept' => 'application/json', - 'User-Agent' => 'pagarme-ruby/1.0' - } - }) - rescue RestClient::ExceptionWithResponse => e - parsed_error = MultiJson.decode(e.http_body) + @path = path + @method = method + @parameters = options[:params] || Hash.new + @query = options[:query] || Hash.new + @headers = options[:headers] || Hash.new + end - if parsed_error['errors'] - raise PagarMeError.fromServerResponse(parsed_error) - else - raise PagarMeError.new(e.http_body) - end - end + def run + response = RestClient::Request.execute request_params + MultiJson.decode response.body + rescue RestClient::Exception => error + begin + parsed_error = MultiJson.decode error.http_body - MultiJson.decode response.body - end - end -end + 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 + def call + PagarMeObject.convert run + end + + def self.get(url, options={}) + self.new url, 'GET', options + end + + def self.post(url, options={}) + self.new url, 'POST', options + end + + def self.put(url, options={}) + self.new url, 'PUT', options + end + + def self.delete(url, options={}) + self.new url, 'DELETE', options + end + + protected + def request_params + { + method: method, + user: PagarMe.api_key, + password: 'x', + url: full_api_url, + payload: MultiJson.encode(parameters), + open_timeout: PagarMe.open_timeout, + timeout: PagarMe.timeout, + ssl_ca_file: SSL_CA_FILEPATH, + headers: DEFAULT_HEADERS.merge(headers) + } + end + + def full_api_url + url = PagarMe.api_endpoint + path + + if query.present? + url += '?' + URI.encode_www_form(query) + end + + url + end + end +end