Class: Dhun::Handler

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

Overview

Handling commands sent by Dhun client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player = Dhun::Player.instance) ⇒ Handler

Returns a new instance of Handler.



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

def initialize(player = Dhun::Player.instance)
  @player = player
end

Instance Attribute Details

#playerObject

Returns the value of attribute player.



5
6
7
# File 'lib/dhun/handler.rb', line 5

def player
  @player
end

Instance Method Details

#clearObject



59
60
61
62
63
# File 'lib/dhun/handler.rb', line 59

def clear
  perform_action :clear,nil,
  :success => "Queue is cleared",
  :error => "Queue is not cleared"
end

#enqueue(files) ⇒ Object



21
22
23
24
25
# File 'lib/dhun/handler.rb', line 21

def enqueue(files)
  perform_action :enqueue, files, 
  :success => ["#{files.size} files queued", {:files => files}],
  :error => ["No files queued"]
end

#historyObject



76
77
78
79
# File 'lib/dhun/handler.rb', line 76

def history
  return [:success, "#{@player.history.size} files in history",{:history => @player.history}] unless @player.history.empty?
  return [:error, "No files in history"]
end

#next(skip_length = 1) ⇒ Object



45
46
47
# File 'lib/dhun/handler.rb', line 45

def next(skip_length=1)
  next_prev :next, 'queue',skip_length
end

#pauseObject



27
28
29
30
31
# File 'lib/dhun/handler.rb', line 27

def pause
  perform_action :pause,nil, 
  :success => "Dhun is paused at #{@player.current}",
  :error => "Dhun has already paused or stopped"
end

#playObject



11
12
13
14
15
16
17
18
19
# File 'lib/dhun/handler.rb', line 11

def play
  response =
  case @player.play
  when false then [:error,"already playing"]
  when true then [:success, "resuming playback"]
  when :empty then [:error, 'no file in queue']
  end
  response
end

#prev(skip_length = 1) ⇒ Object



49
50
51
# File 'lib/dhun/handler.rb', line 49

def prev(skip_length=1)
  next_prev :prev, 'history',skip_length
end

#resumeObject



33
34
35
36
37
# File 'lib/dhun/handler.rb', line 33

def resume
  perform_action :resume,nil,
  :success => "Dhun is resumed at #{@player.current}",
  :error => "Dhun has already resumed or stopped"
end

#shuffleObject



53
54
55
56
57
# File 'lib/dhun/handler.rb', line 53

def shuffle
  perform_action :shuffle,nil,
  :success => ['Queue is shuffled', { :queue => @player.queue }],
  :error => 'Dhun cannot shuffle(queue empty, same songs)'
end

#statusObject



66
67
68
69
70
71
72
73
74
# File 'lib/dhun/handler.rb', line 66

def status
  status_msg =
  case @player.status
  when :playing then "Dhun is running"
  when :paused  then "Dhun is paused"
  when :stopped then "Dhun has stopped"
  end
  [:success, status_msg, {:current => @player.current, :queue => @player.queue}]
end

#stopObject



39
40
41
42
43
# File 'lib/dhun/handler.rb', line 39

def stop
  perform_action :stop,nil,
  :success => "Dhun has stopped",
  :error => "Dhun has already stopped"
end