Class: MPD::Connection

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

Overview

Simple wrapper over Socket

Instance Method Summary collapse

Constructor Details

#initialize(host: 'localhost', port: 6600) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
# File 'lib/mpd/connection.rb', line 8

def initialize(host: 'localhost', port: 6600)
  @host = host
  @port = port
end

Instance Method Details

#connectObject



13
14
15
16
# File 'lib/mpd/connection.rb', line 13

def connect
  disconnect if socket
  @socket = TCPSocket.open(host, port)
end

#disconnectObject



18
19
20
21
# File 'lib/mpd/connection.rb', line 18

def disconnect
  socket && socket.close
  @socket = nil
end

#getsObject



30
31
32
33
34
35
# File 'lib/mpd/connection.rb', line 30

def gets
  raise ConnectionError unless socket
  socket.gets || raise(ConnectionError)
rescue Errno::ECONNRESET
  raise ConnectionError
end

#puts(text) ⇒ Object



23
24
25
26
27
28
# File 'lib/mpd/connection.rb', line 23

def puts(text)
  raise ConnectionError unless socket
  socket.puts(text)
rescue Errno::ECONNRESET
  raise ConnectionError
end