Class: Grocer::SSLConnection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/grocer/ssl_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SSLConnection

Returns a new instance of SSLConnection.



13
14
15
16
17
# File 'lib/grocer/ssl_connection.rb', line 13

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

Instance Attribute Details

#certificateObject

Returns the value of attribute certificate.



11
12
13
# File 'lib/grocer/ssl_connection.rb', line 11

def certificate
  @certificate
end

#gatewayObject

Returns the value of attribute gateway.



11
12
13
# File 'lib/grocer/ssl_connection.rb', line 11

def gateway
  @gateway
end

#passphraseObject

Returns the value of attribute passphrase.



11
12
13
# File 'lib/grocer/ssl_connection.rb', line 11

def passphrase
  @passphrase
end

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/grocer/ssl_connection.rb', line 11

def port
  @port
end

Instance Method Details

#connectObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grocer/ssl_connection.rb', line 23

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

  if certificate

    if certificate.respond_to?(:read)
      cert_data = certificate.read
      certificate.rewind if certificate.respond_to?(:rewind)
    else
      cert_data = File.read(certificate)
    end

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

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

#connected?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/grocer/ssl_connection.rb', line 19

def connected?
  !@ssl.nil?
end

#disconnectObject



46
47
48
49
50
51
52
# File 'lib/grocer/ssl_connection.rb', line 46

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

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

#reconnectObject



54
55
56
57
# File 'lib/grocer/ssl_connection.rb', line 54

def reconnect
  disconnect
  connect
end