courier 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +11 -4
- data/VERSION +1 -1
- data/courier.gemspec +1 -1
- data/lib/courier/message.rb +1 -2
- data/lib/courier/owner.rb +4 -2
- data/lib/courier/service/base.rb +18 -4
- data/lib/courier/service/facebook.rb +4 -4
- data/lib/courier/template/base.rb +6 -1
- data/lib/courier.rb +1 -1
- data/lib/generators/templates/migration.rb +3 -3
- data/spec/courier/message_spec.rb +0 -2
- data/spec/courier/owner_spec.rb +4 -2
- data/spec/courier/service/base_spec.rb +6 -6
- data/spec/courier/service/facebook_spec.rb +2 -2
- data/spec/courier/template/base_spec.rb +1 -1
- data/spec/courier_spec.rb +1 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -70,14 +70,21 @@ Gemfile:
|
|
70
70
|
|
71
71
|
=== Сервис отложенной отправки
|
72
72
|
|
73
|
-
Достаточно реализовать метод <tt>
|
73
|
+
Достаточно реализовать метод <tt>deliver_all!</tt> или <tt>deliver_message</tt>, так как +message+ удачно складывает все сообщения в базу. Например:
|
74
74
|
|
75
|
-
def
|
76
|
-
|
77
|
-
send_message_my_way
|
75
|
+
def deliver_all!
|
76
|
+
messages.fresh.each do |message|
|
77
|
+
send_message_my_way(message.owner, message.text) and message.set_delivered
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
|
+
или
|
82
|
+
|
83
|
+
def deliver_message(message)
|
84
|
+
send_message_my_way(message.owner, message.text)
|
85
|
+
# returns true to mark message as delivered
|
86
|
+
end
|
87
|
+
|
81
88
|
=== Сервис отправки в реальном времени
|
82
89
|
|
83
90
|
Достаточно реализоват метод +message+. Например:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/courier.gemspec
CHANGED
data/lib/courier/message.rb
CHANGED
@@ -7,12 +7,11 @@ class Courier::Message < ActiveRecord::Base
|
|
7
7
|
set_table_name 'courier_messages'
|
8
8
|
|
9
9
|
belongs_to :owner, :polymorphic=>true
|
10
|
-
belongs_to :service #, :polymorphic=>true
|
11
|
-
belongs_to :template #, :polymorphic=>true
|
12
10
|
|
13
11
|
serialize :options, Hash
|
14
12
|
|
15
13
|
scope :fresh, where(:state=>:fresh)
|
14
|
+
scope :by_service, lambda { |service| where(:service=>service.to_s) }
|
16
15
|
|
17
16
|
validates_presence_of :owner, :service, :template
|
18
17
|
|
data/lib/courier/owner.rb
CHANGED
@@ -14,6 +14,7 @@
|
|
14
14
|
module Courier::Owner
|
15
15
|
def has_courier
|
16
16
|
has_one :courier, :as => :owner, :dependent => :destroy, :class_name=>'Courier::OwnerSetting'
|
17
|
+
has_many :courier_messages, :as => :owner, :dependent => :destroy, :class_name=>'Courier::Message'
|
17
18
|
include InstanceMethods
|
18
19
|
|
19
20
|
after_create do
|
@@ -24,8 +25,9 @@ module Courier::Owner
|
|
24
25
|
module InstanceMethods
|
25
26
|
def message(template_key, args={})
|
26
27
|
template = Courier.template(template_key)
|
27
|
-
Courier.config.services_order.
|
28
|
-
|
28
|
+
Courier.config.services_order.select do |service|
|
29
|
+
create_courier unless courier
|
30
|
+
courier.enabled?(template, service, args) and service.message(self, template, args)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
data/lib/courier/service/base.rb
CHANGED
@@ -36,20 +36,34 @@ class Courier::Service::Base
|
|
36
36
|
|
37
37
|
def check_args owner, template, args
|
38
38
|
args[:owner] ||=owner
|
39
|
-
args[:text] ||=template.get_text(args)
|
40
39
|
args[:service]||=self
|
40
|
+
args[:text] ||=template.get_text(args)
|
41
41
|
end
|
42
42
|
|
43
43
|
def message(owner, template, args)
|
44
44
|
check_args owner, template, args
|
45
|
-
|
45
|
+
Courier::Message.create! :owner=>owner, :template=>template.name, :service=>name, :options=>args
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
name
|
46
50
|
end
|
47
51
|
|
48
52
|
def name
|
49
53
|
self.class.name.demodulize.underscore.to_sym
|
50
54
|
end
|
51
55
|
|
52
|
-
def
|
53
|
-
raise 'inherit
|
56
|
+
def deliver_message(message)
|
57
|
+
raise 'inherit and implement me'
|
58
|
+
end
|
59
|
+
|
60
|
+
def messages
|
61
|
+
Courier::Message.by_service(name)
|
62
|
+
end
|
63
|
+
|
64
|
+
def deliver_all!
|
65
|
+
messages.fresh.each do |message|
|
66
|
+
deliver_message(message) and message.set_delivered
|
67
|
+
end
|
54
68
|
end
|
55
69
|
end
|
@@ -7,19 +7,19 @@ class Courier::Service::Facebook < Courier::Service::Base
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def check_args owner, template, args
|
10
|
-
args[:to]
|
10
|
+
args[:to]||='me'
|
11
11
|
args[:attachment]||={}
|
12
12
|
super
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def deliver_all!
|
16
16
|
cache={}
|
17
|
-
|
17
|
+
messages.fresh.each do |message|
|
18
18
|
message.owner.respond_to?(:facebook_token) or
|
19
19
|
raise "method facebook_token is not defined in your owner's model #{owner.class}"
|
20
20
|
token = message.owner.facebook_token or raise "owner's facebook_token is empty"
|
21
21
|
graph = cache[token] ||= Koala::Facebook::GraphAPI.new(token)
|
22
|
-
graph.put_wall_post(message.options[:text], message.options[:attachment], message.options[:to]) and
|
22
|
+
graph.put_wall_post(message.options[:text], message.options[:attachment], message.options[:to] || 'me') and
|
23
23
|
message.set_delivered
|
24
24
|
end
|
25
25
|
end
|
@@ -9,7 +9,8 @@ class Courier::Template::Base
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def get_text(args)
|
12
|
-
|
12
|
+
raise 'Service is not defined' unless args[:service]
|
13
|
+
args[:scope]=[:courier, args[:service].name] unless args[:scope]
|
13
14
|
args[:cascade]=true unless args.has_key? :cascade
|
14
15
|
I18n::translate(name, args )
|
15
16
|
end
|
@@ -26,6 +27,10 @@ class Courier::Template::Base
|
|
26
27
|
defaults[service.name.to_sym] = check_val(val)
|
27
28
|
end
|
28
29
|
|
30
|
+
def to_s
|
31
|
+
"Courier::Template(#{name})"
|
32
|
+
end
|
33
|
+
|
29
34
|
def key
|
30
35
|
name
|
31
36
|
end
|
data/lib/courier.rb
CHANGED
@@ -19,15 +19,15 @@ class CreateCourierTables < ActiveRecord::Migration
|
|
19
19
|
create_table :courier_messages, :force => true do |t|
|
20
20
|
t.integer :owner_id, :null => false
|
21
21
|
t.string :owner_type, :null => false
|
22
|
-
t.
|
23
|
-
t.
|
22
|
+
t.string :template, :null => false
|
23
|
+
t.string :service, :null => false
|
24
24
|
t.string :state, :null => false
|
25
25
|
t.text :options, :null => false
|
26
26
|
t.timestamp :delivered_at
|
27
27
|
t.timestamps
|
28
28
|
end
|
29
29
|
|
30
|
-
add_index :courier_messages, [:
|
30
|
+
add_index :courier_messages, [:service, :state]
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.down
|
data/spec/courier/owner_spec.rb
CHANGED
@@ -11,7 +11,8 @@ describe User, "Courier::Owner extention" do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
describe '#message' do
|
14
|
-
let(:args) { {:level=>123
|
14
|
+
let(:args) { {:level=>123, :text=>'some text'} }
|
15
|
+
subject{ Factory :user }
|
15
16
|
it 'should send message to enabled services only' do
|
16
17
|
template = mock_template
|
17
18
|
|
@@ -19,13 +20,14 @@ describe User, "Courier::Owner extention" do
|
|
19
20
|
service2 = mock_service
|
20
21
|
service1.should_receive(:message).with(subject, template, args)
|
21
22
|
|
23
|
+
subject.should_not_receive :create_courier
|
24
|
+
|
22
25
|
subject.courier.should_receive(:enabled?).twice { |template, service, args|
|
23
26
|
service==service1
|
24
27
|
}
|
25
28
|
|
26
29
|
Courier.should_receive(:config) { double :services_order=>[service1, service2] }
|
27
30
|
Courier.should_receive(:template).with(:templ) { template }
|
28
|
-
|
29
31
|
subject.message :templ, args
|
30
32
|
end
|
31
33
|
end
|
@@ -7,18 +7,18 @@ describe Courier::Service::Base do
|
|
7
7
|
describe '#message' do
|
8
8
|
it 'saves message in database' do
|
9
9
|
args={:a=>1}
|
10
|
-
owner =
|
11
|
-
|
12
|
-
|
13
|
-
cm.should_receive(:create!).with(:owner=>owner, :template=>template, :options=>args)
|
14
|
-
subject.should_receive(:courier_messages) { cm }
|
10
|
+
owner = Factory :user
|
11
|
+
owner.courier_messages.should be_empty
|
12
|
+
template = double :template, :name=>:template_key
|
15
13
|
subject.should_receive(:check_args).with(owner, template, args)
|
16
14
|
subject.message owner, template, args
|
15
|
+
owner.courier_messages(true).should have(1).items
|
17
16
|
end
|
18
17
|
end
|
19
18
|
|
20
19
|
# inherited in subclasses
|
21
|
-
describe '#deliver!'
|
20
|
+
describe '#deliver!' do
|
21
|
+
end
|
22
22
|
|
23
23
|
describe 'last subclass title as name' do
|
24
24
|
before do
|
@@ -15,8 +15,8 @@ describe Courier::Service::Facebook do
|
|
15
15
|
with(message.options[:text], message.options[:attachment], message.options[:to]) { true }
|
16
16
|
Koala::Facebook::GraphAPI.should_receive(:new).with('fbtoken') { graph }
|
17
17
|
|
18
|
-
subject.stub_chain('
|
19
|
-
subject.
|
18
|
+
subject.stub_chain('messages.fresh') { [message] }
|
19
|
+
subject.deliver_all!
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
@@ -12,7 +12,7 @@ describe Courier::Template::Base do
|
|
12
12
|
subject.should_receive(:name) { 'template_key' }
|
13
13
|
subject.
|
14
14
|
get_text(:some_option=>123,:service=>double(:name=>'facebook')).should ==
|
15
|
-
'translation missing: en.courier.
|
15
|
+
'translation missing: en.courier.facebook.template_key'
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
data/spec/courier_spec.rb
CHANGED
@@ -16,7 +16,7 @@ describe Courier do
|
|
16
16
|
describe '.deliver_all!' do
|
17
17
|
it 'should run deliver! for all services' do
|
18
18
|
service = mock_service
|
19
|
-
service.should_receive(:
|
19
|
+
service.should_receive(:deliver_all!).twice
|
20
20
|
Courier.config.should_receive(:services) { [service, service]}
|
21
21
|
Courier.deliver_all!
|
22
22
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: courier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 3
|
10
|
+
version: 0.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Danil Pismenny
|