Class: SimpleSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_socket.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, timeout = DEFAULT_TIMEOUT) ⇒ SimpleSocket

Returns a new instance of SimpleSocket.



8
9
10
11
12
# File 'lib/simple_socket.rb', line 8

def initialize(host, port, timeout = DEFAULT_TIMEOUT)
  @host = host
  @port = port
  @timeout = timeout 
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/simple_socket.rb', line 5

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/simple_socket.rb', line 5

def port
  @port
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



5
6
7
# File 'lib/simple_socket.rb', line 5

def timeout
  @timeout
end

Instance Method Details

#write(message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/simple_socket.rb', line 14

def write(message)
  begin
    socket = TCPSocket.new(host, port)
    socket.puts(message)
    response = nil
    Timeout::timeout(timeout) do 
      response = socket.read
    end
  ensure
    socket.close if socket
  end
  response
end