Class: Subcl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Subcl

Returns a new instance of Subcl.



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
45
46
47
48
49
50
51
# File 'lib/Subcl.rb', line 9

def initialize(options = {})
  #default options
  @options = {
    :interactive => true,
    :tty => true,
    :insert => false
  }

  #overwrite defaults with given options
  @options.merge! options

  begin
  @configs = Configs.new
  rescue => e
    $stderr.puts "Error initializing config"
    $stderr.puts e.message
    exit
  end

  @player = Mpc.new
  @player.debug = @options[:debug]

  @notifier = Notify.new @configs.notifyMethod

  @display = {
    :song => proc { |song|
      "#{song['title']} by #{song['artist']} on #{song['album']} (#{song['year']})"
    },
    :album => proc { |album|
      "#{album['name']} by #{album['artist']} in #{album['year']}"
    },
    :artist => proc { |artist|
      "#{artist['name']}"
    },
    :playlist => proc { |playlist|
      "#{playlist[:name]} by #{playlist[:owner]}"
    },
  }

  @subsonic = Subsonic.new(@configs, @display)
  @subsonic.interactive = @options[:interactive]

end

Instance Attribute Details

#notifierObject (readonly)

Returns the value of attribute notifier.



7
8
9
# File 'lib/Subcl.rb', line 7

def notifier
  @notifier
end

#playerObject (readonly)

Returns the value of attribute player.



7
8
9
# File 'lib/Subcl.rb', line 7

def player
  @player
end

#subsonicObject (readonly)

Returns the value of attribute subsonic.



7
8
9
# File 'lib/Subcl.rb', line 7

def subsonic
  @subsonic
end

Instance Method Details

#albumartUrl(size = nil) ⇒ Object



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

def albumartUrl(size = nil)
  current = @player.current
  puts @subsonic.albumartUrl(current, size) unless current.empty?
end

#noMatches(what = nil) ⇒ Object

print an error that no matches were found, then exit with code 2



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/Subcl.rb', line 139

def noMatches(what = nil)
  if what
    message = "No matching #{what}"
  else
    message = "No matches"
  end

  if @options[:tty]
    $stderr.puts message
  else
    @notifier.notify(message) 
  end
  exit 2 
end

#queue(query, type, inArgs = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/Subcl.rb', line 58

def queue(query, type, inArgs = {})
  args = {
    :clear => false, #whether to clear the playlist prior to adding songs
    :play => false, #whether to start the player after adding the songs
    :insert => false #whether to insert the songs after the current instead of the last one
  }
  args.merge! inArgs

  if @options[:current]
    unless [:album, :artist].include? type
      raise ArgumentError, "'current' option can only be used with albums or artists."
    end
    query = @player.current type
  end

  songs = case type
          when :song
            @subsonic.song(query)
          when :album
            @subsonic.albumSongs(query)
          when :artist
            @subsonic.artistSongs(query)
          when :playlist
            @subsonic.playlistSongs(query)
          when :randomSong
            begin
              @subsonic.randomSongs(query)
            rescue ArgumentError
              raise ArgumentError, "random-songs takes an integer as argument"
            end
          end

  if songs.empty?
    noMatches
  end

  @player.clear if args[:clear]

  songs.shuffle! if @options[:shuffle]

  songs.each do |song|
    @player.add(song, args[:insert])
  end

  @player.play if args[:play]
end

#searchAlbum(name) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/Subcl.rb', line 116

def searchAlbum(name)
  albums = @subsonic.albums(name)
  if(albums.size == 0)
    noMatches("album")
  else
    albums.each do |album|
      puts @display[:album].call(album)
    end
  end
end

#searchArtist(name) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/Subcl.rb', line 127

def searchArtist(name)
  artists = @subsonic.artists(name)
  if(artists.size == 0)
    noMatches("artist")
  else
    artists.each do |artist|
      puts @display[:artist].call(artist)
    end
  end
end

#searchSong(name) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/Subcl.rb', line 105

def searchSong(name)
  songs = @subsonic.songs(name)
  if(songs.size == 0)
    noMatches("song")
  else
    songs.each do |song|
      puts @display[:song].call(song)
    end
  end
end

#testNotifyObject



154
155
156
# File 'lib/Subcl.rb', line 154

def testNotify
  @notifier.notify("Hi!")
end