Sha256: b0c26a8c34ad1948913d5f7a3eb538f979de1c8ce0435d3439577629a49d4813

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

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

		def initialize(path, method)
			self.path = path
			self.method = method
			self.parameters = {}
			self.query = {}
			self.headers = {}
		end

		def run
			raise PagarMeError, "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)

				if parsed_error['errors']
					raise PagarMeError.fromServerResponse(parsed_error)
				else
					raise PagarMeError.new(e.http_body)
				end
			end

			MultiJson.decode response.body
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pagarme-2.0.2 lib/pagarme/request.rb
pagarme-2.0.1 lib/pagarme/request.rb
pagarme-2.0.0 lib/pagarme/request.rb