sr-shout-bot 0.0.1

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.
Files changed (2) hide show
  1. data/shout-bot.rb +181 -0
  2. metadata +62 -0
@@ -0,0 +1,181 @@
1
+ =begin
2
+ ShoutBot
3
+ Ridiculously simple library to quickly say something on IRC
4
+ <http://github.com/sr/shout-bot>
5
+
6
+ EXAMPLE
7
+
8
+ ShoutBot.shout('irc://irc.freenode.net:6667/github', :as => "ShoutBot") do |channel|
9
+ channel.say "check me out! http://github.com/sr/shout-bot"
10
+ end
11
+
12
+ LICENSE
13
+
14
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
15
+ Version 2, December 2004
16
+
17
+ Copyright (C) 2008 Simon Rozet <http://purl.org/net/sr/>
18
+ Copyright (C) 2008 Harry Vangberg <http://trueaffection.net>
19
+
20
+ Everyone is permitted to copy and distribute verbatim or modified
21
+ copies of this license document, and changing it is allowed as long
22
+ as the name is changed.
23
+
24
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
25
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
26
+
27
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
28
+ =end
29
+
30
+ require "rubygems"
31
+ require "addressable/uri"
32
+ require "socket"
33
+
34
+ class ShoutBot
35
+ def self.shout(uri, options={}, &block)
36
+ raise ArgumentError unless block_given?
37
+
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)
41
+ end
42
+ end
43
+
44
+ def initialize(server, port, nick)
45
+ raise ArgumentError unless block_given?
46
+
47
+ @socket = TCPSocket.open(server, port)
48
+ @socket.puts "NICK #{nick}"
49
+ @socket.puts "USER #{nick} #{nick} #{nick} :#{nick}"
50
+ yield self
51
+ @socket.puts "QUIT"
52
+ @socket.gets until @socket.eof?
53
+ end
54
+
55
+ def join(channel)
56
+ raise ArgumentError unless block_given?
57
+
58
+ @channel = "##{channel}"
59
+ @socket.puts "JOIN #{@channel}"
60
+ yield self
61
+ @socket.puts "PART #{@channel}"
62
+ end
63
+
64
+ def say(message)
65
+ return unless @channel
66
+ @socket.puts "PRIVMSG #{@channel} :#{message}"
67
+ end
68
+ end
69
+
70
+ if $0 == __FILE__
71
+ begin
72
+ require "spec"
73
+ rescue LoadError
74
+ abort "No test for you :-("
75
+ end
76
+
77
+ describe "ShoutBot" do
78
+ def create_shouter(&block)
79
+ @shouter ||= ShoutBot.new("irc.freenode.net", 6667, "john", &block || lambda {})
80
+ end
81
+
82
+ setup do
83
+ @socket = mock("socket", :puts => "", :eof? => true, :gets => "")
84
+ TCPSocket.stub!(:open).and_return(@socket)
85
+ end
86
+
87
+ it "should exists" do
88
+ ShoutBot.should be_an_instance_of Class
89
+ end
90
+
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
95
+
96
+ it "raises ArgumentError if no block given" do
97
+ lambda { do_shout(nil) }.should raise_error(ArgumentError)
98
+ end
99
+
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
104
+
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
110
+
111
+ it "passes given block to join" do
112
+ pending
113
+ end
114
+ end
115
+
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)
121
+ end
122
+
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
126
+ end
127
+
128
+ it "sets its nick" do
129
+ @socket.should_receive(:puts).with("NICK john")
130
+ create_shouter
131
+ end
132
+
133
+ it "yields itself" do
134
+ create_shouter { |shouter| shouter.should respond_to(:join) }
135
+ end
136
+
137
+ it "quits" do
138
+ @socket.should_receive(:puts).with("QUIT")
139
+ create_shouter
140
+ end
141
+ end
142
+
143
+ describe "When joining a channel" do
144
+ def do_join(&block)
145
+ create_shouter { |shouter| shouter.join('foo', &block || lambda {}) }
146
+ end
147
+
148
+ it "raises ArgumentError if no block given" do
149
+ lambda do
150
+ do_join(nil)
151
+ end.should raise_error(ArgumentError)
152
+ end
153
+
154
+ it "joins the given channel" do
155
+ @socket.should_receive(:puts).with("JOIN #foo")
156
+ do_join
157
+ end
158
+
159
+ it "yields itself" do
160
+ do_join { |channel| channel.should respond_to(:say) }
161
+ end
162
+
163
+ it "parts the given channel" do
164
+ @socket.should_receive(:puts).with("PART #foo")
165
+ do_join
166
+ end
167
+ end
168
+
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" } }
173
+ end
174
+
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 }
178
+ end
179
+ end
180
+ end
181
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sr-shout-bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Simon Rozet
8
+ - Harry Vangberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-11-19 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: addressable
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Ridiculously simple library to quickly say something on IRC
26
+ email: simon@rozet.name
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - shout-bot.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/sr/shout-bot
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - .
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 2
60
+ summary: Ridiculously simple library to quickly say something on IRC
61
+ test_files: []
62
+