lib/pagarme/request.rb in pagarme-1.9.2 vs lib/pagarme/request.rb in pagarme-1.9.3
- old
+ new
@@ -4,75 +4,75 @@
require File.join(File.dirname(__FILE__), '.', 'util')
require File.join(File.dirname(__FILE__), '.', 'errors')
module PagarMe
class Request
- attr_accessor :path, :method, :parameters, :headers
+ attr_accessor :path, :method, :parameters, :headers
- def initialize(path, method)
- self.path = path
- self.method = method
- self.parameters = {}
- self.headers = {}
- end
+ def initialize(path, method)
+ self.path = path
+ self.method = method
+ self.parameters = {}
+ self.headers = {}
+ end
- def self.encode(params)
- Util.normalize_params(params).to_params
- end
+ def self.encode(params)
+ Util.normalize_params(params).to_params
+ end
- def run
- unless PagarMe.api_key
- raise PagarMeError.new("You need to configure a API key before performing requests.")
- end
+ def run
+ unless PagarMe.api_key
+ raise PagarMeError.new("You need to configure a API key before performing requests.")
+ end
- self.headers = {}
+ self.headers = {}
- parameters = self.parameters.merge({
- :api_key => PagarMe.api_key
- })
- error = nil
+ parameters = self.parameters.merge({
+ :api_key => PagarMe.api_key
+ })
+ error = nil
- begin
- response = RestClient::Request.execute({
- :method => self.method,
- :url => PagarMe.full_api_url(self.path),
- :headers => self.headers,
- :open_timeout => 30,
- :payload => self.class.encode(parameters),
- :timeout => 90
- })
- rescue SocketError => e
- error = "Error connecting to server (#{e.message})."
- rescue NoMethodError => e
- if e.message =~ /\WRequestFailed\W/
- raise ResponseError.new("Unexpected response code (#{e.inspect}).")
- else
- raise
- end
- rescue RestClient::ExceptionWithResponse => e
- parsed_error = parse_json_response(e.http_body)
- if parsed_error['errors']
- error = parsed_error
- raise PagarMeError.initFromServerResponse(error)
- else
- raise PagarMeError.new(e.http_body)
- end
- rescue RestClient::Exception, Errno::ECONNREFUSED => e
- error = "Error connecting to server: connection refused"
- end
+ begin
+ response = RestClient::Request.execute({
+ :method => self.method,
+ :url => PagarMe.full_api_url(self.path),
+ :headers => self.headers,
+ :open_timeout => 30,
+ :payload => self.class.encode(parameters),
+ :timeout => 90
+ })
+ rescue SocketError => e
+ error = "Error connecting to server (#{e.message})."
+ rescue NoMethodError => e
+ if e.message =~ /\WRequestFailed\W/
+ raise ResponseError.new("Unexpected response code (#{e.inspect}).")
+ else
+ raise
+ end
+ rescue RestClient::ExceptionWithResponse => e
+ parsed_error = parse_json_response(e.http_body)
+ if parsed_error['errors']
+ error = parsed_error
+ raise PagarMeError.initFromServerResponse(error)
+ else
+ raise PagarMeError.new(e.http_body)
+ end
+ rescue RestClient::Exception, Errno::ECONNREFUSED => e
+ error = "Error connecting to server: connection refused"
+ end
- raise ConnectionError.new(error) if error
+ raise ConnectionError.new(error) if error
- parse_json_response(response.body)
- end
+ parse_json_response(response.body)
+ end
- private
+ private
- def parse_json_response(response)
- begin
- MultiJson.load(response)
- rescue MultiJson::LoadError => e
- raise PagarMeError.new("Server response is not a valid JSON.")
- end
- end
+ def parse_json_response(response)
+ begin
+ MultiJson.load(response)
+ rescue MultiJson::LoadError => e
+ raise PagarMeError.new("Server response is not a valid JSON.")
+ end
+ end
end
end