Module: Muzak::Cmd

Included in:
Instance
Defined in:
lib/muzak/cmd.rb,
lib/muzak/cmd/meta.rb,
lib/muzak/cmd/index.rb,
lib/muzak/cmd/config.rb,
lib/muzak/cmd/player.rb,
lib/muzak/cmd/playlist.rb

Overview

The namespace for all commands exposed by muzak.

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commandsArray<String>

Returns all valid muzak commands.

Returns:

  • (Array<String>)

    all valid muzak commands



11
12
13
14
# File 'lib/muzak/cmd.rb', line 11

def self.commands
  commands = instance_methods.map(&:to_s).reject { |m| m.start_with?("_") }
  commands.map { |c| Config.resolve_method c }
end

Instance Method Details

#albums_by_artist(*args) ⇒ Object

List all albums by the given artist in the index.

muzak command:

  • albums-by-artist <artist name>

example invocation:

  • muzak> albums-by-artist Your Favorite Artist



24
25
26
27
28
29
30
31
32
# File 'lib/muzak/cmd/index.rb', line 24

def albums_by_artist(*args)
  artist = args.join(" ")

  albums = index.albums_by(artist).map(&:title)

  build_response data: {
    albums: albums
  }
end

#clear_queueObject

Note:

This does not (usually) stop the current song.

Tell the player to clear its internal queue.

muzak command:

  • clear-queue

example invocation:

  • muzak> clear-queue



151
152
153
154
155
# File 'lib/muzak/cmd/player.rb', line 151

def clear_queue
  player.clear_queue

  build_response
end

#config_get(key) ⇒ Object

Query the Muzak::Config for a given key.

muzak command:

  • config-get <key>

example invocation:

  • muzak> config-get player



8
9
10
11
12
# File 'lib/muzak/cmd/config.rb', line 8

def config_get(key)
  value = Config.send Config.resolve_command(key)

  build_response data: { key => value }
end

#enqueue_album(*args) ⇒ Object

Tell the player to enqueue the given album.

muzak command:

  • enqueue-album <album name>

example invocation:

  • muzak> enqueue-album Your Favorite Album



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/muzak/cmd/player.rb', line 103

def enqueue_album(*args)
  album_name = args.join(" ")

  album = index.albums[album_name]

  if album
    player.enqueue_album album
    build_response
  else
    build_response error: "no such album: '#{album_name}'"
  end
end

#enqueue_artist(*args) ⇒ Object

Tell the player to enqueue all songs by the given artist.

muzak command:

  • enqueue-artist <artist name>

example invocation:

  • muzak> enqueue-artist Your Favorite Artist



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/muzak/cmd/player.rb', line 86

def enqueue_artist(*args)
  artist = args.join(" ")
  albums = index.albums_by(artist)

  unless albums.empty?
    albums.each do |album|
      player.enqueue_album album
    end
    build_response
  else
    build_response error: "no albums by: '#{artist}'"
  end
end

#enqueue_playlist(pname) ⇒ Object

Add the given playlist to the player's queue.

muzak command:

  • enqueue-playlist <playlist>

example invocation:

  • muzak> enqueue-playlist favorites



27
28
29
30
31
32
# File 'lib/muzak/cmd/playlist.rb', line 27

def enqueue_playlist(pname)
  player.enqueue_playlist(playlists[pname])
  event :playlist_enqueued, playlists[pname]

  build_response
end

#help(*args) ⇒ Object

Return a "helpful" listing of commands.

muzak command:

  • help

example invocation:

  • muzak> help



18
19
20
21
22
# File 'lib/muzak/cmd/meta.rb', line 18

def help(*args)
  build_response data: {
    commands: Muzak::Cmd.commands
  }
end

#jukebox(count = Config.jukebox_size) ⇒ Object

Tell the player to load the given number of random songs.

muzak command:

  • jukebox [count]

example invocation:

  • muzak> jukebox 150



119
120
121
122
123
124
125
126
127
# File 'lib/muzak/cmd/player.rb', line 119

def jukebox(count = Config.jukebox_size)
  songs = index.jukebox(count.to_i)

  songs.each { |s| player.enqueue_song s }

  build_response data: {
    jukebox: songs.map(&:full_title)
  }
end

#list_albumsObject

List all albums in the index.

muzak command:

  • list-albums

example invocation:

  • muzak> list-albums



15
16
17
18
19
# File 'lib/muzak/cmd/index.rb', line 15

def list_albums
  build_response data: {
    albums: index.album_names
  }
end

#list_artistsObject

List all artists in the index.

muzak command:

  • list-artists

example invocation:

  • muzak> list-artists



6
7
8
9
10
# File 'lib/muzak/cmd/index.rb', line 6

def list_artists
  build_response data: {
    artists: index.artists
  }
end

#list_playlistsObject

List all currently available playlists.

muzak command:

  • list-playlists

example invocation:

  • muzak> list-playlists



6
7
8
9
10
# File 'lib/muzak/cmd/playlist.rb', line 6

def list_playlists
  build_response data: {
    playlists: Playlist.playlist_names
  }
end

#list_pluginsObject

Note:

This list will differ from loaded plugins, if not all available plugins are configured.

List all available plugins.

muzak command:

  • list-plugins

example invocation:

  • muzak> list-plugins



29
30
31
32
33
# File 'lib/muzak/cmd/meta.rb', line 29

def list_plugins
  build_response data: {
    plugins: Plugin.plugin_names
  }
end

#list_queueObject

Tell the player to list its internal queue.

muzak command:

  • list-queue

example invocation:

  • muzak> list-queue



132
133
134
135
136
# File 'lib/muzak/cmd/player.rb', line 132

def list_queue
  build_response data: {
    queue: player.list_queue.map(&:title)
  }
end

#nextObject

Tell the player to load the next song.

muzak command:

  • next

example invocation:

  • muzak> next



68
69
70
71
72
# File 'lib/muzak/cmd/player.rb', line 68

def next
  player.next_song

  build_response
end

#now_playingObject

Retrieve the currently playing song from the player and print it.

muzak command:

  • now-playing

example invocation:

  • muzak> now-playing



160
161
162
163
164
165
166
167
168
# File 'lib/muzak/cmd/player.rb', line 160

def now_playing
  if player.playing?
    build_response data: {
      playing: player.now_playing&.full_title
    }
  else
    build_response error: "no currently playing song"
  end
end

#pauseObject

Tell the player to pause.

muzak command:

  • pause

example invocation:

  • muzak> pause



46
47
48
49
50
# File 'lib/muzak/cmd/player.rb', line 46

def pause
  player.pause

  build_response
end

#pingObject

Return a simple heartbeat message.

muzak command:

  • ping

example invocation:

  • muzak> ping



6
7
8
9
10
11
12
13
# File 'lib/muzak/cmd/meta.rb', line 6

def ping
  timestamp = Time.now.to_i
  debug "pong: #{timestamp}"

  build_response data: {
    pong: timestamp
  }
end

#playObject

Tell the player to begin playback.

muzak command:

  • play

example invocation:

  • muzak> play



37
38
39
40
41
# File 'lib/muzak/cmd/player.rb', line 37

def play
  player.play

  build_response
end

#player_activateObject

Note:

Many playback commands will automatically activate the player.

Activate the configured player.

muzak command:

  • player-activate

example invocation:

  • muzak> player-activate



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/muzak/cmd/player.rb', line 7

def player_activate
  if player.running?
    danger "player is already running"
  else
    player.activate!
  end

  build_response data: {
    player: player.class.name
  }
end

#player_deactivateObject

Note:

Deactivating the player (usually) ends playback immediately.

Deactivate the configured player.

muzak command:

  • player-deactivate

example invocation:

  • muzak> player-deactivate



23
24
25
26
27
28
29
30
31
32
# File 'lib/muzak/cmd/player.rb', line 23

def player_deactivate
  danger "player is not running" unless player.running?

  # do cleanup even if the player isn't running, just in case
  player.deactivate!

  build_response data: {
    player: player.class.name
  }
end

#playlist_add_album(pname, *args) ⇒ Object

Add the given album to the given playlist.

muzak command:

  • playlist-add-album <playlist> <album name>

example invocation:

  • muzak> playlist-add-album favorites Your Favorite Album



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/muzak/cmd/playlist.rb', line 37

def playlist_add_album(pname, *args)
  album_name = args.join(" ")
  album = index.albums[album_name]

  if album
    playlists[pname].add(album.songs)
    build_response
  else
    build_response error: "no such album: '#{album_name}'"
  end
end

#playlist_add_artist(pname, *args) ⇒ Object

Add the given artist to the given playlist.

muzak command:

  • playlist-add-artist <playlist> <artist name>

example invocation:

  • muzak> playlist-add-artist dad-rock The Rolling Stones



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/muzak/cmd/playlist.rb', line 52

def playlist_add_artist(pname, *args)
  artist = args.join(" ")
  songs = index.songs_by(artist)

  unless songs.empty?
    playlists[pname].add(songs)
    build_response
  else
    build_response error: "no songs by artist: '#{artist}'"
  end
end

#playlist_add_current(pname) ⇒ Object

Add the currently playing song to the given playlist.

See Also:

muzak command:

  • playlist-add-current <playlist>

example invocation:

  • muzak> playlist-add-current favorites



68
69
70
71
72
73
74
75
# File 'lib/muzak/cmd/playlist.rb', line 68

def playlist_add_current(pname)
  if player.running?
    playlists[pname].add player.now_playing
    build_response
  else
    build_response error: "the player is not running"
  end
end

#playlist_del_current(pname) ⇒ Object

Deletes the currently playing song from the given playlist.

See Also:

muzak command:

  • playlist-del-current <playlist>

example invocation:

  • muzak> playlist-del-current favorites



81
82
83
84
85
86
87
88
# File 'lib/muzak/cmd/playlist.rb', line 81

def playlist_del_current(pname)
  if player.running?
    playlists[pname].delete player.now_playing
    build_response
  else
    build_response error: "the player is not running"
  end
end

#playlist_delete(pname) ⇒ Object

Delete the given playlist.

muzak command:

  • playlist-delete <playlist>

example invocation:

  • muzak> playlist-delete favorites



15
16
17
18
19
20
21
22
# File 'lib/muzak/cmd/playlist.rb', line 15

def playlist_delete(pname)
  debug "deleting playist '#{pname}'"

  Playlist.delete!(pname)
  playlists[pname] = nil

  build_response
end

#playlist_shuffle(pname) ⇒ Object

Shuffle the given playlist.

See Also:

muzak command:

  • playlist-shuffle <playlist>

example invocation:

  • muzak> playlist-shuffle dad-rock



94
95
96
97
# File 'lib/muzak/cmd/playlist.rb', line 94

def playlist_shuffle(pname)
  playlists[pname].shuffle!
  build_response
end

#previousObject

Tell the player to load the previous song.

muzak command:

  • previous

example invocation:

  • muzak> previous



77
78
79
80
81
# File 'lib/muzak/cmd/player.rb', line 77

def previous
  player.previous_song

  build_response
end

#quitObject

Terminates the muzak instance (not just the client).

muzak command:

  • quit

example invocation:

  • muzak> quit



38
39
40
41
42
43
44
# File 'lib/muzak/cmd/meta.rb', line 38

def quit
  verbose "muzak is quitting..."
  player.deactivate!

  event :instance_quitting
  build_response data: "quitting"
end

#shuffle_queueObject

Tell the player to shuffle its internal queue.

muzak command:

  • shuffle-queue

example invocation:

  • muzak> shuffle-queue



141
142
143
144
145
# File 'lib/muzak/cmd/player.rb', line 141

def shuffle_queue
  player.shuffle_queue

  build_response
end

#songs_by_artist(*args) ⇒ Object

List all songs by the given artist in the index.

muzak command:

  • songs-by-artist <artist name>

example invocation:

  • muzak> songs-by-artist Your Next Favorite Artist



37
38
39
40
41
42
43
44
45
# File 'lib/muzak/cmd/index.rb', line 37

def songs_by_artist(*args)
  artist = args.join(" ")

  songs = index.songs_by(artist).map(&:title)

  build_response data: {
    songs: songs
  }
end

#toggleObject

Tell the player to toggle its playback state.

muzak command:

  • toggle

example invocation:

  • muzak> toggle



55
56
57
58
59
60
61
62
63
# File 'lib/muzak/cmd/player.rb', line 55

def toggle
  if player.playing?
    player.pause
  else
    player.play
  end

  build_response
end