eventmachine-distributed-notification 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'eventmachine'
4
+
5
+ group :development do
6
+ gem 'rspec', '~> 2.8.0'
7
+ gem 'rdoc', '~> 3.12'
8
+ gem 'bundler', '~> 1.1.0'
9
+ gem 'jeweler', '~> 1.8.3'
10
+ gem 'rb-appscript', :require => 'appscript'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ eventmachine (0.12.10)
6
+ git (1.2.5)
7
+ jeweler (1.8.3)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rdoc
12
+ json (1.6.5)
13
+ rake (0.9.2.2)
14
+ rb-appscript (0.6.1)
15
+ rdoc (3.12)
16
+ json (~> 1.4)
17
+ rspec (2.8.0)
18
+ rspec-core (~> 2.8.0)
19
+ rspec-expectations (~> 2.8.0)
20
+ rspec-mocks (~> 2.8.0)
21
+ rspec-core (2.8.0)
22
+ rspec-expectations (2.8.0)
23
+ diff-lcs (~> 1.1.2)
24
+ rspec-mocks (2.8.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler (~> 1.1.0)
31
+ eventmachine
32
+ jeweler (~> 1.8.3)
33
+ rb-appscript
34
+ rdoc (~> 3.12)
35
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 youpy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,37 @@
1
+ = eventmachine-distributed-notification
2
+
3
+ An {EventMachine}[http://wiki.github.com/eventmachine/eventmachine/] extension to watch OSX's Distributed Notification
4
+
5
+ == Synopsis
6
+
7
+ require 'eventmachine-distributed-notification'
8
+
9
+ === Watch Notification
10
+
11
+ class Watcher < EM::DistributedNotificationWatch
12
+ attr_accessor :value
13
+
14
+ def notify(name, user_info)
15
+ puts 'now playing %s' % [user_info['Artist'], user_info['Name']].joint(' - ')
16
+ end
17
+ end
18
+
19
+ EM.run {
20
+ EM.watch_distributed_notification('com.apple.iTunes.playerInfo', Watcher)
21
+ }
22
+
23
+ == Contributing to eventmachine-distributed-notification
24
+
25
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
26
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
27
+ * Fork the project.
28
+ * Start a feature/bugfix branch.
29
+ * Commit and push until you are happy with your contribution.
30
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
31
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
32
+
33
+ == Copyright
34
+
35
+ Copyright (c) 2012 youpy. See LICENSE.txt for
36
+ further details.
37
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ tasks = Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "eventmachine-distributed-notification"
18
+ gem.homepage = "http://github.com/youpy/eventmachine-distributed-notification"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{An EventMachine extension to watch OSX's Distributed Notification}
21
+ gem.description = %Q{An EventMachine extension to watch OSX's Distributed Notification, posted by iTunes etc.}
22
+ gem.email = "youpy@buycheapviagraonlinenow.com"
23
+ gem.authors = ["youpy"]
24
+ gem.extensions = FileList["ext/**/extconf.rb"]
25
+ end
26
+
27
+ # rule to build the extension: this says
28
+ # that the extension should be rebuilt
29
+ # after any change to the files in ext
30
+ ext_name = 'observer_native'
31
+ file "lib/#{ext_name}.bundle" =>
32
+ Dir.glob("ext/#{ext_name}/*{.rb,.m}") do
33
+ Dir.chdir("ext/#{ext_name}") do
34
+ # this does essentially the same thing
35
+ # as what RubyGems does
36
+ ruby "extconf.rb"
37
+ sh "make"
38
+ end
39
+ cp "ext/#{ext_name}/#{ext_name}.bundle", "lib/"
40
+ end
41
+
42
+ task :spec => "lib/#{ext_name}.bundle"
43
+
44
+ Jeweler::RubygemsDotOrgTasks.new
45
+
46
+ require 'rspec/core'
47
+ require 'rspec/core/rake_task'
48
+ RSpec::Core::RakeTask.new(:spec) do |spec|
49
+ spec.pattern = FileList['spec/**/*_spec.rb']
50
+ end
51
+
52
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,57 @@
1
+ /* contains basic macros to facilitate ruby 1.8 and ruby 1.9 compatibility */
2
+
3
+ #ifndef GUARD_COMPAT_H
4
+ #define GUARD_COMPAT_H
5
+
6
+ #include <ruby.h>
7
+
8
+ /* test for 1.9 */
9
+ #if !defined(RUBY_19) && defined(ROBJECT_EMBED_LEN_MAX)
10
+ # define RUBY_19
11
+ #endif
12
+
13
+ /* macros for backwards compatibility with 1.8 */
14
+ #ifndef RUBY_19
15
+ # define RCLASS_M_TBL(c) (RCLASS(c)->m_tbl)
16
+ # define RCLASS_SUPER(c) (RCLASS(c)->super)
17
+ # define RCLASS_IV_TBL(c) (RCLASS(c)->iv_tbl)
18
+ # define OBJ_UNTRUSTED OBJ_TAINTED
19
+ # include "st.h"
20
+ #endif
21
+
22
+ #ifdef RUBY_19
23
+ inline static VALUE
24
+ class_alloc(VALUE flags, VALUE klass)
25
+ {
26
+ rb_classext_t *ext = ALLOC(rb_classext_t);
27
+ NEWOBJ(obj, struct RClass);
28
+ OBJSETUP(obj, klass, flags);
29
+ obj->ptr = ext;
30
+ RCLASS_IV_TBL(obj) = 0;
31
+ RCLASS_M_TBL(obj) = 0;
32
+ RCLASS_SUPER(obj) = 0;
33
+ RCLASS_IV_INDEX_TBL(obj) = 0;
34
+ return (VALUE)obj;
35
+ }
36
+ #endif
37
+
38
+ inline static VALUE
39
+ create_class(VALUE flags, VALUE klass)
40
+ {
41
+ #ifdef RUBY_19
42
+ VALUE new_klass = class_alloc(flags, klass);
43
+ #else
44
+ NEWOBJ(new_klass, struct RClass);
45
+ OBJSETUP(new_klass, klass, flags);
46
+ #endif
47
+
48
+ return (VALUE)new_klass;
49
+ }
50
+
51
+ # define FALSE 0
52
+ # define TRUE 1
53
+
54
+ /* a useful macro. cannot use ordinary CLASS_OF as it does not return an lvalue */
55
+ #define KLASS_OF(c) (RBASIC(c)->klass)
56
+
57
+ #endif
@@ -0,0 +1,14 @@
1
+ require "mkmf"
2
+
3
+ # Name your extension
4
+ extension_name = 'observer_native'
5
+
6
+ # Set your target name
7
+ dir_config(extension_name)
8
+
9
+ $LDFLAGS += ' -framework CoreFoundation -framework CoreServices'
10
+
11
+ have_header(extension_name)
12
+
13
+ # Do the work
14
+ create_makefile(extension_name)
@@ -0,0 +1,15 @@
1
+ #include <ruby.h>
2
+ #import <Foundation/Foundation.h>
3
+
4
+ @interface Observer : NSObject
5
+ {
6
+ }
7
+
8
+ @property (assign) NSString *name;
9
+ @property (assign) VALUE handler;
10
+
11
+ -(void)observe;
12
+
13
+ @end
14
+
15
+ VALUE createInstanceFromObserver(Observer *obs);
@@ -0,0 +1,130 @@
1
+ #import "observer_native.h"
2
+
3
+ @interface Observer (Private)
4
+ - (void) p_iTunesPlayStatusChanged:(NSNotification *)theNotification;
5
+ @end
6
+
7
+ @implementation Observer
8
+
9
+ @synthesize name;
10
+ @synthesize handler;
11
+
12
+ - (void)dealloc
13
+ {
14
+ [[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
15
+ [super dealloc];
16
+ }
17
+
18
+ - (void)observe
19
+ {
20
+ [[NSDistributedNotificationCenter defaultCenter]
21
+ addObserver:self
22
+ selector:@selector(p_iTunesPlayStatusChanged:)
23
+ name:(name != nil) ? name : nil
24
+ object:nil
25
+ ];
26
+ }
27
+
28
+ - (void)p_iTunesPlayStatusChanged:(NSNotification *)theNotification
29
+ {
30
+ VALUE aName = rb_str_new2([[theNotification name] UTF8String]);
31
+ NSDictionary *userInfo = [theNotification userInfo];
32
+ VALUE hash = rb_hash_new();
33
+
34
+ for(id key in userInfo) {
35
+ rb_hash_aset(hash, rb_str_new2([[key description] UTF8String]), rb_str_new2([[[userInfo objectForKey:key] description] UTF8String]));
36
+ }
37
+
38
+ //const char *description = [[[theNotification userInfo] description] UTF8String];
39
+
40
+ rb_funcall(handler, rb_intern("notify"), 2, aName, hash);
41
+ }
42
+
43
+ @end
44
+
45
+ static VALUE rb_cObserverNative;
46
+
47
+ struct ObserverObject {
48
+ Observer *obs;
49
+ };
50
+
51
+ Observer *getObserver(VALUE obj) {
52
+ struct ObserverObject *observerObject;
53
+
54
+ Data_Get_Struct(obj, struct ObserverObject, observerObject);
55
+
56
+ return observerObject->obs;
57
+ }
58
+
59
+ void cObserverNative_free(void *ptr) {
60
+ free(ptr);
61
+ }
62
+
63
+ VALUE createInstanceFromObserver(Observer *obs) {
64
+ struct ObserverObject *obj;
65
+
66
+ obj = malloc(sizeof(struct ObserverObject));
67
+ obj->obs = obs;
68
+
69
+ return Data_Wrap_Struct(rb_cObserverNative, 0, cObserverNative_free, obj);
70
+ }
71
+
72
+ static VALUE cObserverNative_new(int argc, VALUE *argv, VALUE klass)
73
+ {
74
+ VALUE name, handler, obj;
75
+ Observer *obs;
76
+
77
+ NSAutoreleasePool *pool = [NSAutoreleasePool new];
78
+
79
+ rb_scan_args(argc, argv, "2", &name, &handler);
80
+
81
+ obs = [[Observer alloc] init];
82
+ obj = createInstanceFromObserver(obs);
83
+
84
+ if(RTEST(name)) {
85
+ [obs setName:[NSString stringWithUTF8String:StringValuePtr(name)]];
86
+ }
87
+
88
+ [obs setHandler:handler];
89
+ [obs observe];
90
+
91
+ return obj;
92
+ }
93
+
94
+ // static VALUE cObserverNative_notify(int argc, VALUE *argv, VALUE self)
95
+ // {
96
+ // VALUE name, hash;
97
+
98
+ // rb_scan_args(argc, argv, "2", &name, &hash);
99
+
100
+ // rb_p(name);
101
+ // rb_p(hash);
102
+
103
+ // return Qnil;
104
+ // }
105
+
106
+ static VALUE cObserverNative_run(int argc, VALUE *argv, VALUE self)
107
+ {
108
+ [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
109
+
110
+ return Qnil;
111
+ }
112
+
113
+ static VALUE cObserverNative_run_forever(int argc, VALUE *argv, VALUE self)
114
+ {
115
+ [[NSRunLoop currentRunLoop] run];
116
+
117
+ return Qnil;
118
+ }
119
+
120
+ void Init_observer_native(void){
121
+ VALUE rb_mEventMachine, rb_mDistributedNotification;
122
+
123
+ rb_mEventMachine = rb_define_module("EventMachine");
124
+ rb_mDistributedNotification = rb_define_module_under(rb_mEventMachine, "DistributedNotification");
125
+ rb_cObserverNative = rb_define_class_under(rb_mDistributedNotification, "ObserverNative", rb_cObject);
126
+ rb_define_singleton_method(rb_cObserverNative, "new", cObserverNative_new, -1);
127
+ //rb_define_method(rb_cObserverNative, "notify", cObserverNative_notify, -1);
128
+ rb_define_method(rb_cObserverNative, "run", cObserverNative_run, -1);
129
+ rb_define_method(rb_cObserverNative, "run_forever", cObserverNative_run_forever, -1);
130
+ }
@@ -0,0 +1,30 @@
1
+ require 'observer_native'
2
+
3
+ module EventMachine
4
+ class DistributedNotificationWatch
5
+ def initialize(name)
6
+ @observer = DistributedNotification::ObserverNative.new(name, self)
7
+ end
8
+
9
+ def notify(name, user_info)
10
+ end
11
+
12
+ def start
13
+ @timer = EventMachine::add_periodic_timer(1) do
14
+ @observer.run
15
+ end
16
+ end
17
+
18
+ def stop
19
+ @timer.cancel if @timer
20
+ end
21
+ end
22
+
23
+ def self.watch_distributed_notification(name, handler = nil, *args, &block)
24
+ args = [name, *args]
25
+ klass = klass_from_handler(EventMachine::DistributedNotificationWatch, handler, *args);
26
+ c = klass.new(*args, &block)
27
+ c.start
28
+ c
29
+ end
30
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'rubygems'
4
+ require 'spec_helper'
5
+ require 'eventmachine'
6
+ require 'appscript'
7
+
8
+ class Watcher < EM::DistributedNotificationWatch
9
+ attr_accessor :value
10
+
11
+ def notify(name, user_info)
12
+ @value = name
13
+ p user_info
14
+ end
15
+ end
16
+
17
+ describe EventMachine::DistributedNotificationWatch do
18
+ itunes = Appscript.app('iTunes')
19
+ itunes.run
20
+ itunes.stop
21
+
22
+ context 'instantiate' do
23
+ it 'should observe distributed notifications' do
24
+ watcher = Watcher.new(nil)
25
+
26
+ EM.run {
27
+ watcher.start
28
+
29
+ itunes.playlists["Music"].tracks[1].play
30
+
31
+ EM::add_timer(1) {
32
+ itunes.stop
33
+ EM.stop
34
+ }
35
+ }
36
+
37
+ watcher.value.should_not be_nil
38
+ end
39
+ end
40
+
41
+ context 'invoked from EM.watch_distributed_notification' do
42
+ it 'should observe distributed notifications from ' do
43
+ watcher = nil
44
+
45
+ EM.run {
46
+ watcher = EM.watch_distributed_notification(nil, Watcher)
47
+
48
+ itunes.playlists["Music"].tracks[1].play
49
+
50
+ EM::add_timer(1) {
51
+ itunes.stop
52
+ EM.stop
53
+ }
54
+ }
55
+
56
+ watcher.value.should_not be_nil
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'eventmachine-distributed-notification'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eventmachine-distributed-notification
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - youpy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: eventmachine
16
+ requirement: &70175755995160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70175755995160
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70175755994400 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.8.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70175755994400
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &70175756009200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70175756009200
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: &70175756008080 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.1.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70175756008080
58
+ - !ruby/object:Gem::Dependency
59
+ name: jeweler
60
+ requirement: &70175756006500 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.8.3
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70175756006500
69
+ - !ruby/object:Gem::Dependency
70
+ name: rb-appscript
71
+ requirement: &70175756005020 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70175756005020
80
+ description: An EventMachine extension to watch OSX's Distributed Notification, posted
81
+ by iTunes etc.
82
+ email: youpy@buycheapviagraonlinenow.com
83
+ executables: []
84
+ extensions:
85
+ - ext/observer_native/extconf.rb
86
+ extra_rdoc_files:
87
+ - LICENSE.txt
88
+ - README.rdoc
89
+ files:
90
+ - .document
91
+ - .rspec
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.rdoc
96
+ - Rakefile
97
+ - VERSION
98
+ - ext/observer_native/compat.h
99
+ - ext/observer_native/extconf.rb
100
+ - ext/observer_native/observer_native.h
101
+ - ext/observer_native/observer_native.m
102
+ - lib/eventmachine-distributed-notification.rb
103
+ - spec/eventmachine-distributed-notification_spec.rb
104
+ - spec/spec_helper.rb
105
+ homepage: http://github.com/youpy/eventmachine-distributed-notification
106
+ licenses:
107
+ - MIT
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: -1243824302226179873
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 1.8.10
130
+ signing_key:
131
+ specification_version: 3
132
+ summary: An EventMachine extension to watch OSX's Distributed Notification
133
+ test_files: []