broadcast 0.2.3 → 0.3.0
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.
- data/README.markdown +16 -0
- data/lib/broadcast.rb +2 -0
- data/lib/broadcast/message.rb +3 -24
- data/lib/broadcast/publishable.rb +35 -0
- data/lib/broadcast/simple.rb +21 -0
- data/lib/broadcast/version.rb +1 -1
- data/spec/lib/broadcast/publishable_spec.rb +57 -0
- data/spec/lib/broadcast/simple_spec.rb +79 -0
- metadata +9 -3
data/README.markdown
CHANGED
@@ -68,6 +68,22 @@ When you're ready, just instantiate the Message class and call #publish:
|
|
68
68
|
Poke.new.publish
|
69
69
|
```
|
70
70
|
|
71
|
+
Broadcast::Message::Simple
|
72
|
+
------------
|
73
|
+
|
74
|
+
If you need to dynamically create a message or just want a oneliner to publish a message, Broadcast::Message::Simple is your friend!
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
Broadcast::Message::Simple.new(:body => 'Poke!').publish(:jabber)
|
78
|
+
```
|
79
|
+
|
80
|
+
Broadcast::Message::Simple accepts the following keys in the arguments hash
|
81
|
+
|
82
|
+
* body
|
83
|
+
* subject
|
84
|
+
|
85
|
+
Broadcast::Message::Simple#publish accepts the name of the medium and optional override settings for that medium
|
86
|
+
|
71
87
|
Delayed::Job
|
72
88
|
------------
|
73
89
|
|
data/lib/broadcast.rb
CHANGED
data/lib/broadcast/message.rb
CHANGED
@@ -1,33 +1,12 @@
|
|
1
1
|
class Broadcast::Message
|
2
|
+
autoload "Simple", "broadcast/simple"
|
3
|
+
include Broadcast::Publishable
|
2
4
|
|
5
|
+
# Make the options Hashie::Mash a specific trait of Broadcast::Message
|
3
6
|
attr_accessor :options
|
4
|
-
class << self
|
5
|
-
attr_accessor :media
|
6
|
-
def medium(name, options = {})
|
7
|
-
self.media ||= []
|
8
|
-
self.media.push({ :name => name, :options => options })
|
9
|
-
end
|
10
|
-
end
|
11
7
|
|
12
8
|
def initialize(options = {})
|
13
9
|
@options = Hashie::Mash.new(options)
|
14
10
|
end
|
15
11
|
|
16
|
-
def publish
|
17
|
-
(self.class.media || []).each do |medium|
|
18
|
-
begin
|
19
|
-
Broadcast::Medium.const_get(medium[:name].to_s.downcase.capitalize).new(medium[:options]).publish(self)
|
20
|
-
rescue
|
21
|
-
Broadcast.logger.error "Publishing of #{self.class.name} to #{medium[:name]} failed:\n#{$!}"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def subject
|
27
|
-
end
|
28
|
-
|
29
|
-
def body
|
30
|
-
""
|
31
|
-
end
|
32
|
-
|
33
12
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# Module which allows any class to be turned into a message
|
2
|
+
module Broadcast::Publishable
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
attr_accessor :media
|
11
|
+
|
12
|
+
def medium(name, options = {})
|
13
|
+
(self.media ||= []).push({ :name => name, :options => options })
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
def publish
|
19
|
+
(self.class.media || []).each do |medium|
|
20
|
+
begin
|
21
|
+
Broadcast::Medium.const_get(medium[:name].to_s.downcase.capitalize).new(medium[:options]).publish(self)
|
22
|
+
rescue
|
23
|
+
Broadcast.logger.error "Publishing of #{self.class.name} to #{medium[:name]} failed:\n#{$!}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def subject
|
29
|
+
end
|
30
|
+
|
31
|
+
def body
|
32
|
+
""
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Broadcast::Message::Simple < Broadcast::Message
|
2
|
+
|
3
|
+
attr_accessor :body, :subject
|
4
|
+
|
5
|
+
def body
|
6
|
+
@body || options.body
|
7
|
+
end
|
8
|
+
|
9
|
+
def subject
|
10
|
+
@subject || options.subject
|
11
|
+
end
|
12
|
+
|
13
|
+
def publish(medium, medium_arguments = {})
|
14
|
+
begin
|
15
|
+
Broadcast::Medium.const_get(medium.to_s.downcase.capitalize).new(medium_arguments).publish(self)
|
16
|
+
rescue
|
17
|
+
Broadcast.logger.error "Publishing of #{self.class.name} to #{medium} failed:\n#{$!}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/lib/broadcast/version.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class ClassToBecomeMessage
|
4
|
+
include Broadcast::Publishable
|
5
|
+
|
6
|
+
attr_accessor :something
|
7
|
+
|
8
|
+
def initialize(somthng = nil)
|
9
|
+
self.something = somthng
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Broadcast::Publishable do
|
15
|
+
|
16
|
+
describe "in class scope" do
|
17
|
+
|
18
|
+
it "should add the media accessor" do
|
19
|
+
ClassToBecomeMessage.should respond_to(:media)
|
20
|
+
ClassToBecomeMessage.should respond_to(:media=)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should add the medium method" do
|
24
|
+
ClassToBecomeMessage.should respond_to(:medium)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "in instance scope" do
|
30
|
+
|
31
|
+
before {
|
32
|
+
@instance = ClassToBecomeMessage.new(123)
|
33
|
+
}
|
34
|
+
it "should add the publish method" do
|
35
|
+
@instance.should respond_to(:publish)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should add the subject method" do
|
39
|
+
@instance.should respond_to(:subject)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should add the body method" do
|
43
|
+
@instance.should respond_to(:body)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not add the options accessor" do
|
47
|
+
@instance.should_not respond_to(:options)
|
48
|
+
@instance.should_not respond_to(:options=)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not mess with the initializer" do
|
52
|
+
ClassToBecomeMessage.new(123).something.should == 123
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Broadcast::Message::Simple do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@message = Broadcast::Message::Simple.new(:body => 'my body', :subject => 'OMG!')
|
7
|
+
end
|
8
|
+
|
9
|
+
describe ".initialize" do
|
10
|
+
|
11
|
+
it "should accept options" do
|
12
|
+
Broadcast::Message::Simple.new(:body => 'my body', :subject => 'OMG!')
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#body and #subject accessors" do
|
18
|
+
|
19
|
+
describe "#body" do
|
20
|
+
|
21
|
+
it "should be the value sent to the initializer" do
|
22
|
+
@message.body.should == 'my body'
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#body=" do
|
28
|
+
|
29
|
+
it "should allow changing the value of body dynamically" do
|
30
|
+
@message.body = 'updated body'
|
31
|
+
@message.body.should == 'updated body'
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#subject" do
|
37
|
+
|
38
|
+
it "should be the value sent to the initializer" do
|
39
|
+
@message.subject.should == 'OMG!'
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#subject=" do
|
45
|
+
|
46
|
+
it "should be the value sent to the initializer" do
|
47
|
+
@message.subject = 'LOL'
|
48
|
+
@message.subject.should == 'LOL'
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#publish' do
|
56
|
+
|
57
|
+
it "should explode when no media is supplied" do
|
58
|
+
lambda {
|
59
|
+
@message.publish
|
60
|
+
}.should raise_error(ArgumentError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should publish to the media passed as first argument" do
|
64
|
+
mocked = mock
|
65
|
+
mocked.should_receive(:publish).with(@message)
|
66
|
+
Broadcast::Medium::Log.should_receive(:new).and_return(mocked)
|
67
|
+
@message.publish :log
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should override media settings if provided as second argument" do
|
71
|
+
mocked = mock
|
72
|
+
mocked.should_receive(:publish).with(@message)
|
73
|
+
Broadcast::Medium::Log.should_receive(:new).with(:file => 'different.log').and_return(mocked)
|
74
|
+
@message.publish :log, :file => 'different.log'
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: broadcast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 2
|
9
8
|
- 3
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.3.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marcin Bunsch
|
@@ -163,7 +163,9 @@ files:
|
|
163
163
|
- lib/broadcast/media/yammer.rb
|
164
164
|
- lib/broadcast/medium.rb
|
165
165
|
- lib/broadcast/message.rb
|
166
|
+
- lib/broadcast/publishable.rb
|
166
167
|
- lib/broadcast/railtie.rb
|
168
|
+
- lib/broadcast/simple.rb
|
167
169
|
- lib/broadcast/tasks/broadcast.rake
|
168
170
|
- lib/broadcast/version.rb
|
169
171
|
- spec/lib/broadcast/media/campfire_spec.rb
|
@@ -179,6 +181,8 @@ files:
|
|
179
181
|
- spec/lib/broadcast/media/yammer_spec.rb
|
180
182
|
- spec/lib/broadcast/medium_spec.rb
|
181
183
|
- spec/lib/broadcast/message_spec.rb
|
184
|
+
- spec/lib/broadcast/publishable_spec.rb
|
185
|
+
- spec/lib/broadcast/simple_spec.rb
|
182
186
|
- spec/lib/broadcast_spec.rb
|
183
187
|
- spec/spec_helper.rb
|
184
188
|
- spec/support/config.rb
|
@@ -231,6 +235,8 @@ test_files:
|
|
231
235
|
- spec/lib/broadcast/media/yammer_spec.rb
|
232
236
|
- spec/lib/broadcast/medium_spec.rb
|
233
237
|
- spec/lib/broadcast/message_spec.rb
|
238
|
+
- spec/lib/broadcast/publishable_spec.rb
|
239
|
+
- spec/lib/broadcast/simple_spec.rb
|
234
240
|
- spec/lib/broadcast_spec.rb
|
235
241
|
- spec/spec_helper.rb
|
236
242
|
- spec/support/config.rb
|