dhun 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CONTRIBUTORS ADDED
@@ -0,0 +1 @@
1
+ * achiu - http://github.com/achiu
data/FIX.md CHANGED
@@ -1,6 +1,7 @@
1
1
  ## Fixes/Improvements required
2
2
 
3
- * Refactor multiple calls to result.to_json
4
3
  * Refactor handler calls by converting it from module to subclass of EventMachine::Connection object
5
- * Put querying code in play and enqueue methods inside the player
6
- * Clean up parser to avoid any SQL-injection style errors in args
4
+
5
+ * store the socket information somehow so the runner remembers?
6
+
7
+ * have play jump to queue number
data/README.md CHANGED
@@ -19,16 +19,20 @@ put the binaries in the right place. You will need to have XCode installed for
19
19
  the `gem` command to compile the native extensions.
20
20
 
21
21
  $ gem sources -a http://gemcutter.org
22
- $ gem install dhun
22
+ $ gem install dhun
23
23
 
24
24
  ### Starting Dhun
25
25
 
26
- $ dhun start
26
+ $ dhun start_server
27
27
  Starting Dhun
28
28
 
29
- Pass the `-d` option to run the server as a daemon in the background. See
30
- `dhun -h` for more options.
29
+ this runs the Dhun server as a daemon by default. to not run it as a daemon:
31
30
 
31
+ $ dhun start_server --foreground
32
+ or
33
+ $ dhun start_server -f
34
+
35
+ See `dhun help start_server for more information`
32
36
 
33
37
  ### Querying for files
34
38
 
@@ -38,53 +42,105 @@ will look for files matching that keyword and start playing them.
38
42
  You can also query the Spotlight database before playing the files, with the
39
43
  `query` command.
40
44
 
41
- $ dhun query here
42
- 3 Results
43
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/01 40 Day Dream.mp3
44
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/02 Janglin.mp3
45
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/03 Carries On.mp3
45
+ $ dhun query deadmau5
46
+
47
+ Querying: deadmau5 | 6 Results
48
+ 0 : /Volumes/Storage/Music/Grand.Theft.Auto.IV-Radio.Station.Rips-AiTB/Electro-Choc/03 One + One - No Pressure (Deadmau5 Remix).mp3
49
+ 1 : /Volumes/Storage/Music/Grand.Theft.Auto.IV-Radio.Station.Rips-AiTB/Electro-Choc/09 Chris Lake vs. Deadmau5 - I Thought Inside Out (Original Mix).mp3
50
+ 2 : /Volumes/Storage/Music/Deadmau5 - It Sounds Like (MP3, 320bps) [2009]/01 Alone With You.mp3
51
+ 3 : /Volumes/Storage/Music/Deadmau5 - It Sounds Like (MP3, 320bps) [2009]/02 Arguru (EDX's 5un5hine Remix).mp3
52
+ 4 : /Volumes/Storage/Music/Deadmau5 - It Sounds Like (MP3, 320bps) [2009]/03 Bye Friend.mp3
53
+ 5 : /Volumes/Storage/Music/Deadmau5 - It Sounds Like (MP3, 320bps) [2009]/04 Clockwork.mp3
54
+
55
+
56
+
57
+ you can query based on certain filters such as artist,albums, title, genre and file.
58
+
59
+ $ dhun query --artist="Paul van Dyk" --genre=trance --file 'Paul' --title in
60
+
61
+ Querying: [nil] | artist:Paul van Dyk title:in genre:trance file:Paul
62
+ 3 Results
63
+ 0 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/08 - Paul van Dyk - Talk In Grey.mp3
64
+ 1 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/09 - Paul van Dyk - In Circles.mp3
65
+ 2 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/10 - Paul van Dyk - In Between.mp3
66
+
46
67
 
47
- You can use query filters like `album:sid` or `artist:rahman`. Currently
48
- `album`, `artist`, `title`, `genre` and `file` filters are supported.
68
+ YOu can mix filters with regular queries as well.
49
69
 
50
- $ dhun query genre:world album:gypsy
51
- 5 results
52
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 11 - Eastenders - Vino Iubirea Mea (!DelaDap Remix) (Germany).mp3
53
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 10 - Luminescent Orchestrii - Amari Szi, Amari (Amon Remix) (USA).mp3
54
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 09 - Kistehén Tánczenekar - Virágok a Réten (Romano Drom Remix) (Hungary).mp3
55
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 08 - Anselmo Crew - Süt Ictim Dilim Yandi (Hungary).mp3
56
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 07 - Magnifico & Turbolentza - Zh Ne Sui Pa Pur Tua (Slovenia).mp3
70
+ $ dhun query paul --title=haunted
57
71
 
58
- You can even mix the filters with a regular query like.
72
+ Querying: paul | title:haunted
73
+ 1 Results
74
+ 0 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/01 - Paul van Dyk - Haunted.mp3
59
75
 
60
- $ dhun query genre:world album:gypsy Czech
61
- 2 Results
62
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 01 - !DelaDap - Zsa Manca (Czech Republic-Hungary).mp3
63
- /Users/deepak/Dropbox/shared/music/gypsy/Putumayo - Gypsy Groove - 03 - Gipsy.cz - Jednou (Czech Republic).mp3
76
+ You can also pass in multiple regular expressions too. they just need to be seperated by commas (,)
77
+
78
+ $ dhun query paul,trance
79
+
80
+ Querying: paul,trance | 7 Results
81
+ 0 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/101-paul_van_dyk-volume__the_productions.mp3
82
+ 1 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/201-paul_van_dyk-volume__the_remixes_part_1.mp3
83
+ 2 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/301-paul_van_dyk-volume__the_remixes_part_2.mp3
84
+ 3 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/04 - Paul van Dyk - Complicated (Feat. Ashley Tomberlin).mp3
85
+ 4 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/01 - Paul van Dyk - Haunted.mp3
86
+ 5 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/02 - Paul van Dyk - White Lies (Feat. Jessica Sutta).mp3
87
+ 6 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/03 - Paul van Dyk - Sabotage.mp3
88
+
89
+
90
+ Now lets put it all together and go crazy.
91
+
92
+ $ dhun query 'paul van',dyk --genre=trance --title=haunted
93
+
94
+ Querying: paul van,dyk | title:haunted genre:trance
95
+ 1 Results
96
+ 0 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/01 - Paul van Dyk - Haunted.mp3
64
97
 
65
- Note that if you want to pass filters longer than a word, you will need to
66
- enclose the argument in double quotes, like `dhun query "artist:akli d"`
67
98
 
68
99
  ### Playing Files
69
100
 
70
- When you are ready to play the files, pass the query to the `play` command.
71
- Note that the `play` command will remove anything that may be already there on
72
- your queue. To append files to queue, use `enqueue`.
101
+ To play the files, first enqueue the songs.
102
+
103
+ $ dhun play paul,trance
73
104
 
74
- $ dhun play here
75
- 3 files queued for playing
76
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/01 40 Day Dream.mp3
77
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/02 Janglin.mp3
78
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/03 Carries On.mp3
105
+ this can also be done by
106
+
107
+ $ dhun enqueue paul,trance
108
+
109
+ Querying: paul,trance | 7 Results
110
+ 0 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/101-paul_van_dyk-volume__the_productions.mp3
111
+ 1 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/201-paul_van_dyk-volume__the_remixes_part_1.mp3
112
+ 2 : /Volumes/Storage/Music/Paul_Van_Dyk-Volume-3CD-2009-TSP/301-paul_van_dyk-volume__the_remixes_part_2.mp3
113
+ 3 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/04 - Paul van Dyk - Complicated (Feat. Ashley Tomberlin).mp3
114
+ 4 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/01 - Paul van Dyk - Haunted.mp3
115
+ 5 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/02 - Paul van Dyk - White Lies (Feat. Jessica Sutta).mp3
116
+ 6 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/03 - Paul van Dyk - Sabotage.mp3
117
+ Enter index to queue:
118
+
119
+ It will prompt you to enter the index of the songs you want queued.(numbers on the left side)
120
+ You can enter them separated by commas(1,2,3,4) or spaces(1 2 3 4) or a single song if you like.
121
+ If you leave the prompt blank and enter, it will queue ALL the resulting songs.
122
+
123
+ Enter index to queue 4 5
124
+ selected:
125
+ 0 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/01 - Paul van Dyk - Haunted.mp3
126
+ 1 : /Volumes/Storage/Music/Paul van Dyk - In Between (2007)/02 - Paul van Dyk - White Lies (Feat. Jessica Sutta).mp3
127
+ 2 files queued
128
+
129
+
130
+ Once queued, the songs will begin playing. you can continue to enqueue more songs via enqueue and play.
79
131
 
80
- Enqueuing more files.
132
+ ### Controlling Playback
133
+
81
134
 
82
- $ dhun enqueue chup
83
- 1 files queued for playing.
84
- /Users/deepak/Dropbox/shared/music/Coke Studio/Chup.mp3
135
+ Starting Playback(needs to have songs in queue)
136
+
137
+ $ dhun play
138
+ resuming playback
85
139
 
140
+ Stopping playback.
86
141
 
87
- ### Controlling Playback
142
+ $ dhun stop
143
+ Dhun has stopped
88
144
 
89
145
  Pausing playback.
90
146
 
@@ -126,29 +182,25 @@ Status
126
182
 
127
183
  $ dhun status
128
184
  Dhun is running
129
- Now playing /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/03 Carries On.mp3
130
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/02 Janglin.mp3
131
- /Users/deepak/Music/iTunes/iTunes Media/Music/Edward Sharpe & The Magnetic Zeros/Here Comes/03 Carries On.mp3
132
- /Users/deepak/Music/Amazon MP3/Edward Sharpe & The Magnetic Zeros/Here Comes/01 - 40 Day Dream.mp3
133
- /Users/deepak/Music/Amazon MP3/Edward Sharpe & The Magnetic Zeros/Here Comes/02 - Janglin.mp3
134
- /Users/deepak/Music/Amazon MP3/Edward Sharpe & The Magnetic Zeros/Here Comes/03 - Carries On.mp3
135
- /Users/deepak/Dropbox/shared/music/Here Comes/02 - Janglin.mp3
136
- /Users/deepak/Dropbox/shared/music/Here Comes/01 - 40 Day Dream.mp3
137
- /Users/deepak/Dropbox/shared/music/Here Comes/03 - Carries On.mp3
138
-
185
+ Currently Playing:
186
+ /Volumes/Storage/Music/Hydeout Productions (Second Collection)/02 Sky is Falling (feat. C.L. Smooth).mp3
187
+ Queue:
188
+ 0 : /Volumes/Storage/Music/Hydeout Productions (Second Collection)/04 Imaginary Folklore.mp3
189
+ 1 : /Volumes/Storage/Music/Hydeout Productions (Second Collection)/05 Hikari(feat. Substantial).mp3
190
+
139
191
  History
140
192
 
141
193
  $ dhun history
142
- 3 files in history
143
- /Users/deepak/Dropbox/shared/music/Coke Studio/Bari-Barsi.mp3
144
- /Users/deepak/Dropbox/shared/music/Coke Studio/Aankhon-Kay-Sagar.mp3
145
- /Users/deepak/Dropbox/shared/music/Coke Studio/Paimona.mp3
194
+ 1 files in history
195
+ History:
196
+ 0 : /Volumes/Storage/Music/Hydeout Productions (Second Collection)/04 Imaginary Folklore.mp3
197
+
146
198
 
147
199
  ### Stopping Dhun
148
200
 
149
- This will exit the process.
201
+ This will exit the dhun server.
150
202
 
151
- $ dhun stop
203
+ $ dhun stop_server
152
204
 
153
205
  ## Coming Soon
154
206
 
data/Rakefile CHANGED
@@ -81,3 +81,15 @@ end
81
81
  desc 'Build the dhun extension'
82
82
  task :build => "lib/dhun_ext.#{DLEXT}"
83
83
 
84
+ # ==========================================================
85
+ # Riot Testing
86
+ # ==========================================================
87
+
88
+ desc 'Default task: run all tests'
89
+ task :default => [:test]
90
+ require 'rake/testtask'
91
+ Rake::TestTask.new(:test) do |test|
92
+ test.libs << 'lib' << 'test'
93
+ test.pattern = 'test/**/*_test.rb'
94
+ test.verbose = false
95
+ end
data/TODO.md CHANGED
@@ -1,17 +1,8 @@
1
1
  TODO
2
2
  ====
3
3
 
4
- * Daemonizing/Logging of Server
5
-
6
- * Better querying mechanism using a treetop parser
7
-
8
- * Prev, by keeping a history
9
-
10
4
  * Display info from idv3 tags
11
5
 
12
- * Advanced Querying Support
13
-
14
6
  * iTunes support
15
7
 
16
- * Pause and Skip functionality
17
8
 
data/bin/dhun CHANGED
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  # Thin command line interface script.
3
3
  # Run <tt>dhun -h</tt> to get more usage.
4
+ require 'rubygems'
4
5
  $: << File.expand_path(File.dirname(__FILE__) + "/../lib")
5
6
  require 'dhun'
6
- Dhun::Runner.new(ARGV).run!
7
+ Dhun::Runner.start(ARGV)
7
8
 
data/dhun.gemspec CHANGED
@@ -1,17 +1,21 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'dhun'
3
- s.version = '0.5.6'
3
+ s.version = '0.6.0'
4
4
  s.summary = "Minimalist music player for OS X"
5
- s.date = '2009-12-18'
5
+ s.date = '2010-01-12'
6
6
  s.email = 'deepak.jois@gmail.com'
7
7
  s.homepage = 'http://github.com/deepakjois/dhun'
8
8
  s.has_rdoc = false
9
+ s.add_development_dependency('riot', '>=0.10.11')
10
+ s.add_development_dependency('rr', '>=0.10.5')
9
11
  s.add_dependency('eventmachine', '>=0.12.10')
10
12
  s.add_dependency('json_pure', '>=1.2.0')
11
13
  s.add_dependency('daemons', '>=1.0.10')
14
+ s.add_dependency('thor', '>=0.12.0')
12
15
  s.authors = ["Deepak Jois"]
13
16
  # = MANIFEST =
14
17
  s.files = %w[
18
+ CONTRIBUTORS
15
19
  FIX.md
16
20
  LICENSE.txt
17
21
  README.md
@@ -26,9 +30,7 @@ Gem::Specification.new do |s|
26
30
  ext/player.c
27
31
  ext/query.c
28
32
  lib/dhun.rb
29
- lib/dhun/command.rb
30
- lib/dhun/controller.rb
31
- lib/dhun/dhun_client.rb
33
+ lib/dhun/client.rb
32
34
  lib/dhun/dhun_server.rb
33
35
  lib/dhun/handler.rb
34
36
  lib/dhun/logger.rb
data/lib/dhun.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  module Dhun
2
- VERSION = '0.5.6'
2
+ VERSION = '0.6.0'
3
3
 
4
4
  autoload :Runner, 'dhun/runner'
5
- autoload :Controller, 'dhun/controller'
6
5
  autoload :Server, 'dhun/server'
7
6
  autoload :DhunServer, 'dhun/dhun_server'
8
- autoload :DhunClient, 'dhun/dhun_client'
7
+ autoload :Client, 'dhun/client'
9
8
  autoload :Handler, 'dhun/handler'
10
9
  autoload :Player, 'dhun/player'
11
10
  autoload :Query, 'dhun/query'
@@ -0,0 +1,25 @@
1
+ require 'socket'
2
+ require 'json'
3
+ module Dhun
4
+ module Client
5
+
6
+ def send_message(message,socket)
7
+ response = nil
8
+ UNIXSocket.open(socket) do |unix|
9
+ unix.puts message
10
+ response = unix.read
11
+ end
12
+ response
13
+ end
14
+
15
+ def is_server?(socket)
16
+ begin
17
+ UNIXSocket.open(socket) { |socket| }
18
+ return true
19
+ rescue StandardError
20
+ return false
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -1,41 +1,50 @@
1
+ require 'rubygems'
2
+ require 'eventmachine'
1
3
  require 'json'
4
+ %w[handler logger player query result].each {|lib| require File.dirname(__FILE__) + "/#{lib}"}
2
5
  module Dhun
3
6
  # Handler for commands sent from client
4
7
  module DhunServer
8
+ @@logger = Dhun::Logger.instance
9
+
5
10
  def post_init
6
11
  #puts "-- client connected"
12
+ @data = ""
7
13
  end
8
14
 
9
- def receive_data data
10
- @logger ||= Logger.instance
15
+ def receive_data(data)
11
16
  begin
12
- @logger.debug data
13
- cmd = JSON.parse(data)
14
- @command = cmd["command"]
15
- @arguments = cmd["arguments"]
16
- handle_client_request
17
+ @@logger.debug data
18
+ @data << data
19
+ # puts data.inspect.to_s
20
+ cmd = begin
21
+ JSON.parse(@data)
22
+ rescue
23
+ @@logger.log $!
24
+ nil
25
+ end
26
+ handle_client_request cmd["command"], cmd["arguments"] if cmd
17
27
  rescue StandardError => ex
18
- @logger.log "Error parsing command : #{ex.message}"
19
- @logger.log ex.backtrace
20
- ensure
28
+ @@logger.log ex.message
29
+ @@logger.log ex.backtrace
30
+ send_data Dhun::Result.new(:error,ex.message).to_json
21
31
  close_connection true
22
32
  end
23
33
  end
24
34
 
25
- def handle_client_request
26
- @logger ||= Logger.instance
27
- handler = Handler.new
28
- begin
29
- if @command.nil?
30
- raise "Command Not Found"
31
- end
32
- result = handler.send(@command,*@arguments)
33
- @logger.debug "Sending #{result}"
34
- send_data result
35
- rescue StandardError => ex
36
- @logger.log "-- error : #{ex.message}"
37
- @logger.log ex.backtrace
35
+ def handle_client_request(command,arguments)
36
+ handler = Dhun::Handler.new
37
+ raise "No Command!" if command.nil?
38
+ result =
39
+ if arguments and arguments != [] # this check is on the sloppy side...
40
+ handler.send command, arguments
41
+ else
42
+ handler.send command
38
43
  end
44
+ @@logger.debug "Sending #{result}"
45
+ puts (result ? result.inspect.to_s : 'nil')
46
+ send_data Dhun::Result.new(*result).to_json
47
+ close_connection true
39
48
  end
40
49
 
41
50
  def unbind
@@ -43,3 +52,27 @@ module Dhun
43
52
  end
44
53
  end
45
54
  end
55
+
56
+ def remove_socket_file(socket)
57
+ File.delete(socket) if File.exist?(socket)
58
+ end
59
+
60
+ def stop!
61
+ Dhun::Player.instance.stop
62
+ Dhun::Logger.instance.log "Stopping Dhun"
63
+ EventMachine.stop if EventMachine.reactor_running?
64
+ exit
65
+ end
66
+
67
+ if ARGV
68
+ Dhun::Logger.instance.file = ARGV[1]
69
+ Dhun::Logger.instance.log "Starting Dhun"
70
+ at_exit { remove_socket_file(ARGV[0]); stop! }
71
+ EventMachine::run do
72
+ puts "Dhun Server at: #{ARGV[0]}"
73
+ trap('QUIT') { stop! }
74
+ trap('INT') { stop! }
75
+ trap('TERM') { stop! }
76
+ EventMachine::start_server ARGV[0], Dhun::DhunServer
77
+ end
78
+ end
data/lib/dhun/handler.rb CHANGED
@@ -2,118 +2,97 @@ require 'json'
2
2
  module Dhun
3
3
  # Handling commands sent by Dhun client
4
4
  class Handler
5
- def stop
6
- result = Result.new :success, "Dhun is stopping"
7
- Server.stop
8
- Player.instance.stop
9
- return result.to_json
5
+ attr_reader :player
6
+
7
+ def initialize
8
+ @player = Dhun::Player.instance
10
9
  end
11
-
12
- def play(*args)
13
- @player = Player.instance
14
- q = Query.new(args)
15
- if q.is_valid?
16
- files = q.execute_spotlight_query
17
- if files.empty?
18
- result = Result.new :error, "No Results Found"
19
- else
20
- @player.play_files files
21
- result = Result.new :success, "#{files.size} files queued for playing",
22
- :files => files
23
- end
24
- else
25
- result = Result.new :error, "Invalid query syntax. See dhun -h for correct syntax"
10
+
11
+ def play
12
+ response =
13
+ case @player.play
14
+ when false then [:error,"already playing"]
15
+ when true then [:success, "resuming playback"]
16
+ when :empty then [:error, 'no file in queue']
26
17
  end
27
- result.to_json
18
+ response
28
19
  end
29
20
 
30
- def enqueue(*args)
31
- @player = Player.instance
32
- q = Query.new(args.join(" "))
33
- if q.is_valid?
34
- files = q.execute_spotlight_query
35
- if files.empty?
36
- result = Result.new :error, "No Results Found"
37
- else
38
- @player.enqueue files
39
- result = Result.new :success, "#{files.size} files queued for playing.",
40
- :files => files
41
- end
42
- else
43
- result = Result.new :error, "Invalid query syntax. See dhun -h for correct syntax"
44
- end
45
- result.to_json
21
+ def enqueue(files)
22
+ perform_action :enqueue, files,
23
+ :success => ["#{files.size} files queued", {:files => files}],
24
+ :error => ["No files queued"]
46
25
  end
47
26
 
48
- def status
49
- @player = Player.instance
50
- status_msg = case @player.status
51
- when :playing then "Dhun is running"
52
- when :paused then "Dhun is paused"
53
- when :stopped then "Dhun has stopped"
54
- end
55
- now_playing = @player.current
56
- queue = @player.queue
57
- result = Result.new :success, status_msg, :now_playing => now_playing, :queue => queue
58
- result.to_json
27
+ def pause
28
+ perform_action :pause,nil,
29
+ :success => "Dhun is paused at #{@player.current}",
30
+ :error => "Dhun has already paused or stopped"
59
31
  end
60
32
 
61
- def history
62
- @player = Player.instance
63
- status_msg = @player.history.empty? ? "No files in history" : "#{@player.history.size} files in history"
64
- result = Result.new :success, status_msg, :history => @player.history
65
- result.to_json
33
+ def resume
34
+ perform_action :resume,nil,
35
+ :success => "Dhun is resumed at #{@player.current}",
36
+ :error => "Dhun has already resumed or stopped"
37
+ end
38
+
39
+ def stop
40
+ perform_action :stop,nil,
41
+ :success => "Dhun has stopped",
42
+ :error => "Dhun has already stopped"
66
43
  end
67
44
 
68
45
  def next(skip_length=1)
69
- @player = Player.instance
70
- next_track = @player.next skip_length
71
- msg = next_track ? "Dhun is playing #{next_track}" : "Not enough tracks in queue"
72
- result = Result.new :success, msg
73
- return result.to_json
46
+ next_prev :next, 'queue',skip_length
74
47
  end
75
48
 
76
49
  def prev(skip_length=1)
77
- @player = Player.instance
78
- prev_track = @player.prev skip_length
79
- msg = prev_track ? "Dhun is playing #{prev_track}" : "Not enough tracks in history"
80
- result = Result.new :success, msg
81
- return result.to_json
50
+ next_prev :prev, 'history',skip_length
51
+ end
52
+
53
+ def shuffle
54
+ perform_action :shuffle,nil,
55
+ :success => ['Queue is shuffled', { :queue => @player.queue }],
56
+ :error => 'Dhun cannot shuffle(queue empty, same songs)'
82
57
  end
83
58
 
84
- def pause
85
- @player = Player.instance
86
- @player.pause
87
- case @player.status
88
- when :paused
89
- result = Result.new :success, "Dhun is paused at #{@player.current}"
90
- when :stopped
91
- result = Result.new :error, "Dhun has already stopped"
92
- end
93
- return result.to_json
59
+ def clear
60
+ perform_action :clear,nil,
61
+ :success => "Queue is cleared",
62
+ :error => "Queue is not cleared"
94
63
  end
95
64
 
96
- def resume
97
- @player = Player.instance
98
- @player.resume
65
+
66
+ def status
67
+ status_msg =
99
68
  case @player.status
100
- when :playing
101
- result = Result.new :success, "Dhun is playing #{@player.current}"
102
- when :stopped
103
- result = Result.new :error, "Dhun has already stopped"
69
+ when :playing then "Dhun is running"
70
+ when :paused then "Dhun is paused"
71
+ when :stopped then "Dhun has stopped"
104
72
  end
105
- return result.to_json
73
+ [:success, status_msg, {:current => @player.current, :queue => @player.queue}]
106
74
  end
107
75
 
108
- def shuffle
109
- @player = Player.instance
110
- @player.shuffle
111
- if @player.queue.empty?
112
- result = Result.new :error, "Queue is empty"
113
- else
114
- result = Result.new :success, "Queue is shuffled", :queue => @player.queue
115
- end
116
- return result.to_json
76
+ def history
77
+ return [:success, "#{@player.history.size} files in history",{:history => @player.history}] unless @player.history.empty?
78
+ return [:error, "No files in history"]
79
+ end
80
+
81
+ private
82
+
83
+ # get response
84
+ def perform_action(action, arg=nil, message={})
85
+ result = arg.nil? ? @player.send(action) : @player.send(action,arg)
86
+ return [:error,message[:error]].flatten unless result
87
+ return [:success,message[:success]].flatten
117
88
  end
89
+
90
+ #next and previous method
91
+ def next_prev(action,message,skip_length)
92
+ track = @player.send(action,skip_length)
93
+ return [:success, "Dhun is playing #{track}"] if track
94
+ return [:error, "Not enough tracks in #{message}"]
95
+ end
96
+
118
97
  end
119
98
  end