Class: Sonic::Connection
- Inherits:
-
Object
- Object
- Sonic::Connection
- Defined in:
- lib/sonic/connection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#initialize(host, port, channel_type, password = nil) ⇒ Connection
constructor
A new instance of Connection.
- #read ⇒ Object
- #write(data) ⇒ Object
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
#connect ⇒ Object
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 |
#disconnect ⇒ Object
21 22 23 |
# File 'lib/sonic/connection.rb', line 21 def disconnect socket.close end |
#read ⇒ Object
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 |