notifier 0.2.1 → 0.4.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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
@@ -1,22 +1,26 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- notifier (0.2.1)
4
+ notifier (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- metaclass (0.0.1)
10
- mocha (0.11.4)
11
- metaclass (~> 0.0.1)
9
+ diff-lcs (1.1.3)
12
10
  rake (0.9.2.2)
13
- test-unit (2.4.8)
11
+ rspec (2.11.0)
12
+ rspec-core (~> 2.11.0)
13
+ rspec-expectations (~> 2.11.0)
14
+ rspec-mocks (~> 2.11.0)
15
+ rspec-core (2.11.1)
16
+ rspec-expectations (2.11.3)
17
+ diff-lcs (~> 1.1.3)
18
+ rspec-mocks (2.11.2)
14
19
 
15
20
  PLATFORMS
16
21
  ruby
17
22
 
18
23
  DEPENDENCIES
19
- mocha
20
24
  notifier!
21
25
  rake
22
- test-unit
26
+ rspec
@@ -3,6 +3,7 @@
3
3
  Send system notifications on several platforms with a simple and unified API. Currently supports:
4
4
 
5
5
  * Growl (Mac OS X)
6
+ * GNTP Protocol (Growl, with Vagrant support)
6
7
  * Kdialog (Linux/KDE)
7
8
  * Knotify (Linux/KDE)
8
9
  * OSD Cat (Linux)
@@ -1,4 +1,7 @@
1
1
  require "open3"
2
+ require "socket"
3
+ require "digest/md5"
4
+ require "timeout"
2
5
  require "rbconfig"
3
6
 
4
7
  module Notifier
@@ -11,6 +14,7 @@ module Notifier
11
14
  autoload :NotifySend, "notifier/notify_send"
12
15
  autoload :Placebo, "notifier/placebo"
13
16
  autoload :Version, "notifier/version"
17
+ autoload :Adapters, "notifier/adapters"
14
18
 
15
19
  extend self
16
20
 
@@ -28,7 +32,7 @@ module Notifier
28
32
 
29
33
  def notifiers
30
34
  constants.collect do |name|
31
- const_get(name) unless %w[Placebo Version].include?(name.to_s)
35
+ const_get(name) unless %w[Placebo Adapters Version].include?(name.to_s)
32
36
  end.compact + [Placebo]
33
37
  end
34
38
 
@@ -0,0 +1,5 @@
1
+ module Notifier
2
+ module Adapters
3
+ autoload :GNTP, "notifier/adapters/gntp"
4
+ end
5
+ end
@@ -0,0 +1,85 @@
1
+ module Notifier
2
+ module Adapters
3
+ class GNTP
4
+ attr_accessor :application_name
5
+ attr_accessor :host
6
+ attr_accessor :port
7
+ attr_accessor :password
8
+
9
+ def initialize(options = {})
10
+ if options.kind_of?(String)
11
+ options = {:name => options}
12
+ end
13
+
14
+ @application_name = options.fetch(:name, "GNTP/Ruby")
15
+ @host = options.fetch(:host, "127.0.0.1")
16
+ @port = options.fetch(:port, 23053)
17
+ @password = options.fetch(:password, nil)
18
+ end
19
+
20
+ def line_break(number = 1)
21
+ "\r\n" * number
22
+ end
23
+
24
+ def write(*contents)
25
+ socket = TCPSocket.open(host, port)
26
+ message = [*contents, line_break(2)].join(line_break)
27
+ socket.write(message)
28
+
29
+ "".tap do |response|
30
+ while chunk = socket.gets
31
+ break if chunk == line_break
32
+ response << chunk
33
+ end
34
+
35
+ socket.close
36
+ end
37
+ end
38
+
39
+ def notify(options)
40
+ name = options.fetch(:name, "notification")
41
+ register(name)
42
+
43
+ icon = fetch_icon(options[:icon])
44
+
45
+ result = write "GNTP/1.0 NOTIFY NONE",
46
+ "Application-Name: #{application_name}",
47
+ "Notification-Name: #{name}",
48
+ "Notification-Title: #{options[:title]}",
49
+ "Notification-Text: #{options[:message]}",
50
+ "Notification-Icon: x-growl-resource://#{icon[:identifier]}",
51
+ "Notification-Sticky: #{bool options[:sticky]}",
52
+ nil,
53
+ "Identifier: #{icon[:identifier]}",
54
+ "Length: #{icon[:size]}",
55
+ nil,
56
+ icon[:contents]
57
+ end
58
+
59
+ def fetch_icon(path)
60
+ contents = File.open(path, "rb") {|f| f.read }
61
+
62
+ {
63
+ :identifier => Digest::MD5.hexdigest(contents),
64
+ :contents => contents,
65
+ :size => contents.bytesize
66
+ }
67
+ end
68
+
69
+ def bool(boolean)
70
+ {true => "Yes", false => "No"}[boolean]
71
+ end
72
+
73
+ def register(name)
74
+ result = write(
75
+ "GNTP/1.0 REGISTER NONE",
76
+ "Application-Name: #{application_name}",
77
+ "Notifications-count: 1",
78
+ nil,
79
+ "Notification-Name: #{name}",
80
+ "Notification-Enabled: True"
81
+ )
82
+ end
83
+ end
84
+ end
85
+ end
@@ -1,22 +1,37 @@
1
1
  module Notifier
2
- module GNTP
3
- extend self
4
-
2
+ module GNTP extend self
5
3
  def supported?
6
- begin
7
- require "ruby_gntp" unless defined?(::GNTP)
8
- true
9
- rescue LoadError
10
- false
11
- end
4
+ Timeout.timeout(1) { TCPSocket.new(host, port).close }
5
+ true
6
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error
7
+ false
8
+ end
9
+
10
+ def port
11
+ ENV.fetch("GNTP_PORT", 23053)
12
+ end
13
+
14
+ def host
15
+ ENV["GNTP_HOST"] || ssh_connection || "127.0.0.1"
16
+ end
17
+
18
+ def ssh_connection
19
+ ENV["SSH_CONNECTION"][/^([^ ]+)/, 1] if ENV["SSH_CONNECTION"]
12
20
  end
13
21
 
14
22
  def notify(options)
15
- ::GNTP.notify(
16
- :title => options[:title],
17
- :text => options[:message],
18
- :icon => options[:image]
19
- )
23
+ gntp = Adapters::GNTP.new({
24
+ :name => "test_notifier",
25
+ :host => host,
26
+ :port => port
27
+ })
28
+
29
+ gntp.notify({
30
+ :name => "status",
31
+ :title => options[:title],
32
+ :message => options[:message],
33
+ :icon => options[:image]
34
+ })
20
35
  end
21
36
  end
22
37
  end
@@ -1,8 +1,8 @@
1
1
  module Notifier
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 2
5
- PATCH = 1
4
+ MINOR = 4
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -19,7 +19,6 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.requirements << "Growl, Libnotify, OSD, KDE (Knotify and Kdialog) or Snarl"
21
21
 
22
- s.add_development_dependency "test-unit"
23
- s.add_development_dependency "mocha"
22
+ s.add_development_dependency "rspec"
24
23
  s.add_development_dependency "rake"
25
24
  end
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ describe Notifier do
4
+ before do
5
+ unsupport_all_notifiers
6
+ Notifier.default_notifier = nil
7
+ end
8
+
9
+ it "retrieves list of supported notifiers" do
10
+ Notifier::Snarl.stub :supported? => true
11
+ Notifier::Knotify.stub :supported? => true
12
+
13
+ expect(Notifier.supported_notifiers.size).to eql(3)
14
+ end
15
+
16
+ it "returns first available notifier" do
17
+ Notifier::Snarl.stub :supported? => true
18
+ Notifier::Knotify.stub :supported? => true
19
+
20
+ expect(Notifier.notifier).to eql(Notifier::Snarl)
21
+ end
22
+
23
+ it "prefers default notifier" do
24
+ Notifier::Snarl.stub :supported? => true
25
+ Notifier::Knotify.stub :supported? => true
26
+
27
+ Notifier.default_notifier = :knotify
28
+
29
+ expect(Notifier.notifier).to eql(Notifier::Knotify)
30
+ end
31
+
32
+ it "sends notification" do
33
+ params = {
34
+ :title => "Some title",
35
+ :message => "Some message",
36
+ :image => "image.png"
37
+ }
38
+
39
+ Notifier::Snarl.stub :supported? => true
40
+ Notifier::Snarl.should_receive(:notify).with(params)
41
+
42
+ Notifier.notify(params)
43
+ end
44
+
45
+ it "retrieves list of all notifiers" do
46
+ expect(Notifier.notifiers.size).to eql(8)
47
+ end
48
+
49
+ it "considers Placebo as fallback notifier" do
50
+ expect(Notifier.supported_notifiers.last).to eql(Notifier::Placebo)
51
+ end
52
+
53
+ it "returns notifier by its name" do
54
+ expect(Notifier.from_name(:osd_cat)).to eql(Notifier::OsdCat)
55
+ expect(Notifier.from_name(:notify_send)).to eql(Notifier::NotifySend)
56
+ expect(Notifier.from_name(:growl)).to eql(Notifier::Growl)
57
+ end
58
+
59
+ it "returns notifier by its name when supported" do
60
+ Notifier::Snarl.stub :supported? => true
61
+ expect(Notifier.supported_notifier_from_name(:snarl)).to eql(Notifier::Snarl)
62
+ end
63
+
64
+ it "returns nil when have no supported notifiers" do
65
+ expect(Notifier.supported_notifier_from_name(:snarl)).to be_nil
66
+ end
67
+
68
+ it "returns nil when an invalid notifier name is provided" do
69
+ expect(Notifier.from_name(:invalid)).to be_nil
70
+ expect(Notifier.supported_notifier_from_name(:invalid)).to be_nil
71
+ end
72
+ end
@@ -0,0 +1,13 @@
1
+ require "notifier"
2
+
3
+ module SpecHelpers
4
+ def unsupport_all_notifiers
5
+ Notifier.notifiers.each do |notifier|
6
+ notifier.stub :supported? => false unless notifier == Notifier::Placebo
7
+ end
8
+ end
9
+ end
10
+
11
+ RSpec.configure do |config|
12
+ config.include SpecHelpers
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-11 00:00:00.000000000 Z
12
+ date: 2012-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: test-unit
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: mocha
15
+ name: rspec
32
16
  requirement: !ruby/object:Gem::Requirement
33
17
  none: false
34
18
  requirements:
@@ -68,11 +52,14 @@ extensions: []
68
52
  extra_rdoc_files: []
69
53
  files:
70
54
  - .gitignore
55
+ - .rspec
71
56
  - Gemfile
72
57
  - Gemfile.lock
73
58
  - README.rdoc
74
59
  - Rakefile
75
60
  - lib/notifier.rb
61
+ - lib/notifier/adapters.rb
62
+ - lib/notifier/adapters/gntp.rb
76
63
  - lib/notifier/gntp.rb
77
64
  - lib/notifier/growl.rb
78
65
  - lib/notifier/kdialog.rb
@@ -84,8 +71,8 @@ files:
84
71
  - lib/notifier/version.rb
85
72
  - notifier.gemspec
86
73
  - resources/register-growl.scpt
87
- - test/notifier_test.rb
88
- - test/test_helper.rb
74
+ - spec/notifier_spec.rb
75
+ - spec/spec_helper.rb
89
76
  homepage: http://rubygems.org/gems/notifier
90
77
  licenses: []
91
78
  post_install_message:
@@ -98,18 +85,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
85
  - - ! '>='
99
86
  - !ruby/object:Gem::Version
100
87
  version: '0'
101
- segments:
102
- - 0
103
- hash: -3729581669822386303
104
88
  required_rubygems_version: !ruby/object:Gem::Requirement
105
89
  none: false
106
90
  requirements:
107
91
  - - ! '>='
108
92
  - !ruby/object:Gem::Version
109
93
  version: '0'
110
- segments:
111
- - 0
112
- hash: -3729581669822386303
113
94
  requirements:
114
95
  - Growl, Libnotify, OSD, KDE (Knotify and Kdialog) or Snarl
115
96
  rubyforge_project:
@@ -118,6 +99,4 @@ signing_key:
118
99
  specification_version: 3
119
100
  summary: Send system notifications on several platforms with a simple and unified
120
101
  API. Currently supports Growl, Libnotify, OSD, KDE (Knotify and Kdialog) and Snarl
121
- test_files:
122
- - test/notifier_test.rb
123
- - test/test_helper.rb
102
+ test_files: []
@@ -1,73 +0,0 @@
1
- require "test_helper"
2
-
3
- class NotifierTest < Test::Unit::TestCase
4
- def setup
5
- unsupport_all_notifiers
6
- Notifier.default_notifier = nil
7
- end
8
-
9
- test "retrieve list of supported notifiers" do
10
- Notifier::Snarl.expects(:supported?).returns(true)
11
- Notifier::Knotify.expects(:supported?).returns(true)
12
-
13
- assert_equal 3, Notifier.supported_notifiers.size
14
- end
15
-
16
- test "return first available notifier" do
17
- Notifier::Snarl.expects(:supported?).returns(true)
18
- Notifier::Knotify.expects(:supported?).returns(true)
19
-
20
- assert_equal Notifier::Snarl, Notifier.notifier
21
- end
22
-
23
- test "prefer default notifier" do
24
- Notifier::Snarl.stubs(:supported?).returns(true)
25
- Notifier::Knotify.expects(:supported?).returns(true)
26
-
27
- Notifier.default_notifier = :knotify
28
-
29
- assert_equal Notifier::Knotify, Notifier.notifier
30
- end
31
-
32
- test "send notification" do
33
- params = {
34
- :title => "Some title",
35
- :message => "Some message",
36
- :image => "image.png"
37
- }
38
-
39
- Notifier::Snarl.expects(:supported?).returns(true)
40
- Notifier::Snarl.expects(:notify).with(params)
41
-
42
- Notifier.notify(params)
43
- end
44
-
45
- test "retrieve list of all notifiers" do
46
- assert_equal 8, Notifier.notifiers.size
47
- end
48
-
49
- test "consider Placebo as fallback notifier" do
50
- assert_equal Notifier::Placebo, Notifier.supported_notifiers.last
51
- end
52
-
53
- test "return notifier by its name" do
54
- assert_equal Notifier::OsdCat, Notifier.from_name(:osd_cat)
55
- assert_equal Notifier::NotifySend, Notifier.from_name(:notify_send)
56
- assert_equal Notifier::Growl, Notifier.from_name(:growl)
57
- end
58
-
59
- test "return notifier by its name when supported" do
60
- Notifier::Snarl.expects(:supported?).returns(true)
61
-
62
- assert_equal Notifier::Snarl, Notifier.supported_notifier_from_name(:snarl)
63
- end
64
-
65
- test "return nil when have no supported notifiers" do
66
- assert_nil Notifier.supported_notifier_from_name(:snarl)
67
- end
68
-
69
- test "return nil when an invalid notifier name is provided" do
70
- assert_nil Notifier.from_name(:invalid)
71
- assert_nil Notifier.supported_notifier_from_name(:invalid)
72
- end
73
- end
@@ -1,14 +0,0 @@
1
- gem "test-unit"
2
- require "test/unit"
3
- require "mocha"
4
-
5
- require "notifier"
6
-
7
- class Test::Unit::TestCase
8
- private
9
- def unsupport_all_notifiers
10
- Notifier.notifiers.each do |notifier|
11
- notifier.stubs(:supported?).returns(false) unless notifier == Notifier::Placebo
12
- end
13
- end
14
- end