Class: Subcl

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

Constant Summary collapse

PLAYER_METHODS =
%i{play pause toggle stop next previous rewind}

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @options = {
    :interactive => true,
    :tty => true,
    :insert => false,
    :out_stream => STDOUT,
    :err_stream => STDERR
  }.merge! options

  @out = @options[:out_stream]
  @err = @options[:err_stream]

  begin
    @configs = Configs.new
  rescue => e
    @err.puts "Error initializing config"
    @err.puts e.message
    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

Raises:

  • (NoMethodError)


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

#apiObject

Returns the value of attribute api.



3
4
5
# File 'lib/subcl/subcl.rb', line 3

def api
  @api
end

#notifierObject

Returns the value of attribute notifier.



3
4
5
# File 'lib/subcl/subcl.rb', line 3

def notifier
  @notifier
end

#playerObject

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

#albumlistObject



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
    message = "No matching #{what}"
  else
    message = "No matches"
  end

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


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

#testNotifyObject



122
123
124
# File 'lib/subcl/subcl.rb', line 122

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