Sha256: dbe550fd2b3ea1a26fbf36ff225017515d8825d71b176c58e549b97fdc68bb3f

Contents?: true

Size: 996 Bytes

Versions: 3

Compression:

Stored size: 996 Bytes

Contents

require 'socket'
require 'openssl'
require 'forwardable'

module Grocer
  class SSLConnection
    extend Forwardable
    def_delegators :@ssl, :write, :read

    attr_accessor :certificate, :passphrase, :gateway, :port

    def initialize(options = {})
      options.each do |key, val|
        send("#{key}=", val)
      end
    end

    def connected?
      !@ssl.nil?
    end

    def connect
      context = OpenSSL::SSL::SSLContext.new

      if certificate
        cert_data    = File.read(certificate)
        context.key  = OpenSSL::PKey::RSA.new(cert_data, passphrase)
        context.cert = OpenSSL::X509::Certificate.new(cert_data)
      end

      @sock     = TCPSocket.new(gateway, port)
      @ssl      = OpenSSL::SSL::SSLSocket.new(@sock, context)
      @ssl.sync = true
      @ssl.connect
    end

    def disconnect
      @ssl.close if @ssl
      @ssl = nil

      @sock.close if @sock
      @sock = nil
    end

    def reconnect
      disconnect
      connect
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grocer-0.0.13 lib/grocer/ssl_connection.rb
grocer-0.0.12 lib/grocer/ssl_connection.rb
grocer-0.0.11 lib/grocer/ssl_connection.rb