Class: Muzak::Instance

Inherits:
Object
  • Object
show all
Includes:
Cmd, Utils
Defined in:
lib/muzak/instance.rb

Overview

Encapsulates the entirety of muzak's running state.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

album_art?, #build_response, #danger, #debug, #debug?, #error, #error!, music?, #output, #pretty, #verbose, #verbose?, which?

Methods included from Cmd

#albums_by_artist, #clear_queue, commands, #config_get, #dump_index, #enqueue_album, #enqueue_artist, #enqueue_playlist, #help, #jukebox, #list_albums, #list_artists, #list_playlists, #list_plugins, #list_queue, #next, #now_playing, #pause, #ping, #play, #player_activate, #player_deactivate, #playlist_add_album, #playlist_add_artist, #playlist_add_current, #playlist_del_current, #playlist_delete, #playlist_shuffle, #previous, #quit, #reload_index, #shuffle_queue, #songs_by_artist, #toggle

Constructor Details

#initializeInstance

Returns a new instance of Instance.


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/muzak/instance.rb', line 42

def initialize
  verbose "muzak is starting..."

  error! "#{Config.music} doesn't exist" unless File.exist?(Config.music)

  @index     = Index.load_index!
  @player    = Player.load_player!(self)
  @plugins   = Plugin.load_plugins!
  @playlists = Playlist.load_playlists!

  enqueue_playlist Config.default_playlist if Config.default_playlist

  event :instance_started, self
end

Instance Attribute Details

#indexIndex (readonly)

Returns the instance's music index.

Returns:

  • (Index)

    the instance's music index


31
32
33
# File 'lib/muzak/instance.rb', line 31

def index
  @index
end

#playerStubPlayer (readonly)

Returns the instance's player.

Returns:

  • (StubPlayer)

    the instance's player


34
35
36
# File 'lib/muzak/instance.rb', line 34

def player
  @player
end

#playlistsHash{String => Playlist} (readonly)

Returns the instance's playlists.

Returns:

  • (Hash{String => Playlist})

    the instance's playlists


40
41
42
# File 'lib/muzak/instance.rb', line 40

def playlists
  @playlists
end

#pluginsArray<StubPlugin> (readonly)

Returns the instance's plugins.

Returns:

  • (Array<StubPlugin>)

    the instance's plugins


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

def plugins
  @plugins
end

Instance Method Details

#command(cmd, *args) ⇒ Hash

Sends a command to the instance.

Examples:

instance.command "enqueue-playlist", "favorites"
instance.command "pause"

Parameters:

  • cmd (String)

    the name of the command

  • args (Array<String>)

    the command's arguments

Returns:

  • (Hash)

    the command's response hash


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/muzak/instance.rb', line 16

def command(cmd, *args)
  if Cmd.commands.include?(cmd)
    meth = method(Config.resolve_command(cmd))
    if meth.arity == args.size || meth.arity <= -1
      meth.call(*args)
    else
      build_response error: "got #{args.size} args, needed #{meth.arity}"
    end
  else
    danger "unknown command: '#{cmd}'"
    build_response error: "unknown command '#{cmd}'"
  end
end

#event(type, *args) ⇒ void

Note:

Config::PLUGIN_EVENTS contains all valid events.

This method returns an undefined value.

Dispatch an event to all plugins.

Parameters:

  • type (Symbol)

    the type of event to dispatch

  • args (Array)

    the event's arguments


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/muzak/instance.rb', line 62

def event(type, *args)
  # don't propagate events if disabled by the user
  return unless Config.events
  return unless Config::PLUGIN_EVENTS.include?(type)

  plugins.each do |plugin|
    begin
      Thread.new { plugin.send(type, *args) }
    rescue => e
      error "something went wrong in #{plugin.class.plugin_name}: #{e}"
    end
  end
end