Skip to content

Commit eb3f13e

Browse files
committed
Merge spidah's commits and some test codes.
1 parent e84809d commit eb3f13e

5 files changed

+134
-9
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
* Now sends out Origin-X headers.
2525
* Use \r\n instead of \n in the header lines.
2626

27+
== Version 0.2.0 - 2009/11/03
28+
* Merge spidahman's commits. Lot of thanks, spidahman!
29+
* Add some test(spec).
30+

lib/ruby_gntp.rb

+19-8
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,11 @@ def notify(params)
122122
# instant notification
123123
#
124124
def self.notify(params)
125-
growl = GNTP.new(params[:app_name])
125+
host = params[:host]
126+
passwd = params[:passwd]
127+
128+
growl = GNTP.new(params[:app_name], host, passwd)
129+
126130
notification = params
127131
notification[:name] = params[:app_name] || "Ruby/GNTP notification"
128132
growl.register(:notifications => [
@@ -169,6 +173,7 @@ def register_header(app_name, app_icon)
169173
message = "#{get_gntp_header_start('REGISTER')}\r\n"
170174
message << "Application-Name: #{app_name}\r\n"
171175
message << "#{handle_icon(@app_icon, 'Application')}\r\n" if app_icon
176+
message
172177
end
173178

174179
#
@@ -182,6 +187,7 @@ def notify_header(app_name, name, title, text, sticky, icon)
182187
message << "Notification-Text: #{text}\r\n" if text
183188
message << "Notification-Sticky: #{sticky}\r\n" if sticky
184189
message << "#{handle_icon(icon, 'Notification')}\r\n" if icon
190+
message
185191
end
186192

187193
def output_origin_headers
@@ -262,24 +268,29 @@ def output_binary(binary)
262268
#----------------------------
263269
# self test code
264270
if __FILE__ == $0
271+
host = ARGV[0] || 'loaclhost'
272+
passwd = host && ARGV[1]
273+
265274
#--- Use standard notification method ('register' first then 'notify')
266-
growl = GNTP.new("Ruby/GNTP self test")
267-
growl.register({:notifications => [{
275+
growl = GNTP.new("Ruby/GNTP self test", host, passwd)
276+
growl.register(:notifications => [{
268277
:name => "notify",
269-
:enabled => true,
270-
}]})
278+
:enabled => true
279+
}])
271280

272-
growl.notify({
281+
growl.notify(
273282
:name => "notify",
274283
:title => "Congraturation",
275284
:text => "Congraturation! You are successful install ruby_gntp.",
276285
:icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",
277-
:sticky=> true,
278-
})
286+
:sticky=> true
287+
)
279288

280289
#--- Use instant notification method (just 'notify')
281290
GNTP.notify({
282291
:app_name => "Instant notify",
292+
:host => host,
293+
:passwd => passwd,
283294
:title => "Instant notification",
284295
:text => "Instant notification available now.",
285296
:icon => "http://www.hatena.ne.jp/users/sn/snaka72/profile.gif",

ruby_gntp.gemspec

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = "ruby_gntp"
3-
s.version = "0.1.3.1"
3+
s.version = "0.2.0"
44
s.summary = "Ruby library for GNTP(Growl Notification Transport Protocol) client"
55
s.authors = ["snaka", "David Hayward (spidah)"]
66
@@ -9,6 +9,8 @@ Gem::Specification.new do |s|
99
"lib/ruby_gntp.rb",
1010
"example/twitter_notifier.rb",
1111
"example/gntp-notify",
12+
"test/ruby_gntp_spec.rb",
13+
"test/ruby_gntp_spec_helper.rb",
1214
"README",
1315
"TODO",
1416
"ChangeLog"

test/ruby_gntp_spec.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
require '../lib/ruby_gntp'
2+
require 'ruby_gntp_spec_helper'
3+
4+
# use Double Ruby for mock/stub framework.
5+
Spec::Runner.configure do |conf|
6+
conf.mock_with :rr
7+
end
8+
9+
# describe GNTP behavior
10+
describe GNTP do
11+
include GNTPExampleHelperMethods
12+
13+
DEFAULT_APP_NAME = "Ruby/GNTP"
14+
NOTIFICATION_NAME = "TestApp"
15+
16+
before do
17+
@sended_messages = []
18+
@ok_response = StringIO.new(["GNTP/1.0 -OK NONE\r\n", "\r\n"].join)
19+
@opened_socket = create_stub_socket(@ok_response, @sended_messages)
20+
end
21+
22+
it "can register notifications with minimum params" do
23+
@gntp = GNTP.new
24+
@gntp.register :notifications => [{:name => NOTIFICATION_NAME}]
25+
26+
[
27+
"GNTP/1.0 REGISTER NONE\r\n",
28+
"Application-Name: #{DEFAULT_APP_NAME}\r\n",
29+
"Notifications-Count: 1\r\n",
30+
"\r\n",
31+
"Notification-Name: #{NOTIFICATION_NAME}\r\n",
32+
"Notification-Display-Name: #{NOTIFICATION_NAME}\r\n",
33+
"Notification-Enabled: True\r\n"
34+
].each {|expected_text|
35+
@sended_messages.last.should include(expected_text)
36+
}
37+
end
38+
39+
it "can register notifications to remote host" do
40+
@gntp = GNTP.new "TestApp", "1.2.3.4", "password", 12345
41+
@gntp.register :notifications => [{:name => NOTIFICATION_NAME}]
42+
43+
@opened_socket[:host].should == "1.2.3.4"
44+
@opened_socket[:port].should == 12345
45+
46+
@sended_messages.last.first.should match(/GNTP\/1\.0 REGISTER NONE MD5:\S+\r\n/)
47+
[
48+
"Application-Name: TestApp\r\n",
49+
"Notifications-Count: 1\r\n",
50+
"\r\n",
51+
"Notification-Name: #{NOTIFICATION_NAME}\r\n",
52+
"Notification-Display-Name: #{NOTIFICATION_NAME}\r\n",
53+
"Notification-Enabled: True\r\n"
54+
].each {|expected_text|
55+
@sended_messages.last.should include(expected_text)
56+
}
57+
end
58+
59+
it "can notify with minimum params" do
60+
@gntp = GNTP.new
61+
@gntp.register :notifications => [{:name => NOTIFICATION_NAME}]
62+
@gntp.notify :name => NOTIFICATION_NAME
63+
64+
[
65+
"GNTP/1.0 NOTIFY NONE\r\n",
66+
"Application-Name: #{DEFAULT_APP_NAME}\r\n",
67+
"Notification-Name: #{NOTIFICATION_NAME}\r\n",
68+
"Notification-Title: \r\n"
69+
].each {|expected_text|
70+
@sended_messages.last.should include(expected_text)
71+
}
72+
end
73+
74+
end

test/ruby_gntp_spec_helper.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module GNTPExampleHelperMethods
2+
3+
def create_stub_socket(ok_response, sended_messages)
4+
5+
sock = {}
6+
7+
stub(sock).write do |string|
8+
lines = []
9+
buf = StringIO.new(string)
10+
11+
while line = buf.gets
12+
lines << line
13+
end
14+
15+
sended_messages << lines
16+
ok_response.rewind
17+
end
18+
19+
stub(sock).gets do
20+
ok_response.gets
21+
end
22+
23+
stub(sock).close
24+
25+
stub(TCPSocket).open do |host, port|
26+
sock[:host] = host
27+
sock[:port] = port
28+
sock
29+
end
30+
31+
sock
32+
end
33+
34+
end

0 commit comments

Comments
 (0)