sr-shout-bot 0.0.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/shout-bot.rb +131 -83
  2. metadata +3 -2
@@ -5,7 +5,7 @@ ShoutBot
5
5
 
6
6
  EXAMPLE
7
7
 
8
- ShoutBot.shout('irc://irc.freenode.net:6667/github', :as => "ShoutBot") do |channel|
8
+ ShoutBot.shout('irc://shoutbot:password@irc.freenode.net:6667/#github') do |channel|
9
9
  channel.say "check me out! http://github.com/sr/shout-bot"
10
10
  end
11
11
 
@@ -14,8 +14,8 @@ LICENSE
14
14
  DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
15
15
  Version 2, December 2004
16
16
 
17
- Copyright (C) 2008 Simon Rozet <http://purl.org/net/sr/>
18
- Copyright (C) 2008 Harry Vangberg <http://trueaffection.net>
17
+ Copyright (C) 2008, 2009 Simon Rozet <http://purl.org/net/sr/>
18
+ Copyright (C) 2008, 2009 Harry Vangberg <http://trueaffection.net>
19
19
 
20
20
  Everyone is permitted to copy and distribute verbatim or modified
21
21
  copies of this license document, and changing it is allowed as long
@@ -32,21 +32,30 @@ require "addressable/uri"
32
32
  require "socket"
33
33
 
34
34
  class ShoutBot
35
- def self.shout(uri, options={}, &block)
35
+ def self.shout(uri, &block)
36
36
  raise ArgumentError unless block_given?
37
37
 
38
38
  uri = Addressable::URI.parse(uri)
39
- irc = new(uri.host, uri.port, options.delete(:as)) do |irc|
40
- irc.join(uri.path[1..-1], &block)
39
+ irc = new(uri.host, uri.port, uri.user, uri.password) do |irc|
40
+ if channel = uri.fragment
41
+ irc.join(channel, &block)
42
+ else
43
+ irc.channel = uri.path[1..-1]
44
+ yield irc
45
+ end
41
46
  end
42
47
  end
43
48
 
44
- def initialize(server, port, nick)
49
+ attr_accessor :channel
50
+
51
+ def initialize(server, port, nick, password=nil)
45
52
  raise ArgumentError unless block_given?
46
53
 
47
- @socket = TCPSocket.open(server, port)
54
+ @socket = TCPSocket.open(server, port || 6667)
55
+ @socket.puts "PASSWORD #{password}" if password
48
56
  @socket.puts "NICK #{nick}"
49
57
  @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
58
+ sleep 1
50
59
  yield self
51
60
  @socket.puts "QUIT"
52
61
  @socket.gets until @socket.eof?
@@ -67,115 +76,154 @@ class ShoutBot
67
76
  end
68
77
  end
69
78
 
70
- if $0 == __FILE__
71
- begin
72
- require "spec"
73
- rescue LoadError
74
- abort "No test for you :-("
79
+ if $0 == __FILE__ || $0 == "-e"
80
+ require "test/unit"
81
+ require "context"
82
+ require "rr"
83
+
84
+ class MockSocket
85
+ attr_accessor :in, :out
86
+ def gets() @in.gets end
87
+ def puts(m) @out.puts(m) end
88
+ def eof?() true end
75
89
  end
76
90
 
77
- describe "ShoutBot" do
78
- def create_shouter(&block)
79
- @shouter ||= ShoutBot.new("irc.freenode.net", 6667, "john", &block || lambda {})
80
- end
91
+ class ShoutBot
92
+ include Test::Unit::Assertions
93
+ end
94
+
95
+ class Test::Unit::TestCase
96
+ include RR::Adapters::TestUnit
81
97
 
82
- setup do
83
- @socket = mock("socket", :puts => "", :eof? => true, :gets => "")
84
- TCPSocket.stub!(:open).and_return(@socket)
98
+ def setup
99
+ @socket, @server = MockSocket.new, MockSocket.new
100
+ @socket.in, @server.out = IO.pipe
101
+ @server.in, @socket.out = IO.pipe
102
+
103
+ stub(TCPSocket).open(anything, anything) {@socket}
85
104
  end
105
+ end
86
106
 
87
- it "should exists" do
88
- ShoutBot.should be_an_instance_of Class
107
+ class TestShoutBot < Test::Unit::TestCase
108
+ def create_shoutbot(&block)
109
+ ShoutBot.new("irc.freenode.net", 6667, "john", &block || lambda {})
89
110
  end
90
111
 
91
- describe "When using Shouter.shout" do
92
- def do_shout(&block)
93
- ShoutBot.shout("irc://irc.freenode.net:6667/foo", :as => "john", &block || lambda {})
94
- end
112
+ def create_shoutbot_and_register(&block)
113
+ create_shoutbot &block
114
+ 2.times { @server.gets } # NICK + USER
115
+ end
95
116
 
96
- it "raises ArgumentError if no block given" do
97
- lambda { do_shout(nil) }.should raise_error(ArgumentError)
117
+ test "raises error if no block given" do
118
+ assert_raises ArgumentError do
119
+ ShoutBot.new("irc.freenode.net", 6667, "john")
98
120
  end
121
+ end
99
122
 
100
- it "creates a new shouter using URI and :as option" do
101
- ShoutBot.should_receive(:new).with("irc.freenode.net", 6667, "john")
102
- do_shout
103
- end
123
+ test "registers to the irc server" do
124
+ create_shoutbot
125
+ assert_equal "NICK john\n", @server.gets
126
+ assert_equal "USER john john john :john\n", @server.gets
127
+ end
104
128
 
105
- it "join channel using URI's path" do
106
- create_shouter.should_receive(:join).with("foo")
107
- ShoutBot.stub!(:new).and_yield(create_shouter)
108
- do_shout
109
- end
129
+ test "sends password if specified" do
130
+ # hey, retarded test!
131
+ ShoutBot.new("irc.freenode.net", 6667, "john", "malbec") {@socket}
132
+ assert_equal "PASSWORD malbec\n", @server.gets
133
+ end
134
+
135
+ test "falls back to port 6667 if not specified" do
136
+ # talk about retarded test
137
+ mock(TCPSocket).open("irc.freenode.net", 6667) {@socket}
138
+ ShoutBot.new("irc.freenode.net", nil, "john") {}
139
+ end
110
140
 
111
- it "passes given block to join" do
112
- pending
113
- end
141
+ test "quits after doing its job" do
142
+ create_shoutbot_and_register {}
143
+ assert_equal "QUIT\n", @server.gets
114
144
  end
115
145
 
116
- describe "When initializing" do
117
- it "raises ArgumentError if no block given" do
118
- lambda do
119
- create_shouter(nil)
120
- end.should raise_error(ArgumentError)
146
+ test "raises error if no block is given to join" do
147
+ create_shoutbot do |bot|
148
+ assert_raises(ArgumentError) {bot.join "integrity"}
121
149
  end
150
+ end
122
151
 
123
- it "opens a TCPSocket to the given host on the given port" do
124
- TCPSocket.should_receive(:open).with("irc.freenode.net", 6667).and_return(@socket)
125
- create_shouter
152
+ test "joins channel" do
153
+ create_shoutbot_and_register do |bot|
154
+ bot.join("integrity") {}
126
155
  end
156
+ assert_equal "JOIN #integrity\n", @server.gets
157
+ end
127
158
 
128
- it "sets its nick" do
129
- @socket.should_receive(:puts).with("NICK john")
130
- create_shouter
131
- end
159
+ test "doesn't do anything until receiving RPL_MYINFO / 004" do
160
+ # pending
161
+ end
132
162
 
133
- it "yields itself" do
134
- create_shouter { |shouter| shouter.should respond_to(:join) }
163
+ test "joins channel and says something" do
164
+ create_shoutbot_and_register do |bot|
165
+ bot.join "integrity" do |c|
166
+ c.say "foo bar!"
167
+ end
135
168
  end
169
+ @server.gets # JOIN
170
+ assert_equal "PRIVMSG #integrity :foo bar!\n", @server.gets
171
+ end
136
172
 
137
- it "quits" do
138
- @socket.should_receive(:puts).with("QUIT")
139
- create_shouter
173
+ test "sends private message to user" do
174
+ create_shoutbot_and_register do |bot|
175
+ bot.channel = "sr"
176
+ bot.say "Look Ma, new tests!"
140
177
  end
178
+ assert_equal "PRIVMSG sr :Look Ma, new tests!\n", @server.gets
141
179
  end
180
+ end
142
181
 
143
- describe "When joining a channel" do
144
- def do_join(&block)
145
- create_shouter { |shouter| shouter.join('foo', &block || lambda {}) }
146
- end
182
+ class TestShouter < Test::Unit::TestCase
183
+ def create_shouter(&block)
184
+ shouter = ShoutBot.new("irc.freenode.net", 6667, "shouter") {}
185
+ mock(ShoutBot).new(anything, anything, anything, anything).yields(shouter) {shouter}
186
+ shouter
187
+ end
147
188
 
148
- it "raises ArgumentError if no block given" do
149
- lambda do
150
- do_join(nil)
151
- end.should raise_error(ArgumentError)
189
+ test "raises error unless block is given" do
190
+ assert_raises ArgumentError do
191
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo")
152
192
  end
193
+ end
153
194
 
154
- it "joins the given channel" do
155
- @socket.should_receive(:puts).with("JOIN #foo")
156
- do_join
157
- end
195
+ test "creates a new instance of shoutbot" do
196
+ mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", nil)
197
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/foo") {}
198
+ end
158
199
 
159
- it "yields itself" do
160
- do_join { |channel| channel.should respond_to(:say) }
161
- end
200
+ test "creates a new instance of shoutbot with password" do
201
+ mock(ShoutBot).new("irc.freenode.net", 6667, "shouter", "badass")
202
+ ShoutBot.shout("irc://shouter:badass@irc.freenode.net:6667/foo") {}
203
+ end
162
204
 
163
- it "parts the given channel" do
164
- @socket.should_receive(:puts).with("PART #foo")
165
- do_join
166
- end
205
+ test "joins channel" do
206
+ shouter = create_shouter
207
+ mock(shouter).join("integrity")
208
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") {}
167
209
  end
168
210
 
169
- describe "When saying something" do
170
- it "should say the given message in the channel" do
171
- @socket.should_receive(:puts).with("PRIVMSG #foo :bar")
172
- create_shouter { |shouter| shouter.join("foo") { |channel| channel.say "bar" } }
211
+ test "says stuff in channel" do
212
+ shouter = create_shouter
213
+ mock(shouter).say("foo bar!")
214
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/#integrity") do |bot|
215
+ bot.say "foo bar!"
173
216
  end
217
+ assert_equal "#integrity", shouter.channel
218
+ end
174
219
 
175
- it "should stfu and return nil if not joined to a channel" do
176
- @socket.should_not_receive(:puts).with("PRIVMSG #foo :bar")
177
- create_shouter { |shouter| shouter.say("bar").should be_nil }
220
+ test "sends private message to nick" do
221
+ shouter = create_shouter
222
+ mock(shouter).say("foo bar!")
223
+ ShoutBot.shout("irc://shouter@irc.freenode.net:6667/harry") do |bot|
224
+ bot.say "foo bar!"
178
225
  end
226
+ assert_equal "harry", shouter.channel
179
227
  end
180
228
  end
181
229
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sr-shout-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Rozet
@@ -10,11 +10,12 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-11-19 00:00:00 -08:00
13
+ date: 2009-03-02 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: addressable
18
+ type: :runtime
18
19
  version_requirement:
19
20
  version_requirements: !ruby/object:Gem::Requirement
20
21
  requirements: