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 |
# File 'lib/subcl/subcl.rb', line 5 def initialize( = {}) @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 @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
141 142 143 144 |
# File 'lib/subcl/subcl.rb', line 141 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
48 49 50 51 |
# File 'lib/subcl/subcl.rb', line 48 def albumart_url(size = nil) current = @player.current @api.albumart_url(current, size) unless current.empty? end |
#albumlist ⇒ Object
126 127 128 129 130 |
# File 'lib/subcl/subcl.rb', line 126 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
134 135 136 137 138 |
# File 'lib/subcl/subcl.rb', line 134 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/subcl/subcl.rb', line 107 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
98 99 100 101 102 103 104 |
# File 'lib/subcl/subcl.rb', line 98 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
53 54 55 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 |
# File 'lib/subcl/subcl.rb', line 53 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 @api.random_songs(query) 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
122 123 124 |
# File 'lib/subcl/subcl.rb', line 122 def testNotify @notifier.notify("Hi!") end |