Class: Zwiebel::Control

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "127.0.0.1", port: 9051, cookie: nil) ⇒ Control

Returns a new instance of Control.



20
21
22
23
24
25
# File 'lib/zwiebel/control.rb', line 20

def initialize(host: "127.0.0.1", port: 9051, cookie: nil)
  @host = host
  @port = port
  @cookie = cookie
  connect
end

Instance Attribute Details

Returns the value of attribute cookie.



18
19
20
# File 'lib/zwiebel/control.rb', line 18

def cookie
  @cookie
end

#hostObject

Returns the value of attribute host.



18
19
20
# File 'lib/zwiebel/control.rb', line 18

def host
  @host
end

#portObject

Returns the value of attribute port.



18
19
20
# File 'lib/zwiebel/control.rb', line 18

def port
  @port
end

#socketObject

Returns the value of attribute socket.



18
19
20
# File 'lib/zwiebel/control.rb', line 18

def socket
  @socket
end

Instance Method Details

#authenticateObject



50
51
52
53
54
# File 'lib/zwiebel/control.rb', line 50

def authenticate
  send_line(@cookie ? "AUTHENTICATE #{cookie}" : "AUTHENTICATE")
  reply = read_reply
  @authenticated = reply == "250 OK"
end

#authenticated?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/zwiebel/control.rb', line 56

def authenticated?
  !!@authenticated
end

#closeObject



37
38
39
40
41
# File 'lib/zwiebel/control.rb', line 37

def close
  @socket.close unless @socket.nil?
  @authenticated = false
  @socket = nil
end

#connectObject



27
28
29
30
31
# File 'lib/zwiebel/control.rb', line 27

def connect
  close
  @socket = TCPSocket.new(host, port)
  @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
end

#connected?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/zwiebel/control.rb', line 33

def connected?
  !@socket.nil?
end

#quitObject



43
44
45
46
47
48
# File 'lib/zwiebel/control.rb', line 43

def quit
  send_line("QUIT")
  reply = read_reply
  close
  reply
end

#read_replyObject



77
78
79
# File 'lib/zwiebel/control.rb', line 77

def read_reply
  @socket.readline.chomp
end

#send_command(command, *args) ⇒ Object



67
68
69
70
# File 'lib/zwiebel/control.rb', line 67

def send_command(command, *args)
  authenticate unless authenticated?
  send_line([command, *args].join(" "))
end

#send_line(line) ⇒ Object



72
73
74
75
# File 'lib/zwiebel/control.rb', line 72

def send_line(line)
  @socket.write("#{line}\r\n")
  @socket.flush
end

#versionObject



60
61
62
63
64
65
# File 'lib/zwiebel/control.rb', line 60

def version
  send_command("GETINFO", "version")
  reply = read_reply.split("=").last
  read_reply
  reply
end