flamethrower 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 (47) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +10 -0
  4. data/Gemfile.lock +29 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +36 -0
  7. data/Rakefile +28 -0
  8. data/VERSION +1 -0
  9. data/bin/flamethrower +59 -0
  10. data/flamethrower.gemspec +106 -0
  11. data/lib/flamethrower.rb +25 -0
  12. data/lib/flamethrower/campfire/connection.rb +38 -0
  13. data/lib/flamethrower/campfire/message.rb +42 -0
  14. data/lib/flamethrower/campfire/rest_api.rb +40 -0
  15. data/lib/flamethrower/campfire/room.rb +142 -0
  16. data/lib/flamethrower/campfire/user.rb +17 -0
  17. data/lib/flamethrower/dispatcher.rb +102 -0
  18. data/lib/flamethrower/irc/channel.rb +55 -0
  19. data/lib/flamethrower/irc/codes.rb +17 -0
  20. data/lib/flamethrower/irc/commands.rb +57 -0
  21. data/lib/flamethrower/irc/message.rb +41 -0
  22. data/lib/flamethrower/irc/user.rb +33 -0
  23. data/lib/flamethrower/server.rb +43 -0
  24. data/lib/flamethrower/server/event_server.rb +28 -0
  25. data/lib/flamethrower/server/mock_server.rb +13 -0
  26. data/spec/fixtures/enter_message.json +1 -0
  27. data/spec/fixtures/kick_message.json +1 -0
  28. data/spec/fixtures/leave_message.json +1 -0
  29. data/spec/fixtures/message.json +1 -0
  30. data/spec/fixtures/paste_message.json +1 -0
  31. data/spec/fixtures/room.json +1 -0
  32. data/spec/fixtures/room_update.json +1 -0
  33. data/spec/fixtures/rooms.json +1 -0
  34. data/spec/fixtures/speak_message.json +1 -0
  35. data/spec/fixtures/streaming_message.json +1 -0
  36. data/spec/spec_helper.rb +13 -0
  37. data/spec/unit/campfire/connection_spec.rb +37 -0
  38. data/spec/unit/campfire/message_spec.rb +85 -0
  39. data/spec/unit/campfire/room_spec.rb +296 -0
  40. data/spec/unit/campfire/user_spec.rb +32 -0
  41. data/spec/unit/dispatcher_spec.rb +255 -0
  42. data/spec/unit/irc/channel_spec.rb +56 -0
  43. data/spec/unit/irc/message_spec.rb +32 -0
  44. data/spec/unit/irc/user_spec.rb +63 -0
  45. data/spec/unit/server/event_server_spec.rb +16 -0
  46. data/spec/unit/server_spec.rb +164 -0
  47. metadata +165 -0
@@ -0,0 +1,43 @@
1
+ module Flamethrower
2
+ module Server
3
+ include Flamethrower::Irc::Commands
4
+
5
+ attr_accessor :campfire_connection, :current_user, :dispatcher, :irc_channels
6
+
7
+ def initialize(options = {})
8
+ @irc_channels = []
9
+ @current_user = Flamethrower::Irc::User.new
10
+ @dispatcher = Flamethrower::Dispatcher.new(self)
11
+ end
12
+
13
+ def after_connect
14
+ send_motd
15
+ populate_irc_channels
16
+ send_channel_list
17
+ end
18
+
19
+ def send_message(msg)
20
+ send_data "#{msg}\r\n"
21
+ ::FLAMETHROWER_LOGGER.debug ">> #{msg}"
22
+ msg
23
+ end
24
+
25
+ def receive_data(msg)
26
+ messages = msg.split("\r\n")
27
+ messages.each do |message|
28
+ dispatcher.handle_message(Flamethrower::Irc::Message.new(message))
29
+ ::FLAMETHROWER_LOGGER.debug "<< #{message}"
30
+ end
31
+ end
32
+
33
+ def send_messages(*messages)
34
+ yield(messages) if block_given?
35
+ messages.each {|msg| send_message(msg)}
36
+ end
37
+
38
+ def populate_irc_channels
39
+ @irc_channels = campfire_connection.rooms.map(&:to_irc)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,28 @@
1
+ module Flamethrower
2
+ class EventConnection < EventMachine::Connection
3
+ include Flamethrower::Server
4
+
5
+ attr_accessor :server
6
+ end
7
+
8
+ class EventServer
9
+ attr_reader :host, :port, :campfire_connection
10
+
11
+ def initialize(host, port, domain, token)
12
+ @host = host || "0.0.0.0"
13
+ @port = port || 6667
14
+ @domain = domain
15
+ @token = token
16
+ end
17
+
18
+ def start
19
+ EventMachine::run do
20
+ FLAMETHROWER_LOGGER.info "Flamethrower started at #{@host}:#{@port} on domain #{@domain}"
21
+ EventMachine::start_server(@host, @port, EventConnection) do |connection|
22
+ connection.server = self
23
+ connection.campfire_connection = Flamethrower::Campfire::Connection.new(@domain, @token, connection)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Flamethrower
2
+ class MockServer
3
+ include Flamethrower::Server
4
+
5
+ def send_data(msg)
6
+ end
7
+
8
+ def campfire_connection
9
+ Flamethrower::Campfire::Connection.new("mydomain", "mytoken", self)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1 @@
1
+ {"room_id":73541,"created_at":"2010/12/14 20:47:11 +0000","body":null,"id":289382823,"user_id":734581,"type":"EnterMessage"}
@@ -0,0 +1 @@
1
+ {"room_id":73541,"created_at":"2010/12/14 20:40:26 +0000","body":null,"id":289379507,"user_id":613191,"type":"KickMessage"}
@@ -0,0 +1 @@
1
+ {"room_id":347348,"created_at":"2010/12/15 08:16:32 +0000","body":null,"id":289594534,"user_id":734581,"type":"LeaveMessage"}
@@ -0,0 +1 @@
1
+ {"room_id":347348,"created_at":"Tue Nov 09 22:34:55 -0600 2010","body":"Wait for the message.","id":277226443,"type":"TextMessage","user":{"name":"Blake Smith","created_at":"Tue Nov 09 21:47:24 -0600 2010","admin":true,"id":734581,"type":"Member","email_address":"bsmith@groupon.com"}}
@@ -0,0 +1 @@
1
+ {"room_id":73541,"created_at":"2010/12/14 20:48:30 +0000","body":"Line one\\n\\tpoint one\\n\\tpoint two\\npoint three","id":289383470,"user_id":703609,"type":"PasteMessage"}
@@ -0,0 +1 @@
1
+ {"room":{"name":"Room 1","created_at":"2010/11/10 03:47:24 +0000","full":false,"updated_at":"2010/11/10 03:47:24 +0000","topic":null,"open_to_guests":false,"id":347348,"users":[{"type":"Member","created_at":"2010/11/10 03:47:24 +0000","admin":true,"email_address":"bsmith@groupon.com","id":734581,"name":"Blake Smith"}],"membership_limit":4,"locked":false}}
@@ -0,0 +1 @@
1
+ {"room":{"name":"Room 1","topic":"some topic"}}
@@ -0,0 +1 @@
1
+ {"rooms":[{"name":"Room 1","created_at":"2010/11/10 03:47:24 +0000","updated_at":"2010/11/10 03:47:24 +0000","topic":"some topic","id":347348,"membership_limit":4,"locked":false}]}
@@ -0,0 +1 @@
1
+ {"message":{"type":"TextMessage","room_id":347348,"created_at":"2010/11/23 13:15:49 +0000","body":"Hello there","id":282030462,"user_id":734581}}
@@ -0,0 +1 @@
1
+ {"room_id":73541,"created_at":"2010/12/14 20:09:23 +0000","body":"yep","id":289361911,"user_id":489198,"type":"TextMessage"}
@@ -0,0 +1,13 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), "../lib")
2
+
3
+ require 'flamethrower'
4
+ require 'server/mock_server'
5
+ require 'json'
6
+ require 'fakeweb'
7
+
8
+ ::FLAMETHROWER_LOGGER = Logger.new("/dev/null") unless Object.const_defined?("FLAMETHROWER_LOGGER")
9
+
10
+ def json_fixture(name)
11
+ file = File.join(File.dirname(__FILE__), "fixtures/#{name}.json")
12
+ File.read(file)
13
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Campfire::Connection do
4
+ before do
5
+ @server = Flamethrower::MockServer.new
6
+ @connection = @server.campfire_connection
7
+ FakeWeb.allow_net_connect = false
8
+ end
9
+
10
+ describe "#rooms" do
11
+ it "retrieves a list of rooms from JSON" do
12
+ FakeWeb.register_uri(:get, "https://mytoken:x@mydomain.campfirenow.com/rooms.json", :body => json_fixture("rooms"), :status => ["200", "OK"])
13
+ room = @connection.rooms.first
14
+ room.number.should == 347348
15
+ room.name.should == "Room 1"
16
+ room.topic.should == "some topic"
17
+ end
18
+
19
+ it "makes the http request with a token in basic auth" do
20
+ FakeWeb.register_uri(:get, "https://mytoken:x@mydomain.campfirenow.com/rooms.json", :body => json_fixture("rooms"), :status => ["200", "OK"])
21
+ @connection.rooms
22
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64("#{@connection.token}:x").chomp}"
23
+ end
24
+
25
+ it "returns empty set if not a successful response" do
26
+ FakeWeb.register_uri(:get, "https://mytoken:x@mydomain.campfirenow.com/rooms.json", :status => ["400", "Bad Request"])
27
+ @connection.rooms.should == []
28
+ end
29
+
30
+ it "sends a motd error message if unable to fetch room list" do
31
+ @connection.should_receive(:campfire_get).and_raise(SocketError)
32
+ @server.should_receive(:send_message).with(@server.reply(Flamethrower::Irc::Codes::RPL_MOTD, ":ERROR: Unable to fetch room list! Check your connection?"))
33
+ @connection.rooms.should == []
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Campfire::Message do
4
+ before do
5
+ @room = Flamethrower::Campfire::Room.new("mydomain", "mytoken", {'name' => "flamethrower"})
6
+ @channel = Flamethrower::Irc::Channel.new("#flamethrower", @room)
7
+ @campfire_user = Flamethrower::Campfire::User.new('name' => "bob", 'id' => 734581)
8
+ @room.users << @campfire_user
9
+ @irc_user = @campfire_user.to_irc
10
+ @message = Flamethrower::Campfire::Message.new('user' => @campfire_user, 'room' => @room, 'body' => 'thebody', 'type' => "TextMessage")
11
+ end
12
+
13
+ it "should have the message body" do
14
+ @message.body.should == "thebody"
15
+ end
16
+
17
+ it "should have the message type" do
18
+ @message.message_type.should == "TextMessage"
19
+ end
20
+
21
+ it "initializes the status to 'pending'" do
22
+ @message.status.should == "pending"
23
+ end
24
+
25
+ describe "#to_irc" do
26
+ it "converts the message to an irc message" do
27
+ @message.to_irc.to_s.should == ":#{@irc_user.to_s} PRIVMSG #{@channel.name} :thebody"
28
+ end
29
+
30
+ it "converts a EnterMessage to a join irc message" do
31
+ json = JSON.parse(json_fixture('enter_message'))
32
+ message = Flamethrower::Campfire::Message.new(json)
33
+ message.user = @campfire_user
34
+ message.room = @room
35
+ message.to_irc.to_s.should == ":#{@irc_user.to_s} JOIN #{@channel.name}"
36
+ end
37
+
38
+ it "converts a KickMessage to a part irc message" do
39
+ json = JSON.parse(json_fixture('kick_message'))
40
+ message = Flamethrower::Campfire::Message.new(json)
41
+ message.user = @campfire_user
42
+ message.room = @room
43
+ message.to_irc.to_s.should == ":#{@irc_user.to_s} PART #{@channel.name}"
44
+ end
45
+
46
+ it "converts a LeaveMessage to a part irc message" do
47
+ json = JSON.parse(json_fixture('leave_message'))
48
+ message = Flamethrower::Campfire::Message.new(json)
49
+ message.user = @campfire_user
50
+ message.room = @room
51
+ message.to_irc.to_s.should == ":#{@irc_user.to_s} PART #{@channel.name}"
52
+ end
53
+
54
+
55
+ it "makes a PasteMessage a PRIVMSG" do
56
+ json = JSON.parse(json_fixture('paste_message'))
57
+ message = Flamethrower::Campfire::Message.new(json)
58
+ message.user = @campfire_user
59
+ message.room = @room
60
+ message.to_irc.to_s.should == ":#{@irc_user.to_s} PRIVMSG #{@channel.name} :Line one\\n\\tpoint one\\n\\tpoint two\\npoint three"
61
+ end
62
+ end
63
+
64
+ describe "#mark_delivered!" do
65
+ it "sets the status to delivered" do
66
+ @message.status.should_not == "delivered"
67
+ @message.mark_delivered!
68
+ @message.status.should == "delivered"
69
+ end
70
+ end
71
+
72
+ describe "#mark_failed!" do
73
+ it "sets the status to failed" do
74
+ @message.status.should_not == "delivered"
75
+ @message.mark_failed!
76
+ @message.status.should == "failed"
77
+ end
78
+
79
+ it "sets the retry_at to 15 seconds from now" do
80
+ Time.stub(:now).and_return(Time.parse("9:00AM"))
81
+ @message.mark_failed!
82
+ @message.retry_at.should == Time.parse("9:00:15AM")
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,296 @@
1
+ require File.join(File.dirname(__FILE__), "../../spec_helper")
2
+
3
+ describe Flamethrower::Campfire::Room do
4
+ before do
5
+ @room = Flamethrower::Campfire::Room.new("mydomain", "mytoken", "id" => 347348, "topic" => "some topic", "name" => "some name")
6
+ @user = Flamethrower::Campfire::User.new('name' => "bob", 'id' => 489198)
7
+ @user2 = Flamethrower::Campfire::User.new('name' => "bill", 'id' => 123456)
8
+ end
9
+
10
+ describe "params" do
11
+ it "has number" do
12
+ @room.number.should == 347348
13
+ end
14
+
15
+ it "has name" do
16
+ @room.name.should == "some name"
17
+ end
18
+ end
19
+
20
+ describe "#topic" do
21
+ it "has topic" do
22
+ @room.topic.should == "some topic"
23
+ end
24
+
25
+ it "displays 'No topic' if the topic is nil" do
26
+ @room.topic = nil
27
+ @room.topic.should == "No topic"
28
+ end
29
+ end
30
+
31
+ describe "#send_topic!" do
32
+ it "sets the topic when the campfire API returns 200" do
33
+ FakeWeb.register_uri(:put, "https://mytoken:x@mydomain.campfirenow.com/room/347348.json", :body => json_fixture("room_update"), :status => ["200", "OK"])
34
+ @room.send_topic!("some updated topic")
35
+ @room.topic.should == "some updated topic"
36
+ end
37
+
38
+ it "keeps the previous topic when the campfire API returns non 200" do
39
+ FakeWeb.register_uri(:put, "https://mytoken:x@mydomain.campfirenow.com/room/347348.json", :body => json_fixture("room_update"), :status => ["400", "Bad Request"])
40
+ @room.instance_variable_set("@topic", "some old topic")
41
+ @room.send_topic!("some updated topic")
42
+ @room.topic.should == "some old topic"
43
+ end
44
+ end
45
+
46
+ describe "#fetch_room_info" do
47
+ before do
48
+ FakeWeb.register_uri(:get, "https://mytoken:x@mydomain.campfirenow.com/room/347348.json", :body => json_fixture("room"), :status => ["200", "OK"])
49
+ end
50
+
51
+ it "retrieves a list of users and stores them as user objects" do
52
+ @room.fetch_room_info
53
+ @room.users.all? {|u| u.is_a?(Flamethrower::Campfire::User)}.should be_true
54
+ end
55
+
56
+ it "makes the http request with a token in basic auth" do
57
+ @room.fetch_room_info
58
+ FakeWeb.last_request['authorization'].should == "Basic #{Base64::encode64("#{@room.token}:x").chomp}"
59
+ end
60
+
61
+ end
62
+
63
+ describe "#join" do
64
+ it "returns true when posting to the room join call succeeds" do
65
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/join.json", :status => ["200", "OK"])
66
+ @room.join.should == true
67
+ end
68
+
69
+ it "returns false when posting to the room join call fails" do
70
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/join.json", :status => ["400", "Bad Request"])
71
+ @room.join.should == false
72
+ end
73
+ end
74
+
75
+ describe "#connect" do
76
+ it "initializes the twitter jsonstream with the right options" do
77
+ Twitter::JSONStream.should_receive(:connect).with(:path => "/room/347348/live.json", :host => "streaming.campfirenow.com", :auth => "mytoken:x")
78
+ @room.connect
79
+ end
80
+ end
81
+
82
+ describe "#thread" do
83
+ before do
84
+ Kernel.stub(:sleep)
85
+ end
86
+
87
+ context "in a Thread" do
88
+ it "calls connect at the start of the thread" do
89
+ @room.stub(:fetch_messages)
90
+ @room.stub(:post_messages)
91
+ @room.should_receive(:connect).at_least(1).times
92
+ @room.start_thread
93
+ @room.kill_thread!
94
+ end
95
+
96
+ it "fetches messages from the stream" do
97
+ @room.stub(:connect)
98
+ @room.stub(:post_messages)
99
+ @room.should_receive(:fetch_messages).at_least(1).times
100
+ @room.start_thread
101
+ @room.kill_thread!
102
+ end
103
+
104
+ it "posts messages to the campfire API" do
105
+ @room.stub(:connect)
106
+ @room.stub(:fetch_messages)
107
+ @room.should_receive(:post_messages).at_least(1).times
108
+ @room.start_thread
109
+ @room.kill_thread!
110
+ end
111
+
112
+ end
113
+ end
114
+
115
+ describe "#fetch_messages" do
116
+ before do
117
+ Twitter::JSONStream.stub(:connect).and_return("stream")
118
+ item = json_fixture("streaming_message")
119
+ @room.connect
120
+ @room.stream.stub(:each_item).and_yield(item)
121
+ end
122
+
123
+ it "iterates over each stream item and sends to the message queue" do
124
+ @room.fetch_messages
125
+ @room.inbound_messages.size.should == 1
126
+ end
127
+
128
+ it "maps the message body to a message object with the right body" do
129
+ @room.fetch_messages
130
+ @room.inbound_messages.pop.body.should == "yep"
131
+ end
132
+
133
+ it "maps the message sender to the right user" do
134
+ @room.users << @user2
135
+ @room.users << @user
136
+ @room.fetch_messages
137
+ @room.inbound_messages.pop.user.should == @user
138
+ end
139
+
140
+ it "maps the message room to the right room" do
141
+ @room.fetch_messages
142
+ @room.inbound_messages.pop.room.should == @room
143
+ end
144
+ end
145
+
146
+ describe "#say" do
147
+ it "queues a campfire message given a message body" do
148
+ message = Flamethrower::Campfire::Message.new('body' => 'Hello there', 'user' => @user, 'room' => @room)
149
+ @room.say('Hello there')
150
+ popped_message = @room.outbound_messages.pop
151
+ popped_message.body.should == 'Hello there'
152
+ end
153
+
154
+ it "takes an optional message type" do
155
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
156
+ @room.say('Hello there', 'TextMessage')
157
+ popped_message = @room.outbound_messages.pop
158
+ popped_message.message_type.should == 'TextMessage'
159
+ end
160
+ end
161
+
162
+ describe "#requeue_failed_messages" do
163
+ it "queues a message whos retry_at is greater than now" do
164
+ Time.stub(:now).and_return(Time.parse("9:00AM"))
165
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
166
+ message.retry_at = Time.parse("9:00:01AM")
167
+ @room.failed_messages << message
168
+ @room.requeue_failed_messages
169
+ @room.outbound_messages.size.should == 1
170
+ @room.failed_messages.size.should == 0
171
+ end
172
+
173
+ it "doesn't queue a message whos retry_at is less than now" do
174
+ Time.stub(:now).and_return(Time.parse("9:00AM"))
175
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
176
+ message.retry_at = Time.parse("8:59AM")
177
+ @room.failed_messages << message
178
+ @room.requeue_failed_messages
179
+ @room.outbound_messages.size.should == 0
180
+ @room.failed_messages.size.should == 1
181
+ end
182
+ end
183
+
184
+ describe "#post_messages" do
185
+ it "pops the message off the queue and posts it to the campfire api" do
186
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/speak.json", :body => json_fixture("speak_message"), :status => ["201", "Created"])
187
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
188
+ @room.outbound_messages << message
189
+ @room.post_messages
190
+ @room.outbound_messages.size.should == 0
191
+ end
192
+
193
+ it "adds the message to the failed_messages array if it fails to post to the campfire API" do
194
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/speak.json", :status => ["400", "Bad Request"])
195
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
196
+ @room.outbound_messages << message
197
+ @room.post_messages
198
+ @room.outbound_messages.size.should == 0
199
+ @room.failed_messages.size.should == 1
200
+ end
201
+
202
+ it "marks the message as failed when not able to deliver" do
203
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/speak.json", :status => ["400", "Bad Request"])
204
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
205
+ @room.outbound_messages << message
206
+ @room.post_messages
207
+ message.status.should == "failed"
208
+ end
209
+
210
+ it "marks the message as delivered if successfully posted to campfire" do
211
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/speak.json", :body => json_fixture("speak_message"), :status => ["201", "Created"])
212
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
213
+ @room.outbound_messages << message
214
+ @room.post_messages
215
+ message.status.should == "delivered"
216
+ end
217
+
218
+ it "sends the right json" do
219
+ FakeWeb.register_uri(:post, "https://mytoken:x@mydomain.campfirenow.com/room/347348/speak.json", :body => json_fixture("speak_message"), :status => ["201", "Created"])
220
+ message = Flamethrower::Campfire::Message.new('type' => 'TextMessage', 'body' => 'Hello there', 'user' => @user, 'room' => @room)
221
+ @room.outbound_messages << message
222
+ expected_json = {"message"=>{"body"=>"Hello there", "type"=>"TextMessage"}}.to_json
223
+ @room.should_receive(:campfire_post).with("/room/#{@room.number}/speak.json", expected_json)
224
+ @room.post_messages
225
+ end
226
+ end
227
+
228
+ describe "#retrieve_messages" do
229
+ it "returns all the messages in the message buffer" do
230
+ @room.inbound_messages << "one"
231
+ @room.inbound_messages << "two"
232
+ @room.retrieve_messages.should == ["one", "two"]
233
+ end
234
+
235
+ it "pops the messages from the messages array" do
236
+ @room.inbound_messages << "one"
237
+ @room.retrieve_messages.should == ["one"]
238
+ @room.inbound_messages.size.should == 0
239
+ end
240
+ end
241
+
242
+ describe "#to_irc" do
243
+ it "returns an irc channel object" do
244
+ @room.to_irc.is_a?(Flamethrower::Irc::Channel).should be_true
245
+ end
246
+
247
+ it "references the same room when doing conversions" do
248
+ @room.to_irc.to_campfire.should == @room
249
+ end
250
+
251
+ it "sends the current campfire topic" do
252
+ @room.to_irc.topic.should == @room.topic
253
+ end
254
+
255
+ it "replaces newlines with spaces" do
256
+ @room.topic = "Some topic\nyeah"
257
+ @room.to_irc.topic.should == "Some topic yeah"
258
+ end
259
+
260
+ context "channel name" do
261
+ it "maps the campfire room name to the channel name" do
262
+ @room.name = "somename"
263
+ @room.to_irc.name.should == "#somename"
264
+ end
265
+
266
+ it "downcases channel names" do
267
+ @room.name = "Somename"
268
+ @room.to_irc.name.should == "#somename"
269
+ end
270
+
271
+ it "separates replaces name spaces with underscores" do
272
+ @room.name = "Some Name 1"
273
+ @room.to_irc.name.should == "#some_name_1"
274
+ end
275
+
276
+ it "replaces slashes with underscores" do
277
+ @room.name = "API/Mobile"
278
+ @room.to_irc.name.should == "#api_mobile"
279
+ end
280
+
281
+ it "replaces ampersands with underscores" do
282
+ @room.name = "Stuff & Something"
283
+ @room.to_irc.name.should == "#stuff_something"
284
+ end
285
+
286
+ end
287
+
288
+ context "populating users" do
289
+ it "fills the irc channel's user array with mapped campfire users" do
290
+ @room.users = [Flamethrower::Campfire::User.new('name' => "bob")]
291
+ @room.to_irc.users.first.is_a?(Flamethrower::Irc::User).should be_true
292
+ end
293
+ end
294
+
295
+ end
296
+ end