itunes-observer 0.0.3 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -19
- data/examples/love.rb +2 -2
- data/lib/itunes_observer.rb +64 -11
- data/spec/itunes_observer_spec.rb +105 -11
- metadata +26 -10
data/Rakefile
CHANGED
@@ -4,9 +4,6 @@ require 'rake/clean'
|
|
4
4
|
require 'rake/testtask'
|
5
5
|
require 'rake/packagetask'
|
6
6
|
require 'rake/gempackagetask'
|
7
|
-
require 'rake/rdoctask'
|
8
|
-
require 'rake/contrib/rubyforgepublisher'
|
9
|
-
require 'rake/contrib/sshpublisher'
|
10
7
|
require 'spec/rake/spectask'
|
11
8
|
require 'fileutils'
|
12
9
|
include FileUtils
|
@@ -40,7 +37,6 @@ task :package => [:clean]
|
|
40
37
|
Spec::Rake::SpecTask.new do |t|
|
41
38
|
t.spec_opts = ['--options', "spec/spec.opts"]
|
42
39
|
t.spec_files = FileList['spec/*_spec.rb']
|
43
|
-
t.rcov = true
|
44
40
|
end
|
45
41
|
|
46
42
|
spec = Gem::Specification.new do |s|
|
@@ -63,6 +59,7 @@ spec = Gem::Specification.new do |s|
|
|
63
59
|
s.test_files = Dir["test/test_*.rb"]
|
64
60
|
|
65
61
|
#s.add_dependency('')
|
62
|
+
s.add_development_dependency('eventmachine')
|
66
63
|
#s.required_ruby_version = '>= 1.8.2'
|
67
64
|
|
68
65
|
s.files = %w(README.rdoc ChangeLog Rakefile) +
|
@@ -89,21 +86,6 @@ task :uninstall => [:clean] do
|
|
89
86
|
sh %{sudo gem uninstall #{NAME}}
|
90
87
|
end
|
91
88
|
|
92
|
-
|
93
|
-
Rake::RDocTask.new do |rdoc|
|
94
|
-
rdoc.rdoc_dir = 'html'
|
95
|
-
rdoc.options += RDOC_OPTS
|
96
|
-
rdoc.template = "resh"
|
97
|
-
#rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
98
|
-
if ENV['DOC_FILES']
|
99
|
-
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
|
100
|
-
else
|
101
|
-
rdoc.rdoc_files.include('README.rdoc', 'ChangeLog')
|
102
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
103
|
-
rdoc.rdoc_files.include('ext/**/*.c')
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
89
|
desc "Show information about the gem"
|
108
90
|
task :debug_gem do
|
109
91
|
puts spec.to_ruby
|
data/examples/love.rb
CHANGED
@@ -23,14 +23,14 @@ def main
|
|
23
23
|
unless session = config['session']
|
24
24
|
token = lastfm.auth.get_token
|
25
25
|
auth(config['api_key'], token)
|
26
|
-
session = lastfm.auth.get_session(token)
|
26
|
+
session = lastfm.auth.get_session(token)['key']
|
27
27
|
Pit.set('last.fm', :data => {
|
28
28
|
"session" => session
|
29
29
|
}.merge(config))
|
30
30
|
|
31
31
|
puts "Session key was generated."
|
32
32
|
|
33
|
-
|
33
|
+
exit
|
34
34
|
end
|
35
35
|
|
36
36
|
lastfm.session = session
|
data/lib/itunes_observer.rb
CHANGED
@@ -1,11 +1,30 @@
|
|
1
1
|
require 'osx/cocoa'
|
2
2
|
|
3
3
|
class ITunesObserver
|
4
|
-
VERSION = '0.
|
4
|
+
VERSION = '0.1.1'
|
5
|
+
|
6
|
+
STATES = {
|
7
|
+
:playing => 'Playing',
|
8
|
+
:paused => 'Paused',
|
9
|
+
:stopped => 'Stopped'
|
10
|
+
}
|
5
11
|
|
6
12
|
def initialize(&callback)
|
7
|
-
observer = Observer.alloc.init
|
8
|
-
|
13
|
+
@observer = Observer.alloc.init
|
14
|
+
|
15
|
+
add_callback(STATES[:playing], &callback)
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_play(&callback)
|
19
|
+
add_callback(STATES[:playing], &callback)
|
20
|
+
end
|
21
|
+
|
22
|
+
def on_pause(&callback)
|
23
|
+
add_callback(STATES[:paused], &callback)
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_stop(&callback)
|
27
|
+
add_callback(STATES[:stopped], &callback)
|
9
28
|
end
|
10
29
|
|
11
30
|
def run(stop_after = nil)
|
@@ -16,23 +35,55 @@ class ITunesObserver
|
|
16
35
|
end
|
17
36
|
end
|
18
37
|
|
38
|
+
def finish
|
39
|
+
@observer.finish
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def add_callback(state, &callback)
|
45
|
+
if callback
|
46
|
+
@observer.add_callback(state, &callback)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
19
50
|
# based on http://blog.8-p.info/articles/2006/12/24/rubycocoa-skype-itunes
|
20
51
|
class Observer < OSX::NSObject
|
52
|
+
def initialize
|
53
|
+
@callbacks = {}
|
54
|
+
|
55
|
+
notification_centor.addObserver_selector_name_object_(self,
|
56
|
+
'onPlayerInfo:',
|
57
|
+
'com.apple.iTunes.playerInfo',
|
58
|
+
'com.apple.iTunes.player')
|
59
|
+
end
|
60
|
+
|
21
61
|
def onPlayerInfo(info)
|
22
|
-
|
23
|
-
|
24
|
-
|
62
|
+
result = Result.new(info.userInfo)
|
63
|
+
|
64
|
+
STATES.each do |k, state|
|
65
|
+
if info.userInfo['Player State'] == state
|
66
|
+
(@callbacks[state] || []).each do |callback|
|
67
|
+
callback.call(result)
|
68
|
+
end
|
69
|
+
end
|
25
70
|
end
|
26
71
|
end
|
27
72
|
|
28
|
-
def
|
29
|
-
@
|
30
|
-
|
31
|
-
|
32
|
-
|
73
|
+
def add_callback(state, &callback)
|
74
|
+
@callbacks[state] ||= []
|
75
|
+
@callbacks[state] << callback
|
76
|
+
end
|
77
|
+
|
78
|
+
def finish
|
79
|
+
notification_centor.removeObserver_name_object_(self,
|
33
80
|
'com.apple.iTunes.playerInfo',
|
34
81
|
'com.apple.iTunes.player')
|
35
82
|
end
|
83
|
+
|
84
|
+
def notification_centor
|
85
|
+
OSX::NSDistributedNotificationCenter.defaultCenter
|
86
|
+
end
|
36
87
|
end
|
37
88
|
|
38
89
|
class Result
|
@@ -46,6 +97,8 @@ class ITunesObserver
|
|
46
97
|
value.to_s
|
47
98
|
when OSX::NSCFString
|
48
99
|
value.to_s
|
100
|
+
when OSX::NSNumber
|
101
|
+
value.to_i
|
49
102
|
else
|
50
103
|
value
|
51
104
|
end
|
@@ -1,7 +1,9 @@
|
|
1
1
|
$:.unshift File.dirname(__FILE__)
|
2
2
|
|
3
|
+
require 'rubygems'
|
3
4
|
require 'spec_helper'
|
4
5
|
require 'osx/cocoa'
|
6
|
+
require 'eventmachine'
|
5
7
|
|
6
8
|
include OSX
|
7
9
|
OSX.require_framework 'ScriptingBridge'
|
@@ -10,11 +12,6 @@ describe ITunesObserver do
|
|
10
12
|
before do
|
11
13
|
@itunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
|
12
14
|
raise 'iTunes must be running' unless @itunes.isRunning
|
13
|
-
@result = nil
|
14
|
-
@observer = ITunesObserver.new do |result|
|
15
|
-
@result = result
|
16
|
-
end
|
17
|
-
|
18
15
|
@itunes.stop
|
19
16
|
end
|
20
17
|
|
@@ -22,11 +19,108 @@ describe ITunesObserver do
|
|
22
19
|
@itunes.stop
|
23
20
|
end
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
describe ".new" do
|
23
|
+
it "should observe playing" do
|
24
|
+
result = nil
|
25
|
+
|
26
|
+
observer = ITunesObserver.new do |result|
|
27
|
+
result = result
|
28
|
+
end
|
29
|
+
|
30
|
+
run_loop(observer) do
|
31
|
+
@itunes.playpause
|
32
|
+
end
|
33
|
+
|
34
|
+
result.should_not be_nil
|
35
|
+
result['Name'].should_not be_nil
|
36
|
+
result['Player State'].should eql('Playing')
|
37
|
+
result['Total Time'].should be_kind_of(Fixnum)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#on_stop" do
|
42
|
+
it "should observe stopping" do
|
43
|
+
observer = ITunesObserver.new
|
44
|
+
result = nil
|
45
|
+
|
46
|
+
observer.on_stop do |result|
|
47
|
+
result = result
|
48
|
+
end
|
49
|
+
|
50
|
+
run_loop(observer) do
|
51
|
+
@itunes.playpause
|
52
|
+
@itunes.stop
|
53
|
+
end
|
54
|
+
|
55
|
+
observer.finish
|
56
|
+
|
57
|
+
result.should_not be_nil
|
58
|
+
result['Name'].should_not be_nil
|
59
|
+
result['Player State'].should eql('Stopped')
|
60
|
+
result['Total Time'].should be_kind_of(Fixnum)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#on_play" do
|
65
|
+
it "should observe playing" do
|
66
|
+
observer = ITunesObserver.new
|
67
|
+
result = nil
|
68
|
+
|
69
|
+
observer.on_play do |result|
|
70
|
+
result = result
|
71
|
+
end
|
72
|
+
|
73
|
+
run_loop(observer) do
|
74
|
+
@itunes.playpause
|
75
|
+
end
|
76
|
+
|
77
|
+
result.should_not be_nil
|
78
|
+
result['Name'].should_not be_nil
|
79
|
+
result['Player State'].should eql('Playing')
|
80
|
+
result['Total Time'].should be_kind_of(Fixnum)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#on_pause" do
|
85
|
+
it "should observe pausing" do
|
86
|
+
observer = ITunesObserver.new
|
87
|
+
result = nil
|
88
|
+
|
89
|
+
observer.on_pause do |result|
|
90
|
+
result = result
|
91
|
+
end
|
92
|
+
|
93
|
+
run_loop(observer) do
|
94
|
+
@itunes.playpause
|
95
|
+
|
96
|
+
EM::Timer.new(1) do
|
97
|
+
@itunes.pause
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
observer.run(0)
|
102
|
+
observer.finish
|
103
|
+
|
104
|
+
result.should_not be_nil
|
105
|
+
result['Name'].should_not be_nil
|
106
|
+
result['Player State'].should eql('Paused')
|
107
|
+
result['Total Time'].should be_kind_of(Fixnum)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def run_loop(observer)
|
112
|
+
EM.run do
|
113
|
+
yield
|
114
|
+
|
115
|
+
EM::Timer.new(3) do
|
116
|
+
EM.stop
|
117
|
+
end
|
118
|
+
|
119
|
+
EM::add_periodic_timer(1) do
|
120
|
+
observer.run(0)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
ensure
|
124
|
+
observer.finish
|
31
125
|
end
|
32
126
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itunes-observer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- youpy
|
@@ -14,10 +15,22 @@ autorequire: ""
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
date: 2012-03-20 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: eventmachine
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
21
34
|
description: ""
|
22
35
|
email: youpy@buycheapviagraonlinenow.com
|
23
36
|
executables: []
|
@@ -36,7 +49,6 @@ files:
|
|
36
49
|
- spec/spec_helper.rb
|
37
50
|
- lib/itunes_observer.rb
|
38
51
|
- examples/love.rb
|
39
|
-
has_rdoc: true
|
40
52
|
homepage: http://itunes-observer.rubyforge.org
|
41
53
|
licenses: []
|
42
54
|
|
@@ -57,23 +69,27 @@ rdoc_options:
|
|
57
69
|
require_paths:
|
58
70
|
- lib
|
59
71
|
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
60
73
|
requirements:
|
61
74
|
- - ">="
|
62
75
|
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
63
77
|
segments:
|
64
78
|
- 0
|
65
79
|
version: "0"
|
66
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
67
82
|
requirements:
|
68
83
|
- - ">="
|
69
84
|
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
70
86
|
segments:
|
71
87
|
- 0
|
72
88
|
version: "0"
|
73
89
|
requirements: []
|
74
90
|
|
75
91
|
rubyforge_project: itunes-observer
|
76
|
-
rubygems_version: 1.
|
92
|
+
rubygems_version: 1.8.17
|
77
93
|
signing_key:
|
78
94
|
specification_version: 3
|
79
95
|
summary: ""
|