Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Coerce incoming message objects to a string.

Because JSON just marshals every object we pass to it we should make sure
we only send sensible message bodies to Campfire, otherwise it will 500.

- Coerces anything responding to the join method using join(' ')
- Coerces all other objects using to_s.
  • Loading branch information...
commit c60cbf46ed3bfe32d16973b93fcc2c82ffde9d03 1 parent 50e866e
@Manfred authored
Showing with 27 additions and 1 deletion.
  1. +11 −1 lib/broach/room.rb
  2. +16 −0 test/unit/room_test.rb
View
12 lib/broach/room.rb
@@ -32,10 +32,20 @@ def speak(content, options={})
options[:type] ||= :text
Broach.session.post("room/#{id}/speak", 'message' => {
'type' => TYPE_MAP[options[:type]],
- 'body' => content
+ 'body' => friendly_coerce_to_string(content)
})['message']
end
+ private
+
+ def friendly_coerce_to_string(content)
+ if content.respond_to?(:join)
+ content.join(' ')
+ else
+ content.to_s
+ end
+ end
+
# Returns a Room instance for all rooms accessible to the authenticated user
def self.all
Broach.session.get('rooms')['rooms'].map do |attributes|
View
16 test/unit/room_test.rb
@@ -73,4 +73,20 @@ def room(attributes)
}).and_return({'message' => {}})
@room.speak('crickets', :type => :sound)
end
+
+ it "should coerce content arrays of strings to a string" do
+ Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
+ 'type' => 'TextMessage',
+ 'body' => 'Hi there'
+ }).and_return({'message' => {}})
+ @room.speak(['Hi', 'there'])
+ end
+
+ it "should coerce content objects to a string" do
+ Broach.session.should.receive(:post).with('room/12/speak', 'message' => {
+ 'type' => 'TextMessage',
+ 'body' => '12'
+ }).and_return({'message' => {}})
+ @room.speak(12)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.