Class: Mpc

Inherits:
Object
  • Object
show all
Defined in:
lib/mpc.rb,
lib/mpc/song.rb,
lib/mpc/version.rb

Defined Under Namespace

Classes: Song

Constant Summary collapse

VERSION =
"0.9.4"
@@regexps =
{
  "ACK"     => /\AACK \[(\d+)\@(\d+)\] \{(.*)\} (.+)\Z/,
  "OK"      => /\AOK\n\Z/,
  "FILE"    => /\Afile\:(.*)\Z/,
  "CONNECT" => /\AOK MPD (\d+)\.(\d+).(\d+)\n\Z/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "127.0.0.1", port = 6600) ⇒ Mpc

Returns a new instance of Mpc.



14
15
16
17
# File 'lib/mpc.rb', line 14

def initialize(host = "127.0.0.1", port = 6600)
  @host = host
  @port = port
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



13
14
15
# File 'lib/mpc.rb', line 13

def host
  @host
end

#portObject

Returns the value of attribute port.



13
14
15
# File 'lib/mpc.rb', line 13

def port
  @port
end

Instance Method Details

#add(song, pos = nil) ⇒ Object



101
102
103
# File 'lib/mpc.rb', line 101

def add(song, pos = nil)
  send_command "addid #{song} #{pos}"
end

#connectObject



19
20
21
22
# File 'lib/mpc.rb', line 19

def connect
  @socket = socket
  @socket.gets
end

#current_songObject



92
93
94
95
# File 'lib/mpc.rb', line 92

def current_song
  song = to_hash(send_command("currentsong"))
  Song.new(song)
end

#disconnectObject



24
25
26
27
# File 'lib/mpc.rb', line 24

def disconnect
  @socket.close
  @socket = nil
end

#infoObject



97
98
99
# File 'lib/mpc.rb', line 97

def info
  "State: #{status[:state]} Volume: #{volume}% Artists: #{stats[:artists]} Albums: #{stats[:albums]} Songs: #{stats[:songs]}"
end

#nextObject



53
54
55
# File 'lib/mpc.rb', line 53

def next
  send_command "next"
end

#pauseObject



45
46
47
# File 'lib/mpc.rb', line 45

def pause
  send_command "pause 1"
end

#paused?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/mpc.rb', line 49

def paused?
  status[:state] == "pause"
end

#play(pos = "") ⇒ Object



29
30
31
# File 'lib/mpc.rb', line 29

def play(pos = "")
  send_command "play #{pos}"
end

#playing?Boolean

Returns:

  • (Boolean)


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

def playing?
  status[:state] == "play"
end

#previousObject



57
58
59
# File 'lib/mpc.rb', line 57

def previous
  send_command "previous"
end

#random(state = nil) ⇒ Object



61
62
63
64
# File 'lib/mpc.rb', line 61

def random(state = nil)
  state ||= bool_to_int(!random?)
  send_command "random #{state}"
end

#random?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/mpc.rb', line 66

def random?
  status[:random] == "1"
end

#repeat(state = nil) ⇒ Object



70
71
72
73
# File 'lib/mpc.rb', line 70

def repeat(state = nil)
  state ||= bool_to_int(!repeat?)
  send_command "repeat #{state}"
end

#repeat?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/mpc.rb', line 75

def repeat?
  status[:repeat] == "1"
end

#stopObject



37
38
39
# File 'lib/mpc.rb', line 37

def stop
  send_command "stop"
end

#stopped?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/mpc.rb', line 41

def stopped?
  status[:state] == "stop"
end

#volume(level = nil) ⇒ Object



79
80
81
82
# File 'lib/mpc.rb', line 79

def volume(level = nil)
  set_volume(level) if level
  get_volume
end

#volume_downObject



88
89
90
# File 'lib/mpc.rb', line 88

def volume_down
  set_volume(volume.to_i - 20)
end

#volume_upObject



84
85
86
# File 'lib/mpc.rb', line 84

def volume_up
  set_volume(volume.to_i + 20)
end