Class: MPD::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/mpd/controller.rb,
lib/mpd/controller/do.rb,
lib/mpd/controller/audio.rb,
lib/mpd/controller/stats.rb,
lib/mpd/controller/config.rb,
lib/mpd/controller/player.rb,
lib/mpd/controller/status.rb,
lib/mpd/controller/toggle.rb,
lib/mpd/controller/channels.rb,
lib/mpd/controller/commands.rb,
lib/mpd/controller/database.rb,
lib/mpd/controller/decoders.rb,
lib/mpd/controller/stickers.rb,
lib/mpd/controller/playlists.rb,
lib/mpd/controller/supported_tags.rb,
lib/mpd/controller/current_playlist.rb,
lib/mpd/controller/supported_protocols.rb

Overview

This is the main class to manage moc.

The class also acts as a Socket if needed.

Defined Under Namespace

Classes: Audio, Channels, Commands, Config, CurrentPlaylist, Database, Decoders, Do, Player, Playlists, Stats, Status, Stickers, SupportedProtocols, SupportedTags, Toggle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 6600) ⇒ Controller

Returns a new instance of Controller.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mpd/controller.rb', line 40

def initialize (host = 'localhost', port = 6600)
  @socket  = File.exists?(host) ? UNIXSocket.new(host) : TCPSocket.new(host, port)
  @version = @socket.readline.chomp.split(' ', 3).last

  if unix?
    @path = host
  else
    @host = host
    @port = port
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id, *args, &block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/mpd/controller.rb', line 64

def method_missing (id, *args, &block)
  if @socket.respond_to? id
    return @socket.__send__ id, *args, &block
  end

  super
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



38
39
40
# File 'lib/mpd/controller.rb', line 38

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



38
39
40
# File 'lib/mpd/controller.rb', line 38

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



38
39
40
# File 'lib/mpd/controller.rb', line 38

def port
  @port
end

#versionObject (readonly)

Returns the value of attribute version.



38
39
40
# File 'lib/mpd/controller.rb', line 38

def version
  @version
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/mpd/controller.rb', line 101

def active?
  self.do(:ping).success?
rescue
  false
end

#audioObject



123
124
125
# File 'lib/mpd/controller.rb', line 123

def audio
  Audio.new(self)
end

#authenticate(password) ⇒ Object



95
96
97
98
99
# File 'lib/mpd/controller.rb', line 95

def authenticate (password)
  self.do :password, password

  self
end

#channel(name) ⇒ Object



179
180
181
# File 'lib/mpd/controller.rb', line 179

def channel (name)
  channels[name]
end

#channelsObject



171
172
173
# File 'lib/mpd/controller.rb', line 171

def channels
  @channels ||= Channels.new(self)
end

#commandsObject



139
140
141
# File 'lib/mpd/controller.rb', line 139

def commands
  Commands.new(self)
end

#configObject



127
128
129
# File 'lib/mpd/controller.rb', line 127

def config
  Config.new(self)
end

#databaseObject



115
116
117
# File 'lib/mpd/controller.rb', line 115

def database
  @database ||= Database.new(self)
end

#decodersObject



143
144
145
# File 'lib/mpd/controller.rb', line 143

def decoders
  Decoders.new(self)
end

#disconnect!Object



111
112
113
# File 'lib/mpd/controller.rb', line 111

def disconnect!
  self.do :close
end

#do(*args, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mpd/controller.rb', line 72

def do (*args, &block)
  if block
    Do.new(self, &block).send
  else
    name    = args.shift
    command = Protocol::Command.new(name, args)

    @socket.puts command.to_s

    Protocol::Response.read(self, command)
  end
end

#do_and_raise_if_needed(*args) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/mpd/controller.rb', line 85

def do_and_raise_if_needed (*args)
  response = self.do *args

  if response.is_a?(Protocol::Error)
    raise response.message
  end

  response
end

#kill!Object



107
108
109
# File 'lib/mpd/controller.rb', line 107

def kill!
  self.do :kill
end

#loop(*what) ⇒ Object



199
200
201
202
203
# File 'lib/mpd/controller.rb', line 199

def loop (*what)
  loop do
    yield wait_for *what
  end
end

#playerObject



151
152
153
# File 'lib/mpd/controller.rb', line 151

def player
  @player ||= Player.new(self)
end

#playlist(name = nil) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/mpd/controller.rb', line 159

def playlist (name = nil)
  if name
    playlists[name]
  else
    @playlist ||= CurrentPlaylist.new(self)
  end
end

#playlistsObject



155
156
157
# File 'lib/mpd/controller.rb', line 155

def playlists
  @playlists ||= Playlists.new(self)
end

#respond_to_missing?(id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/mpd/controller.rb', line 60

def respond_to_missing? (id, include_private = false)
  @socket.respond_to? id, include_private
end

#statsObject



119
120
121
# File 'lib/mpd/controller.rb', line 119

def stats
  Stats.new(self)
end

#statusObject



167
168
169
# File 'lib/mpd/controller.rb', line 167

def status
  Status.new(self)
end

#stickersObject



175
176
177
# File 'lib/mpd/controller.rb', line 175

def stickers
  @stickers ||= Stickers.new(self)
end

#stop_waitingObject



195
196
197
# File 'lib/mpd/controller.rb', line 195

def stop_waiting
  self.do :noidle
end

#supported_protocolsObject



135
136
137
# File 'lib/mpd/controller.rb', line 135

def supported_protocols
  SupportedProtocols.new(self)
end

#supported_tagsObject



131
132
133
# File 'lib/mpd/controller.rb', line 131

def supported_tags
  SupportedTags.new(self)
end

#tcp?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/mpd/controller.rb', line 56

def tcp?
  @socket.is_a? TCPSocket
end

#toggleObject



147
148
149
# File 'lib/mpd/controller.rb', line 147

def toggle
  @toggle ||= Toggle.new(self)
end

#unix?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/mpd/controller.rb', line 52

def unix?
  @socket.is_a? UNIXSocket
end

#waitObject



183
184
185
186
187
# File 'lib/mpd/controller.rb', line 183

def wait
  self.do(:idle).map(&:last)
rescue Interrupt
  stop_waiting and raise # my undead army
end

#wait_for(*args) ⇒ Object



189
190
191
192
193
# File 'lib/mpd/controller.rb', line 189

def wait_for (*args)
  self.do(:idle, *args.flatten.compact.uniq).map(&:last)
rescue Interrupt
  stop_waiting and raise # my undead army
end