Class: Houston::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/houston/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, certificate, passphrase) ⇒ Connection


27
28
29
30
31
# File 'lib/houston/connection.rb', line 27

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

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate


12
13
14
# File 'lib/houston/connection.rb', line 12

def certificate
  @certificate
end

#passphraseObject (readonly)

Returns the value of attribute passphrase


12
13
14
# File 'lib/houston/connection.rb', line 12

def passphrase
  @passphrase
end

#socketObject (readonly)

Returns the value of attribute socket


12
13
14
# File 'lib/houston/connection.rb', line 12

def socket
  @socket
end

#sslObject (readonly)

Returns the value of attribute ssl


12
13
14
# File 'lib/houston/connection.rb', line 12

def ssl
  @ssl
end

Class Method Details

.open(uri, certificate, passphrase) {|connection| ... } ⇒ Object

Yields:

  • (connection)

15
16
17
18
19
20
21
22
23
24
# File 'lib/houston/connection.rb', line 15

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

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

  yield connection

  connection.close
end

Instance Method Details

#closeObject


51
52
53
54
55
56
57
58
59
# File 'lib/houston/connection.rb', line 51

def close
  return false if closed?

  @ssl.close
  @ssl = nil

  @socket.close
  @socket = nil
end

#closed?Boolean


61
62
63
# File 'lib/houston/connection.rb', line 61

def closed?
  not open?
end

#openObject


33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/houston/connection.rb', line 33

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

#open?Boolean


47
48
49
# File 'lib/houston/connection.rb', line 47

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