Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a speak method to Broach to quickly say something in a room.

  • Loading branch information...
commit 4578530113cb87e1e7dbd696c376181e97d429d7 1 parent 64eda7c
@Manfred authored
View
4 .kick
@@ -3,8 +3,8 @@ recipe :ruby
process do |files|
Ruby.run_tests(files.take_and_map do |file|
case file
- when %r{^lib/broach/(.*)\.rb$}
- "test/unit/#{$1}_test.rb"
+ when %r{^lib/(broach/)?(.*)\.rb$}
+ "test/unit/#{$2}_test.rb"
end
end)
end
View
26 lib/broach.rb
@@ -35,4 +35,30 @@ def self.me
def self.rooms
Broach::Room.all
end
+
+ # Send a message to a room with a certain name.
+ #
+ # Note that you should only use this method if you're sending just one message. It fetches
+ # all rooms before sending the message to find the room with the name you're specifying.
+ #
+ # If you need to send multiple messages to the same room you should instantiate a room first.
+ #
+ # ==== Options
+ #
+ # +room+
+ # The name of the room, ie. 'Office'
+ # +content+
+ # The content of the message, see Broach::Room#speak for more information.
+ # +options+
+ # Options for the message, see Broach::Room#speak for more information.
+ #
+ # ==== Examples
+ #
+ # Broach.speak('Office', 'Manfred just deployed a new version of the weblog (http://www.fngtps.com)')
+ # Broach.speak('Office', 'crickets', :type => :sound)
+ def self.speak(room_name, content, options={})
+ if room = rooms.find { |room| room.name == room_name }
+ room.speak(content, options)
+ end
+ end
end
View
2  lib/broach/room.rb
@@ -28,7 +28,7 @@ def speak(content, options={})
Broach.session.post("room/#{id}/speak", 'message' => {
'type' => TYPE_MAP[options[:type]],
'body' => content
- })
+ })['message']
end
def self.all
View
13 test/remote/broach_test.rb
@@ -0,0 +1,13 @@
+require File.expand_path('../../start', __FILE__)
+
+describe "Broach" do
+ before do
+ Broach.settings = settings
+ end
+
+ it "should speak to a named room" do
+ body = 'Should speak to a named room'
+ message = Broach.speak(Broach.session.room, body)
+ message['body'].should == body
+ end
+end
View
6 test/remote/room_test.rb
@@ -32,18 +32,18 @@
it "should speak text" do
body = 'Should speak text'
message = @room.speak(body)
- message['message']['body'].should == body
+ message['body'].should == body
end
it "should speak paste" do
body = 'Should speak paste'
message = @room.speak(body, :type => :paste)
- message['message']['body'].should == body
+ message['body'].should == body
end
it "should speak sound" do
body = 'crickets'
message = @room.speak(body, :type => :sound)
- message['message']['body'].should == body
+ message['body'].should == body
end
end
View
61 test/unit/broach_test.rb
@@ -0,0 +1,61 @@
+require File.expand_path('../../start', __FILE__)
+
+module BroachSpecHelpers
+ def attributes
+ { "account" => "example", "token" => "xxx", "use_ssl" => true }
+ end
+end
+
+describe "Broach" do
+ extend BroachSpecHelpers
+
+ it "should set and return settings" do
+ Broach.settings = attributes
+ Broach.settings.should == attributes
+ end
+
+ it "should use the settings to create a new session" do
+ Broach.settings = attributes
+ session = Broach.session
+
+ attributes.each do |key, value|
+ session.send(key).should == value
+ end
+ end
+
+ it "should make sure the session has new settings when the settings change" do
+ Broach.settings = attributes
+ Broach.session
+
+ other_attributes = attributes
+ other_attributes['account'] = 'other_example'
+ Broach.settings = other_attributes
+ session = Broach.session
+
+ other_attributes.each do |key, value|
+ session.send(key).should == value
+ end
+ end
+end
+
+describe "Broach, concerning the API" do
+ it "should return the user associated with the authentication token" do
+ Broach::User.should.receive(:me)
+ Broach.me
+ end
+
+ it "should return all rooms for the account" do
+ Broach::Room.should.receive(:all)
+ Broach.rooms
+ end
+
+ it "should speak to a named room" do
+ office_room = mock('Room', :name => 'Office')
+ business_room = mock('Room', :name => 'Business')
+ Broach.stub!(:rooms).and_return([office_room, business_room])
+
+ office_room.should.receive(:speak).with('Codes!', :type => :paste)
+
+ Broach.speak('Office', 'Codes!', :type => :paste)
+ end
+end
View
8 test/unit/room_test.rb
@@ -36,7 +36,7 @@ def room(attributes)
Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
'type' => 'TextMessage',
'body' => 'Howdy'
- })
+ }).and_return({'message' => {}})
@room.speak('Howdy')
end
@@ -44,7 +44,7 @@ def room(attributes)
Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
'type' => 'TextMessage',
'body' => 'Howdy'
- })
+ }).and_return({'message' => {}})
@room.speak('Howdy', :type => :text)
end
@@ -52,7 +52,7 @@ def room(attributes)
Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
'type' => 'PasteMessage',
'body' => '<code></code>'
- })
+ }).and_return({'message' => {}})
@room.speak('<code></code>', :type => :paste)
end
@@ -60,7 +60,7 @@ def room(attributes)
Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
'type' => 'SoundMessage',
'body' => 'crickets'
- })
+ }).and_return({'message' => {}})
@room.speak('crickets', :type => :sound)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.