Class: Net::Tofu::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/net/tofu/socket.rb

Overview

Stoes an SSLSocket for making requests and receiving data.

Instance Method Summary collapse

Constructor Details

#initialize(host, port, trust) ⇒ Socket

Constructor for the socket type.

Parameters:

  • host (String)

    Hostname of the server to connect to.

  • port (Integer)

    Server port to connect to (typically 1965).



10
11
12
13
14
15
# File 'lib/net/tofu/socket.rb', line 10

def initialize(host, port, trust)
  @host = host
  @port = port
  @trust = trust
  @sock = OpenSSL::SSL::SSLSocket.open(@host, @port, context: generate_secure_context)
end

Instance Method Details

#closeObject

Close the connection with the server.



39
40
41
# File 'lib/net/tofu/socket.rb', line 39

def close
  @sock.close
end

#connectObject

Open a connection to the server.



18
19
20
21
22
# File 'lib/net/tofu/socket.rb', line 18

def connect
  @sock.hostname = @host
  @sock.connect
  validate_certs
end

#gets(req) ⇒ Object

Try and retrieve data from a request.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/net/tofu/socket.rb', line 25

def gets(req)
  @sock.puts req.format

  io = StringIO.new
  while (line = @sock.gets)
    io.puts line
  end

  Response.new(io.string)
ensure
  io.close
end