Class: Subcl
- Inherits:
-
Object
- Object
- Subcl
- Defined in:
- lib/subcl/subcl.rb,
lib/subcl/version.rb
Constant Summary collapse
- PLAYER_METHODS =
these methods will be passed through to the underlying player
%i{play pause toggle stop next previous rewind}
- VERSION =
'1.1.2'
Instance Attribute Summary collapse
-
#api ⇒ Object
Returns the value of attribute api.
-
#configs ⇒ Object
Returns the value of attribute configs.
-
#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
-
#any_sorter(query) ⇒ Object
returns a sorter proc for two hashes with the attribute :type and :name.
-
#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.
- #match_score(entity, query) ⇒ Object
- #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 50 51 52 53 |
# 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, :wildcard_order => %i{playlist album artist song} }.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]}" }, :any => proc { |thing| #TODO this works, but looks confusing when multiple types are displayed @display[thing[:type]].call(thing) } } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, args) ⇒ Object
179 180 181 182 |
# File 'lib/subcl/subcl.rb', line 179 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 |
#configs ⇒ Object
Returns the value of attribute configs.
3 4 5 |
# File 'lib/subcl/subcl.rb', line 3 def configs @configs 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
55 56 57 58 |
# File 'lib/subcl/subcl.rb', line 55 def albumart_url(size = nil) current = @player.current_song @api.albumart_url(current.file, size) if current end |
#albumlist ⇒ Object
163 164 165 166 167 |
# File 'lib/subcl/subcl.rb', line 163 def albumlist @api.albumlist.each do |album| @display[:album].call(album) end end |
#any_sorter(query) ⇒ Object
returns a sorter proc for two hashes with the attribute :type and :name
it will use split(“ ”) on query and then count how many words of query each :name contains. If two hashes have the same amount of query words, @options is used
the closest matches will be at the beginning
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/subcl/subcl.rb', line 115 def any_sorter(query) #TODO do things with query! find the things that match the query the most order = @options[:wildcard_order] lambda do |e1, e2| cmp = match_score(e1, query) <=> match_score(e2, query) if cmp == 0 out = order.index(e1[:type]) <=> order.index(e2[:type]) out else -cmp end 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
171 172 173 174 175 |
# File 'lib/subcl/subcl.rb', line 171 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 |
#match_score(entity, query) ⇒ Object
129 130 131 132 133 |
# File 'lib/subcl/subcl.rb', line 129 def match_score(entity, query) query.split(' ').inject(0) do |memo, word| memo + (entity[:name].downcase.include?(word.downcase) ? 1 : 0) end end |
#no_matches(what = nil) ⇒ Object
print an error that no matches were found, then exit with code 2
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/subcl/subcl.rb', line 144 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
135 136 137 138 139 140 141 |
# File 'lib/subcl/subcl.rb', line 135 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
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 104 105 |
# File 'lib/subcl/subcl.rb', line 60 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, any entities = @api.search(query, type) entities.sort!(&any_sorter(query)) if type == :any 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
159 160 161 |
# File 'lib/subcl/subcl.rb', line 159 def testNotify @notifier.notify("Hi!") end |