class RubyPsigate::Connection

Constants

MAX_RETRIES
OPEN_TIMEOUT
READ_TIMEOUT

> Values in seconds

RETRY_SAFE
VERIFY_PEER

Attributes

endpoint[R]
open_timeout[RW]
read_timeout[RW]
retry_safe[RW]
verify_peer[RW]

Public Class Methods

new(endpoint) click to toggle source
# File lib/ruby_psigate/connection.rb, line 18
def initialize(endpoint)
  @endpoint = endpoint.is_a?(URI) ? endpoint : URI.parse(endpoint)
  @retry_safe   = RETRY_SAFE
  @read_timeout = READ_TIMEOUT
  @open_timeout = OPEN_TIMEOUT
  @verify_peer  = VERIFY_PEER
end

Public Instance Methods

post(params) click to toggle source
# File lib/ruby_psigate/connection.rb, line 32
def post(params)
  retry_exceptions do
    begin

      uri = @endpoint
      psigate = Net::HTTP.new(uri.host, uri.port)

      # Configure Timeouts
      psigate.open_timeout = open_timeout
      psigate.read_timeout = read_timeout 

      # Configure SSL
      psigate.use_ssl = true
      if verify_peer
        psigate.verify_mode = OpenSSL::SSL::VERIFY_PEER
        psigate.ca_file     = ca_file
      else
        psigate.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end

      raw_response = psigate.post(uri.path, params)
      response = handle_response(raw_response)
      response
    rescue EOFError => e
      raise ConnectionError, "The remote server dropped the connection"
    rescue Errno::ECONNRESET => e
      raise ConnectionError, "The remote server reset the connection"
    rescue Errno::ECONNREFUSED => e
      raise RetriableConnectionError, "The remote server refused the connection"
    rescue Timeout::Error, Errno::ETIMEDOUT => e
      raise ConnectionError, "The connection to the remote server timed out"
      
    end # begin
  end # retry_exceptions
end

Private Instance Methods

ca_file() click to toggle source
# File lib/ruby_psigate/connection.rb, line 70
def ca_file
  ca_file = File.dirname(__FILE__) + '/../certs/cacert.pem'
  raise CertVerificationFileMissingError unless File.exists?(ca_file)
  ca_file
end
handle_response(raw_response) click to toggle source
# File lib/ruby_psigate/connection.rb, line 76
def handle_response(raw_response)
  case raw_response.code.to_i
  when 200..300
    raw_response.body
  else
    raise ResponseError.new(raw_response)
  end
end
retry_exceptions() { || ... } click to toggle source
# File lib/ruby_psigate/connection.rb, line 85
def retry_exceptions(&block)
  retries = MAX_RETRIES
  begin
    yield
  rescue RetriableConnectionError => e
    retries -= 1
    retry unless retries.zero?
    raise ConnectionError, e.message
  rescue ConnectionError
    retries -= 1
    retry if retry_safe && !retries.zero?
    raise
  end
end