Class: Sonic::Connection

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, channel_type, password = nil) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
13
# File 'lib/sonic/connection.rb', line 8

def initialize(host, port, channel_type, password = nil)
  @host = host
  @port = port
  @channel_type = channel_type
  @password = password
end

Class Method Details

.connect(*args) ⇒ Object



3
4
5
6
# File 'lib/sonic/connection.rb', line 3

def self.connect(*args)
  connection = new(*args)
  connection if connection.connect
end

Instance Method Details

#connectObject



15
16
17
18
19
# File 'lib/sonic/connection.rb', line 15

def connect
  read # ...
  write(['START', @channel_type, @password].compact.join(' '))
  read.start_with?('STARTED ')
end

#disconnectObject



21
22
23
# File 'lib/sonic/connection.rb', line 21

def disconnect
  socket.close
end

#readObject

Raises:



25
26
27
28
29
30
# File 'lib/sonic/connection.rb', line 25

def read
  data = socket.gets.chomp
  raise ServerError, data if data.start_with?('ERR ')

  data
end

#write(data) ⇒ Object



32
33
34
# File 'lib/sonic/connection.rb', line 32

def write(data)
  socket.puts(data)
end