Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Broadcast::Medium::Simple

  • Loading branch information...
commit aadbba01f544b710e778002ed236ebe0d1876504 1 parent 59f65a1
Marcin Bunsch marcinbunsch authored marcinbunsch committed
16 README.markdown
View
@@ -68,6 +68,22 @@ When you're ready, just instantiate the Message class and call #publish:
Poke.new.publish
```
+Broadcast::Message::Simple
+------------
+
+If you need to dynamically create a message or just want a oneliner to publish a message, Broadcast::Message::Simple is your friend!
+
+```ruby
+ Broadcast::Message::Simple.new(:body => 'Poke!').publish(:jabber)
+```
+
+Broadcast::Message::Simple accepts the following keys in the arguments hash
+
+* body
+* subject
+
+Broadcast::Message::Simple#publish accepts the name of the medium and optional override settings for that medium
+
Delayed::Job
------------
2  lib/broadcast/message.rb
View
@@ -1,5 +1,7 @@
class Broadcast::Message
+ autoload "Simple", "broadcast/simple"
+
attr_accessor :options
class << self
attr_accessor :media
21 lib/broadcast/simple.rb
View
@@ -0,0 +1,21 @@
+class Broadcast::Message::Simple < Broadcast::Message
+
+ attr_accessor :body, :subject
+
+ def body
+ @body || options.body
+ end
+
+ def subject
+ @subject || options.subject
+ end
+
+ def publish(medium, medium_arguments = {})
+ begin
+ Broadcast::Medium.const_get(medium.to_s.downcase.capitalize).new(medium_arguments).publish(self)
+ rescue
+ Broadcast.logger.error "Publishing of #{self.class.name} to #{medium} failed:\n#{$!}"
+ end
+ end
+
+end
79 spec/lib/broadcast/simple_spec.rb
View
@@ -0,0 +1,79 @@
+require 'spec_helper'
+
+describe Broadcast::Message::Simple do
+
+ before do
+ @message = Broadcast::Message::Simple.new(:body => 'my body', :subject => 'OMG!')
+ end
+
+ describe ".initialize" do
+
+ it "should accept options" do
+ Broadcast::Message::Simple.new(:body => 'my body', :subject => 'OMG!')
+ end
+
+ end
+
+ describe "#body and #subject accessors" do
+
+ describe "#body" do
+
+ it "should be the value sent to the initializer" do
+ @message.body.should == 'my body'
+ end
+
+ end
+
+ describe "#body=" do
+
+ it "should allow changing the value of body dynamically" do
+ @message.body = 'updated body'
+ @message.body.should == 'updated body'
+ end
+
+ end
+
+ describe "#subject" do
+
+ it "should be the value sent to the initializer" do
+ @message.subject.should == 'OMG!'
+ end
+
+ end
+
+ describe "#subject=" do
+
+ it "should be the value sent to the initializer" do
+ @message.subject = 'LOL'
+ @message.subject.should == 'LOL'
+ end
+
+ end
+
+ end
+
+ describe '#publish' do
+
+ it "should explode when no media is supplied" do
+ lambda {
+ @message.publish
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should publish to the media passed as first argument" do
+ mocked = mock
+ mocked.should_receive(:publish).with(@message)
+ Broadcast::Medium::Log.should_receive(:new).and_return(mocked)
+ @message.publish :log
+ end
+
+ it "should override media settings if provided as second argument" do
+ mocked = mock
+ mocked.should_receive(:publish).with(@message)
+ Broadcast::Medium::Log.should_receive(:new).with(:file => 'different.log').and_return(mocked)
+ @message.publish :log, :file => 'different.log'
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.