Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/itc/command.rb

Class Method Summary collapse

Class Method Details

.execute(action, filters) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/itc/command.rb', line 4

def self.execute(action, filters)
    db = Database.new
    player = Player.new
    xml = ENV["HOME"] + "/Music/iTunes/iTunes Music Library.xml"

    supported_filters = ("artist" "album" "song" "genre" "year")
    filters.keys.each do |filter|
        raise UnknownFilterException, filter unless supported_filters.include? filter
    end

    case action
    when "index"
        db.reindex MusicLibrary.new(xml)
    when "list"
        raise InvalidFilterNumberException, "No filter specified" if filters.empty?
        puts db.list(filters).collect { |f| f.last }
    when "play"
        player.play filters.empty? ? {} : db.list_ids(filters)
    when "pause"
        player.pause
    when "next"
        player.next
    when "previous", "prev"
        player.previous
    when "status"
        if player.state == "stopped"
            puts "iTunes is #{player.state}\n"
            return
        end

        curr_song = player.current_track
        state_icon = player.state == "playing" ? "" : ""
        playlist = player.current_playlist_tracks.split(", ")
        db.list_by_ids(playlist.size < 1000 ? playlist : [curr_song]).collect do |track|
            printf " %s %s - %s - %s\n", track[0].to_s == curr_song ? state_icon : " ",
            track[1], track[2], track[3]
        end
    else
        raise UnknownCommandException
    end
end