scamp 1.2.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,33 +1,18 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Scamp do
4
- before do
5
- @valid_params = {:api_key => "6124d98749365e3db2c9e5b27ca04db6", :subdomain => "oxygen"}
6
- @valid_user_cache_data = {123 => {"name" => "foo"}, 456 => {"name" => "bar"}, 'me' => {"name" => "bot", "id" => 123}}
7
-
8
- # Stub fetch for room data
9
- @valid_room_cache_data = {
10
- 123 => {
11
- "id" => 123,
12
- "name" => "foo",
13
- "users" => []
14
- },
15
- 456 => {
16
- "id" => 456,
17
- "name" => "bar",
18
- "users" => []
19
- }
20
- }
21
- end
22
-
23
4
  describe "#initialize" do
24
5
  it "should work with valid params" do
25
- a(Scamp).should be_a(Scamp)
6
+ Scamp.new do |scamp|
7
+ scamp.should be_a(Scamp)
8
+ end
26
9
  end
10
+
27
11
  it "should warn if given an option it doesn't know" do
28
12
  mock_logger
29
13
 
30
- a(Scamp, :fred => "estaire").should be_a(Scamp)
14
+ Scamp.new :fred => "estaire" do |scamp|
15
+ end
31
16
 
32
17
  logger_output.should =~ /WARN.*Scamp initialized with :fred => "estaire" but NO UNDERSTAND!/
33
18
  end
@@ -35,716 +20,191 @@ describe Scamp do
35
20
 
36
21
  describe "#verbose" do
37
22
  it "should default to false" do
38
- a(Scamp).verbose.should be_false
23
+ Scamp.new do |scamp|
24
+ scamp.verbose.should be_false
25
+ end
39
26
  end
27
+
40
28
  it "should be overridable at initialization" do
41
- a(Scamp, :verbose => true).verbose.should be_true
29
+ Scamp.new :verbose => true do |scamp|
30
+ scamp.verbose.should be_true
31
+ end
42
32
  end
43
33
  end
44
34
 
45
35
  describe "#logger" do
46
36
  context "default logger" do
47
- before { @bot = a Scamp }
48
- it { @bot.logger.should be_a(Logger) }
49
- it { @bot.logger.level.should be == Logger::INFO }
37
+ before do
38
+ Scamp.new do |scamp|
39
+ @bot = scamp
40
+ end
41
+ end
42
+
43
+ it "has a log level of INFO" do
44
+ @bot.logger.should be_a(Logger)
45
+ @bot.logger.level.should be == Logger::INFO
46
+ end
50
47
  end
48
+
51
49
  context "default logger in verbose mode" do
52
- before { @bot = a Scamp, :verbose => true }
53
- it { @bot.logger.level.should be == Logger::DEBUG }
50
+ before do
51
+ Scamp.new :verbose => true do |scamp|
52
+ @bot = scamp
53
+ end
54
+ end
55
+
56
+ it "has a log level of DEBUG" do
57
+ @bot.logger.level.should be == Logger::DEBUG
58
+ end
54
59
  end
60
+
55
61
  context "overriding default" do
56
62
  before do
57
63
  @custom_logger = Logger.new("/dev/null")
58
- @bot = a Scamp, :logger => @custom_logger
64
+ Scamp.new :logger => @custom_logger do |scamp|
65
+ @bot = scamp
66
+ end
67
+ end
68
+
69
+ it "uses the custom logger provided" do
70
+ @bot.logger.should be == @custom_logger
59
71
  end
60
- it { @bot.logger.should be == @custom_logger }
61
72
  end
62
73
  end
63
74
 
64
75
  describe "#first_match_only" do
65
76
  it "should default to false" do
66
- a(Scamp).first_match_only.should be_false
77
+ Scamp.new do |scamp|
78
+ scamp.first_match_only.should be_false
79
+ end
67
80
  end
68
81
  it "should be settable" do
69
- a(Scamp, :first_match_only => true).first_match_only.should be_true
70
- end
71
- end
72
-
73
- describe "private methods" do
74
-
75
- describe "#process_message" do
76
- before do
77
- @bot = a Scamp
78
- $attempts = 0 # Yes, I hate it too. Works though.
79
- @message = {:body => "my message here"}
80
-
81
- @bot.behaviour do
82
- 2.times { match(/.*/) { $attempts += 1 } }
83
- end
84
- end
85
- after { $attempts = nil }
86
- context "with first_match_only not set" do
87
- before { @bot.first_match_only.should be_false }
88
- it "should process all matchers which attempt the message" do
89
- @bot.send(:process_message, @message)
90
- $attempts.should be == 2
91
- end
92
- end
93
- context "with first_match_only set" do
94
- before do
95
- @bot.first_match_only = true
96
- @bot.first_match_only.should be_true
97
- end
98
- it "should only process the first matcher which attempts the message" do
99
- @bot.send(:process_message, @message)
100
- $attempts.should be == 1
101
- end
82
+ Scamp.new :first_match_only => true do |scamp|
83
+ scamp.first_match_only.should be_true
102
84
  end
103
85
  end
104
86
  end
105
-
106
- describe "matching" do
107
-
108
- context "with conditions" do
109
- context "for room" do
110
-
111
- it "should limit matches by id" do
112
- canary = mock
113
- canary.expects(:lives).once
114
- canary.expects(:bang).never
115
-
116
- bot = a Scamp
117
- bot.behaviour do
118
- match("a string", :conditions => {:room => 123}) {canary.lives}
119
- match("a string", :conditions => {:room => 456}) {canary.bang}
120
- end
121
-
122
- bot.send(:process_message, {:room_id => 123, :body => "a string"})
123
- end
124
87
 
125
- it "should limit matches by array of IDs" do
126
- canary = mock
127
- canary.expects(:lives).once
128
- canary.expects(:bang).never
129
-
130
- bot = a Scamp
131
- bot.behaviour do
132
- match("a string", :conditions => {:room => [123]}) {canary.lives}
133
- match("a string", :conditions => {:room => [456]}) {canary.bang}
134
- end
135
-
136
- bot.room_cache = @valid_room_cache_data
137
-
138
- bot.send(:process_message, {:room_id => 123, :body => "a string"})
139
- end
140
-
141
- it "should limit matches by array in complex form" do
142
- canary = mock
143
- canary.expects(:lives).once
144
- canary.expects(:bang).never
145
-
146
- bot = a Scamp
147
- bot.behaviour do
148
- match("a string", :conditions => {:rooms => ["foo", 777]}) {canary.lives}
149
- match("a string", :conditions => {:room => ["bar"]}) {canary.bang}
150
- end
151
-
152
- bot.room_cache = @valid_room_cache_data
153
- bot.send(:process_message, {:room_id => 123, :body => "a string"})
154
- end
155
-
156
- it "should limit matches by name" do
157
- canary = mock
158
- canary.expects(:lives).once
159
- canary.expects(:bang).never
160
-
161
- bot = a Scamp
162
- bot.behaviour do
163
- match("a string", :conditions => {:room => "foo"}) {canary.lives}
164
- match("a string", :conditions => {:room => "bar"}) {canary.bang}
165
- end
166
-
167
- bot.room_cache = @valid_room_cache_data
168
-
169
- bot.send(:process_message, {:room_id => 123, :body => "a string"})
170
- end
171
-
172
- end
173
-
174
- it "should limit matches by user id" do
175
- canary = mock
176
- canary.expects(:lives).once
177
- canary.expects(:bang).never
178
-
179
- bot = a Scamp
180
- bot.behaviour do
181
- match("a string", :conditions => {:user => 123}) {canary.lives}
182
- match("a string", :conditions => {:user => 456}) {canary.bang}
183
- end
184
-
185
- bot.send(:process_message, {:user_id => 123, :body => "a string"})
186
- end
187
-
188
- it "should limit matches by user name" do
189
- canary = mock
190
- canary.expects(:lives).once
191
- canary.expects(:bang).never
192
-
193
- bot = a Scamp
194
- bot.behaviour do
195
- match("a string", :conditions => {:user => "foo"}) {canary.lives}
196
- match("a string", :conditions => {:user => "bar"}) {canary.bang}
197
- end
198
-
199
- bot.user_cache = @valid_user_cache_data
200
-
201
- bot.send(:process_message, {:user_id => 123, :body => "a string"})
202
- end
203
-
204
- it "should limit matches by room and user" do
205
- canary = mock
206
- canary.expects(:lives).once
207
- canary.expects(:bang).never
208
-
209
- bot = a Scamp
210
- bot.behaviour do
211
- match("a string", :conditions => {:room => 123, :user => 123}) {canary.lives}
212
- match("a string", :conditions => {:room => 456, :user => 456}) {canary.bang}
213
- end
214
-
215
- bot.room_cache = @valid_room_cache_data
216
- bot.user_cache = @valid_user_cache_data
217
- bot.send(:process_message, {:room_id => 123, :user_id => 123, :body => "a string"})
218
- bot.send(:process_message, {:room_id => 123, :user_id => 456, :body => "a string"})
219
- bot.send(:process_message, {:room_id => 456, :user_id => 123, :body => "a string"})
220
- end
221
-
222
- it "should ignore itself if so requested" do
223
- canary = mock
224
- canary.expects(:bang).never
225
-
226
- bot = a Scamp
227
- bot.user_cache = @valid_user_cache_data
228
- bot.ignore_self = true
229
- bot.ignore_self.should be_true
230
- bot.behaviour do
231
- match("a string") {canary.bang}
232
- end
233
-
234
- bot.send(:process_message, {:user_id => 123, :body => "a string"})
235
- end
236
- end
237
-
88
+ describe "matching" do
238
89
  describe "strings" do
239
90
  it "should match an exact string" do
240
91
  canary = mock
241
92
  canary.expects(:lives).once
242
93
  canary.expects(:bang).never
243
-
244
- bot = a Scamp
245
- bot.behaviour do
246
- match("a string") {canary.lives}
247
- match("another string") {canary.bang}
248
- match("a string like no other") {canary.bang}
249
- end
250
-
251
- bot.send(:process_message, {:body => "a string"})
252
- end
253
-
254
- it "should not match without prefix when required_prefix is true" do
255
- canary = mock
256
- canary.expects(:bang).never
257
-
258
- bot = a Scamp, :required_prefix => 'Bot: '
259
- bot.behaviour do
260
- match("a string") {canary.bang}
261
- end
262
-
263
- bot.send(:process_message, {:body => "a string"})
264
- end
265
94
 
266
- it "should match with exact prefix when required_prefix is true" do
267
- canary = mock
268
- canary.expects(:lives).once
269
-
270
- bot = a Scamp, :required_prefix => 'Bot: '
271
- bot.behaviour do
272
- match("a string") {canary.lives}
95
+ EM.run_block do
96
+ Scamp.new do |bot|
97
+ bot.adapter :rspec, RspecAdapter
98
+
99
+ bot.match("a string") {|context, message| canary.lives }
100
+ bot.match("another string") {|context, message| canary.bang }
101
+ bot.match("a string like no other") {|context, message| canary.bang }
102
+
103
+ bot.connect!
104
+ bot.adapters[:rspec][:adapter].speak! "a string"
105
+ end
273
106
  end
274
-
275
- bot.send(:process_message, {:body => "Bot: a string"})
276
107
  end
277
108
  end
278
-
109
+
279
110
 
280
111
  describe "regexes" do
281
112
  it "should match a regex" do
282
113
  canary = mock
283
114
  canary.expects(:ping).twice
284
-
285
- bot = a Scamp
286
- bot.behaviour do
287
- match /foo/ do
288
- canary.ping
115
+ EM.run_block do
116
+ Scamp.new do |bot|
117
+ bot.adapter :rspec, RspecAdapter
118
+
119
+ bot.match /foo/ do
120
+ canary.ping
121
+ end
122
+
123
+ bot.connect!
124
+ bot.adapters[:rspec][:adapter].speak! "something foo other thing"
125
+ bot.adapters[:rspec][:adapter].speak! "foomaster"
289
126
  end
290
127
  end
291
-
292
- bot.send(:process_message, {:body => "something foo other thing"})
293
- bot.send(:process_message, {:body => "foomaster"})
294
128
  end
295
-
296
- it "should make named captures vailable as methods" do
129
+
130
+ it "should make named captures available on the message" do
297
131
  canary = mock
298
132
  canary.expects(:one).with("first")
299
133
  canary.expects(:two).with("the rest of it")
300
-
301
- bot = a Scamp
302
- bot.behaviour do
303
- match /^please match (?<yousaidthis>\w+) and (?<andthis>.+)$/ do
304
- canary.one(yousaidthis)
305
- canary.two(andthis)
134
+
135
+ EM.run_block do
136
+ Scamp.new do |bot|
137
+ bot.adapter :rspec, RspecAdapter
138
+
139
+ bot.match /^please match (?<yousaidthis>\w+) and (?<andthis>.+)$/ do |channel, msg|
140
+ canary.one(msg.matches.yousaidthis)
141
+ canary.two(msg.matches.andthis)
142
+ end
143
+
144
+ bot.connect!
145
+ bot.adapters[:rspec][:adapter].speak! "please match first and the rest of it"
306
146
  end
307
147
  end
308
-
309
- bot.send(:process_message, {:body => "please match first and the rest of it"})
310
148
  end
311
-
149
+
312
150
  it "should make matches available in an array" do
313
151
  canary = mock
314
152
  canary.expects(:one).with("first")
315
153
  canary.expects(:two).with("the rest of it")
316
-
317
- bot = a Scamp
318
- bot.behaviour do
319
- match /^please match (\w+) and (.+)$/ do
320
- canary.one(matches[0])
321
- canary.two(matches[1])
322
- end
323
- end
324
-
325
- bot.send(:process_message, {:body => "please match first and the rest of it"})
326
- end
327
-
328
- it "should not match without prefix when required_prefix is present" do
329
- canary = mock
330
- canary.expects(:bang).never
331
-
332
- bot = a Scamp, :required_prefix => /^Bot[\:,\s]+/i
333
- bot.behaviour do
334
- match(/a string/) {canary.bang}
335
- end
336
-
337
- bot.send(:process_message, {:body => "a string"})
338
- bot.send(:process_message, {:body => "some kind of a string"})
339
- bot.send(:process_message, {:body => "a string!!!"})
340
- end
341
154
 
342
- it "should match with regex prefix when required_prefix is present" do
343
- canary = mock
344
- canary.expects(:lives).times(4)
345
-
346
- bot = a Scamp, :required_prefix => /^Bot\W{1,2}/i
347
- bot.behaviour do
348
- match(/a string/) {canary.lives}
155
+ EM.run_block do
156
+ Scamp.new do |bot|
157
+ bot.adapter :rspec, RspecAdapter
158
+
159
+ bot.match /^please match (\w+) and (.+)$/ do |channel, msg|
160
+ canary.one(msg.matches[0])
161
+ canary.two(msg.matches[1])
162
+ end
163
+
164
+ bot.connect!
165
+ bot.adapters[:rspec][:adapter].speak! "please match first and the rest of it"
166
+ end
349
167
  end
350
-
351
- bot.send(:process_message, {:body => "Bot, a string"})
352
- bot.send(:process_message, {:body => "Bot a string"})
353
- bot.send(:process_message, {:body => "bot: a string"})
354
- bot.send(:process_message, {:body => "Bot: a string oh my!"})
355
168
  end
356
169
  end
357
170
  end
358
-
359
- describe "match block" do
360
- it "should make the room details available to the action block" do
361
- canary = mock
362
- canary.expects(:id).with(123)
363
- canary.expects(:name).with(@valid_room_cache_data[123]["name"])
364
-
365
- bot = a Scamp
366
- bot.behaviour do
367
- match("a string") {
368
- canary.id(room_id)
369
- canary.name(room)
370
- }
371
- end
372
-
373
- bot.room_cache = @valid_room_cache_data
374
- bot.send(:process_message, {:room_id => 123, :body => "a string"})
375
- end
376
-
377
- it "should make the speaking user details available to the action block" do
378
- canary = mock
379
- canary.expects(:id).with(123)
380
- canary.expects(:name).with(@valid_user_cache_data[123]["name"])
381
-
382
- bot = a Scamp
383
- bot.behaviour do
384
- match("a string") {
385
- canary.id(user_id)
386
- canary.name(user)
387
- }
388
- end
389
-
390
- bot.user_cache = @valid_user_cache_data
391
- bot.send(:process_message, {:user_id => 123, :body => "a string"})
392
- end
393
-
394
- it "should make the message said available to the action block" do
395
- canary = mock
396
- canary.expects(:message).with("Hello world")
397
-
398
- bot = a Scamp
399
- bot.behaviour do
400
- match("Hello world") {
401
- canary.message(message)
402
- }
403
- end
404
-
405
- bot.send(:process_message, {:body => "Hello world"})
406
- end
407
-
408
- it "should provide a command list" do
409
- canary = mock
410
- canary.expects(:commands).with([["Hello world", {}], ["Hello other world", {:room=>123}], [/match me/, {:user=>123}]])
411
-
412
- bot = a Scamp
413
- bot.behaviour do
414
- match("Hello world") {
415
- canary.commands(command_list)
416
- }
417
- match("Hello other world", :conditions => {:room => 123}) {}
418
- match(/match me/, :conditions => {:user => 123}) {}
419
- end
420
-
421
- bot.send(:process_message, {:body => "Hello world"})
422
- end
423
-
424
- it "should be able to play a sound to the room the action was triggered in" do
425
- bot = a Scamp
426
- bot.behaviour do
427
- match("Hello world") {
428
- play "yeah"
429
- }
430
- end
431
-
432
- EM.run_block {
433
- room_id = 123
434
- stub_request(:post, "https://#{@valid_params[:subdomain]}.campfirenow.com/room/#{room_id}/speak.json").
435
- with(
436
- :body => "{\"message\":{\"body\":\"yeah\",\"type\":\"SoundMessage\"}}",
437
- :headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}
438
- )
439
-
440
- bot.send(:process_message, {:room_id => room_id, :body => "Hello world"})
441
- }
442
- end
443
-
444
- it "should be able to play a sound to an arbitrary room" do
445
- play_room = 456
446
-
447
- bot = a Scamp
448
- bot.behaviour do
449
- match("Hello world") {
450
- play "yeah", play_room
451
- }
452
- end
453
-
454
- EM.run_block {
455
- room_id = 123
456
- stub_request(:post, "https://#{@valid_params[:subdomain]}.campfirenow.com/room/#{play_room}/speak.json").
457
- with(
458
- :body => "{\"message\":{\"body\":\"yeah\",\"type\":\"SoundMessage\"}}",
459
- :headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}
460
- )
461
-
462
- bot.send(:process_message, {:room_id => room_id, :body => "Hello world"})
463
- }
464
- end
465
-
466
- it "should be able to say a message to the room the action was triggered in" do
467
- bot = a Scamp
468
- bot.behaviour do
469
- match("Hello world") {
470
- say "yeah"
471
- }
472
- end
473
-
474
- EM.run_block {
475
- room_id = 123
476
- stub_request(:post, "https://#{@valid_params[:subdomain]}.campfirenow.com/room/#{room_id}/speak.json").
477
- with(
478
- :body => "{\"message\":{\"body\":\"yeah\",\"type\":\"Textmessage\"}}",
479
- :headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}
480
- )
481
-
482
- bot.send(:process_message, {:room_id => room_id, :body => "Hello world"})
483
- }
484
- end
485
-
486
- it "should be able to say a message to an arbitrary room" do
487
- play_room = 456
488
-
489
- bot = a Scamp
490
- bot.behaviour do
491
- match("Hello world") {
492
- say "yeah", play_room
493
- }
494
- end
495
-
496
- EM.run_block {
497
- room_id = 123
498
- stub_request(:post, "https://#{@valid_params[:subdomain]}.campfirenow.com/room/#{play_room}/speak.json").
499
- with(
500
- :body => "{\"message\":{\"body\":\"yeah\",\"type\":\"Textmessage\"}}",
501
- :headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}
502
- )
503
-
504
- bot.send(:process_message, {:room_id => room_id, :body => "Hello world"})
505
- }
506
- end
507
- end
508
171
 
509
- describe "API" do
510
- context "user operations" do
511
- it "should fetch user data" do
512
- bot = a Scamp
513
-
514
- EM.run_block {
515
- stub_request(:get, "https://#{@valid_params[:subdomain]}.campfirenow.com/users/123.json").
516
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}).
517
- to_return(:status => 200, :body => Yajl::Encoder.encode(:user => @valid_user_cache_data[123]), :headers => {})
518
- bot.username_for(123)
519
- }
520
- end
521
-
522
- it "should handle HTTP errors fetching user data" do
523
- mock_logger
524
- bot = a Scamp
525
-
526
- url = "https://#{@valid_params[:subdomain]}.campfirenow.com/users/123.json"
527
- EM.run_block {
528
- stub_request(:get, url).
529
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}).
530
- to_return(:status => 502, :body => "", :headers => {'Content-Type'=>'text/html'})
531
- lambda {bot.username_for(123)}.should_not raise_error
532
- }
533
- logger_output.should =~ /ERROR.*Couldn't fetch user data for user 123 with url #{url}, http response from API was 502/
534
- end
535
-
536
- it "should handle network errors fetching user data" do
537
- mock_logger
538
- bot = a Scamp
539
-
540
- url = "https://#{@valid_params[:subdomain]}.campfirenow.com/users/123.json"
541
- EM.run_block {
542
- stub_request(:get, url).
543
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}).to_timeout
544
- lambda {bot.username_for(123)}.should_not raise_error
545
- }
546
- logger_output.should =~ /ERROR.*Couldn't connect to #{url} to fetch user data for user 123/
172
+ describe ".adapter" do
173
+ let(:adapter) { mock }
174
+ before do
175
+ RspecAdapter.stubs(:new) do
176
+ adapter
547
177
  end
548
178
  end
549
-
550
- context "room operations" do
551
- before do
552
- @room_list_url = "https://#{@valid_params[:subdomain]}.campfirenow.com/rooms.json"
553
- @me_list_url = "https://#{@valid_params[:subdomain]}.campfirenow.com/users/me.json"
554
- @room_url = "https://#{@valid_params[:subdomain]}.campfirenow.com/room/123.json"
555
- @stream_url = "https://streaming.campfirenow.com/room/123/live.json"
556
- end
557
-
558
- it "should fetch a room list" do
559
- mock_logger
560
- bot = a Scamp
561
-
562
- EM.run_block {
563
- stub_request(:get, @room_list_url).
564
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).
565
- to_return(:status => 200, :body => Yajl::Encoder.encode(:rooms => @valid_room_cache_data.values), :headers => {})
566
- bot.send(:populate_room_list)
567
- }
568
- logger_output.should =~ /DEBUG.*Fetched room list/
569
- end
570
-
571
- it "should invoke the post connection callback" do
572
- mock_logger
573
- bot = a Scamp
574
-
575
- invoked_cb = false
576
-
577
- EM.run_block {
578
- stub_request(:get, @room_list_url).
579
- with(:headers => {
580
- 'Authorization'=>[@valid_params[:api_key], 'X'],
581
- 'Content-Type' => 'application/json'
582
- }).
583
- to_return(:status => 200, :body => Yajl::Encoder.encode(:rooms => @valid_room_cache_data.values), :headers => {})
584
-
585
- stub_request(:get, @room_list_url).
586
- with(:headers => {
587
- 'Authorization'=>[@valid_params[:api_key], 'X']
588
- }).
589
- to_return(:status => 200, :body => Yajl::Encoder.encode(:rooms => @valid_room_cache_data.values), :headers => {})
590
179
 
591
- # Disable fetch_data_for, not important to this test.
592
- Scamp.any_instance.expects(:fetch_data_for).returns(nil)
180
+ it "subscribes to an adapter" do
181
+ adapter.expects(:subscribe).once
182
+ RspecAdapter.stubs(:new).returns adapter
593
183
 
594
- bot.send(:connect!, [@valid_room_cache_data.keys.first]) do
595
- invoked_cb = true
596
- end
597
- }
598
- invoked_cb.should be_true
599
- end
600
-
601
- it "should handle HTTP errors fetching the room list" do
602
- mock_logger
603
- bot = a Scamp
604
-
605
- EM.run_block {
606
- # stub_request(:get, url).
607
- # with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type'=>'application/json'}).
608
- # to_return(:status => 502, :body => "", :headers => {'Content-Type'=>'text/html'})
609
- stub_request(:get, @room_list_url).
610
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).
611
- to_return(:status => 502, :body => "", :headers => {'Content-Type'=>'text/html'})
612
- lambda {bot.send(:populate_room_list)}.should_not raise_error
613
- }
614
- logger_output.should =~ /ERROR.*Couldn't fetch room list with url #{@room_list_url}, http response from API was 502/
615
- end
616
-
617
- it "should handle network errors fetching the room list" do
618
- mock_logger
619
- bot = a Scamp
620
- EM.run_block {
621
- stub_request(:get, @room_list_url).
622
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).to_timeout
623
- lambda {bot.send(:populate_room_list)}.should_not raise_error
624
- }
625
- logger_output.should =~ /ERROR.*Couldn't connect to url #{@room_list_url} to fetch room list/
626
- end
627
-
628
- it "should fetch individual room data" do
629
- mock_logger
630
- bot = a Scamp
631
-
632
- EM.run_block {
633
- stub_request(:get, @room_url).
634
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).
635
- to_return(:status => 200, :body => Yajl::Encoder.encode(:room => @valid_room_cache_data[123]), :headers => {})
636
- bot.room_name_for(123)
637
- }
638
- logger_output.should =~ /DEBUG.*Fetched room data for 123/
639
- end
640
-
641
- it "should handle HTTP errors fetching individual room data" do
642
- mock_logger
643
- bot = a Scamp
644
-
645
- EM.run_block {
646
- stub_request(:get, @room_url).
647
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).
648
- to_return(:status => 502, :body => "", :headers => {'Content-Type'=>'text/html'})
649
- lambda {bot.room_name_for(123)}.should_not raise_error
650
- }
651
- logger_output.should =~ /ERROR.*Couldn't fetch room data for room 123 with url #{@room_url}, http response from API was 502/
652
- end
653
-
654
- it "should handle network errors fetching individual room data" do
655
- mock_logger
656
- bot = a Scamp
657
-
658
- EM.run_block {
659
- stub_request(:get, @room_url).
660
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).to_timeout
661
- lambda {bot.room_name_for(123)}.should_not raise_error
662
- }
663
- logger_output.should =~ /ERROR.*Couldn't connect to #{@room_url} to fetch room data for room 123/
664
- end
665
-
666
- it "should stream a room"
667
-
668
- it "should handle response errors streaming a room" do
669
- mock_logger
670
- bot = a Scamp
671
-
672
- EM.run_block {
673
- stub_request(:get, @stream_url).
674
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X']}).
675
- to_return(:status => 201, :body => 'foobarbaz', :headers => {})
676
- lambda { bot.send(:stream, 123) }.should_not raise_error
677
- }
678
- logger_output.should =~ /ERROR.*Couldn't parse room data for room 123 with url #{@stream_url}, http response data was foobarbaz.../
184
+ Scamp.new do |scamp|
185
+ scamp.adapter :rspec_adapter, RspecAdapter
679
186
  end
680
-
681
- it "should handle HTTP errors streaming a room"
682
187
  end
683
-
684
- context "message operations" do
685
- before do
686
- @message_post_url = "https://#{@valid_params[:subdomain]}.campfirenow.com/room/123/speak.json"
687
- end
188
+ end
688
189
 
689
- it "should send a message" do
690
- mock_logger
691
- bot = a Scamp
692
-
693
- EM.run_block {
694
- stub_request(:post, @message_post_url).
695
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type' => 'application/json'}).
696
- to_return(:status => 201, :body => Yajl::Encoder.encode(:room => @valid_room_cache_data[123]), :headers => {})
697
- bot.send(:send_message, 123, "Hi", "Textmessage")
698
- }
699
- logger_output.should =~ /DEBUG.*Posted message "Hi" to room 123/
190
+ describe ".plugin" do
191
+ it "creates a new instance of the plugin" do
192
+ Scamp::Plugin.expects(:new)
193
+ Scamp.new do |scamp|
194
+ scamp.plugin Scamp::Plugin
700
195
  end
196
+ end
701
197
 
702
- it "should paste a message" do
703
- mock_logger
704
- bot = a Scamp
705
-
706
- EM.run_block {
707
- stub_request(:post, @message_post_url).
708
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type' => 'application/json'}).
709
- to_return(:status => 201, :body => Yajl::Encoder.encode(:room => @valid_room_cache_data[123]), :headers => {})
710
- bot.send(:send_message, 123, "Hi", "PasteMessage")
711
- }
712
- logger_output.should =~ /DEBUG.*Posted message "Hi" to room 123/
713
- end
198
+ it "passes options through to plugin instance" do
199
+ Scamp.new do |scamp|
200
+ opts = {:hello => "World"}
201
+ Scamp::Plugin.expects(:new).with(scamp, opts)
714
202
 
715
- it "should handle HTTP errors fetching individual room data" do
716
- mock_logger
717
- bot = a Scamp
718
-
719
- EM.run_block {
720
- stub_request(:post, @message_post_url).
721
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type' => 'application/json'}).
722
- to_return(:status => 502, :body => "", :headers => {'Content-Type'=>'text/html'})
723
- lambda {bot.send(:send_message, 123, "Hi", "Textmessage")}.should_not raise_error
724
- }
725
- logger_output.should =~ /ERROR.*Couldn't post message "Hi" to room 123 using url #{@message_post_url}, http response from the API was 502/
726
- end
727
-
728
- it "should handle network errors fetching individual room data" do
729
- mock_logger
730
- bot = a Scamp
731
-
732
- EM.run_block {
733
- stub_request(:post, @message_post_url).
734
- with(:headers => {'Authorization'=>[@valid_params[:api_key], 'X'], 'Content-Type' => 'application/json'}).to_timeout
735
- lambda {bot.send(:send_message, 123, "Hi", "Textmessage")}.should_not raise_error
736
- }
737
- logger_output.should =~ /ERROR.*Couldn't connect to #{@message_post_url} to post message "Hi" to room 123/
203
+ scamp.plugin Scamp::Plugin, opts
738
204
  end
739
205
  end
740
206
  end
741
207
 
742
- def a klass, params={}
743
- params ||= {}
744
- params = @valid_params.merge(params) if klass == Scamp
745
- klass.new(params)
746
- end
747
-
748
208
  # Urg
749
209
  def mock_logger
750
210
  @logger_string = StringIO.new