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 CHANGED
@@ -70,14 +70,21 @@ Gemfile:
70
70
 
71
71
  === Сервис отложенной отправки
72
72
 
73
- Достаточно реализовать метод <tt>deliver!</tt>, так как +message+ удачно складывает все сообщения в базу. Например:
73
+ Достаточно реализовать метод <tt>deliver_all!</tt> или <tt>deliver_message</tt>, так как +message+ удачно складывает все сообщения в базу. Например:
74
74
 
75
- def deliver!
76
- courier_messages.fresh.each do |message|
77
- send_message_my_way message.owner, message.text
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.2
1
+ 0.1.3
data/courier.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{courier}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Danil Pismenny"]
@@ -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.each do |service|
28
- service.message(self, template, args) if courier.enabled?(template, service, args)
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
@@ -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
- courier_messages.create! :owner=>owner, :template=>template, :options=>args
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 deliver!
53
- raise 'inherit my class and implement me'
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]|='me'
10
+ args[:to]||='me'
11
11
  args[:attachment]||={}
12
12
  super
13
13
  end
14
14
 
15
- def deliver!
15
+ def deliver_all!
16
16
  cache={}
17
- courier_messages.fresh.each do |message|
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
- args[:scope]=[:courier,:messages,args[:service].name] unless args[:scope]
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,7 +19,7 @@ module Courier
19
19
 
20
20
  def deliver_all!
21
21
  config.services.each do |service|
22
- service.deliver!
22
+ service.deliver_all!
23
23
  end
24
24
  end
25
25
 
@@ -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.integer :template_id, :null => false
23
- t.integer :service_id, :null => false
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, [:service_id, :state]
30
+ add_index :courier_messages, [:service, :state]
31
31
  end
32
32
 
33
33
  def self.down
@@ -4,8 +4,6 @@ require 'spec_helper'
4
4
  describe Courier::Message do
5
5
 
6
6
  it { should belong_to(:owner) }
7
- it { should belong_to(:template) }
8
- it { should belong_to(:service) }
9
7
 
10
8
  it { should validate_presence_of(:owner) }
11
9
  it { should validate_presence_of(:service) }
@@ -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,:text=>'some text'} }
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 = double
11
- template = double
12
- cm = double
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('courier_messages.fresh') { [message] }
19
- subject.deliver!
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.messages.facebook.template_key'
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(:deliver!).twice
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: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Danil Pismenny