Sha256: 2431c2c10a34cf35ce76458f731a5693c77d3297c8a4a3ddcbc153532f18f187

Contents?: true

Size: 1.26 KB

Versions: 14

Compression:

Stored size: 1.26 KB

Contents

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

module Houston
  class Connection
    extend Forwardable
    def_delegators :@ssl, :read, :write
    def_delegators :@uri, :scheme, :host, :port

    attr_reader :ssl, :socket, :certificate, :passphrase

    class << self
      def open(uri, certificate, passphrase)
        return unless block_given?

        connection = new(uri, certificate, passphrase)
        connection.open

        yield connection

        connection.close
      end
    end

    def initialize(uri, certificate, passphrase)
      @uri = URI(uri)
      @certificate = certificate
      @passphrase = passphrase
    end

    def open
      return false if open?

      @socket = TCPSocket.new(@uri.host, @uri.port)

      context = OpenSSL::SSL::SSLContext.new
      context.key = OpenSSL::PKey::RSA.new(@certificate, @passphrase)
      context.cert = OpenSSL::X509::Certificate.new(@certificate)

      @ssl = OpenSSL::SSL::SSLSocket.new(@socket, context)
      @ssl.sync = true
      @ssl.connect
    end

    def open?
      not (@ssl and @socket).nil?
    end

    def close
      return false if closed?

      @ssl.close
      @ssl = nil

      @socket.close
      @socket = nil
    end

    def closed?
      not open?
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
houston-2.2.3 ./lib/houston/connection.rb
houston-2.2.2 ./lib/houston/connection.rb
houston-2.2.1 ./lib/houston/connection.rb
houston-2.2.0 ./lib/houston/connection.rb
houston-2.1.0 ./lib/houston/connection.rb
houston-2.0.2 ./lib/houston/connection.rb
houston-2.0.0 ./lib/houston/connection.rb
houston-1.0.0 ./lib/houston/connection.rb
houston-0.3.1 ./lib/houston/connection.rb
houston-0.3.0 ./lib/houston/connection.rb
houston-0.2.4 ./lib/houston/connection.rb
houston-0.2.3 ./lib/houston/connection.rb
houston-0.2.2 ./lib/houston/connection.rb
houston-0.2.1 ./lib/houston/connection.rb