Class: Subcl
- Inherits:
-
Object
- Object
- Subcl
- Defined in:
- lib/subcl/subcl.rb
Constant Summary collapse
- PLAYER_METHODS =
%i{play pause toggle stop next previous rewind}
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#notifier ⇒ Object
Returns the value of attribute notifier.
-
#player ⇒ Object
Returns the value of attribute player.
Instance Method Summary collapse
- #albumart_url(size = nil) ⇒ Object
- #albumlist ⇒ Object
-
#initialize(options = {}) ⇒ Subcl
constructor
A new instance of Subcl.
-
#invoke_picker(array, &display_proc) ⇒ Object
show an interactive picker that lists every element of the array using &display_proc The user can then choose one, many or no of the elements which will be returned as array.
- #method_missing(name, args) ⇒ Object
-
#no_matches(what = nil) ⇒ Object
print an error that no matches were found, then exit with code 2.
- #print(name, type) ⇒ Object
- #queue(query, type, inArgs = {}) ⇒ Object
- #testNotify ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Subcl
Returns a new instance of Subcl.
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 45 46 47 48 49 |
# File 'lib/subcl/subcl.rb', line 5 def initialize( = {}) #TODO merge options and configs @options = { :interactive => true, :tty => true, :insert => false, :out_stream => STDOUT, :err_stream => STDERR }.merge! @out = @options[:out_stream] @err = @options[:err_stream] begin @configs = Configs.new rescue => e @err.puts "Error initializing config" @err.puts e. exit 4 end @configs[:random_song_count] = @options[:random_song_count] if @options[:random_song_count] @player = @options[:mock_player] || Player.new @api = @options[:mock_api] || SubsonicAPI.new(@configs) @notifier = Notify.new @configs[:notify_method] @display = { :song => proc { |song| @out.puts sprintf "%-20.20s %-20.20s %-20.20s %-4.4s", song[:title], song[:artist], song[:album], song[:year] }, :album => proc { |album| @out.puts sprintf "%-30.30s %-30.30s %-4.4s", album[:name], album[:artist], album[:year] }, :artist => proc { |artist| @out.puts "#{artist[:name]}" }, :playlist => proc { |playlist| @out.puts "#{playlist[:name]} by #{playlist[:owner]}" }, } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, args) ⇒ Object
145 146 147 148 |
# File 'lib/subcl/subcl.rb', line 145 def method_missing(name, args) raise NoMethodError unless PLAYER_METHODS.include? name @player.send(name) end |
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
3 4 5 |
# File 'lib/subcl/subcl.rb', line 3 def api @api end |
#notifier ⇒ Object
Returns the value of attribute notifier.
3 4 5 |
# File 'lib/subcl/subcl.rb', line 3 def notifier @notifier end |
#player ⇒ Object
Returns the value of attribute player.
3 4 5 |
# File 'lib/subcl/subcl.rb', line 3 def player @player end |
Instance Method Details
#albumart_url(size = nil) ⇒ Object
51 52 53 54 |
# File 'lib/subcl/subcl.rb', line 51 def albumart_url(size = nil) current = @player.current_song @api.albumart_url(current.file, size) if current end |
#albumlist ⇒ Object
130 131 132 133 134 |
# File 'lib/subcl/subcl.rb', line 130 def albumlist @api.albumlist.each do |album| @display[:album].call(album) end end |
#invoke_picker(array, &display_proc) ⇒ Object
show an interactive picker that lists every element of the array using &display_proc The user can then choose one, many or no of the elements which will be returned as array
138 139 140 141 142 |
# File 'lib/subcl/subcl.rb', line 138 def invoke_picker(array, &display_proc) return array if array.length <= 1 return [array.first] unless @options[:interactive] return Picker.new(array).pick(&display_proc) end |
#no_matches(what = nil) ⇒ Object
print an error that no matches were found, then exit with code 2
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/subcl/subcl.rb', line 111 def no_matches(what = nil) if what = "No matching #{what}" else = "No matches" end if @options[:tty] @err.puts else @notifier.notify() end exit 2 end |
#print(name, type) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/subcl/subcl.rb', line 102 def print(name, type) entities = @api.search(name, type) no_matches(type) if entities.empty? entities.each do |entity| @display[type].call(entity) end end |
#queue(query, type, inArgs = {}) ⇒ Object
56 57 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 |
# File 'lib/subcl/subcl.rb', line 56 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] query = case type when :album @player.current_song.album when :artist @player.current_song.artist else raise ArgumentError, "'current' option can only be used with albums or artists." end end songs = case type when :randomSong begin count = query.empty? ? @configs[:random_song_count] : query @api.random_songs(count) rescue ArgumentError raise ArgumentError, "random-songs takes an integer as argument" end else #song, album, artist, playlist entities = @api.search(query, type) entities = invoke_picker(entities, &@display[type]) @api.get_songs(entities) end no_matches if songs.empty? @player.clearstop if args[:clear] songs.shuffle! if @options[:shuffle] songs.each do |song| @player.add(song, args[:insert]) end @player.play if args[:play] end |
#testNotify ⇒ Object
126 127 128 |
# File 'lib/subcl/subcl.rb', line 126 def testNotify @notifier.notify("Hi!") end |