notifiable-rails 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53d30b25ba3440af53de4b2bd74294a3c7d927af
4
- data.tar.gz: b093120437a3319b16eb1df279647992bdd3fed8
3
+ metadata.gz: 6b4955923e80912ffec3e18234d87fb942380a8b
4
+ data.tar.gz: c4b3b65c9c917b88f35705118a65988eca07327a
5
5
  SHA512:
6
- metadata.gz: 1b99297db23479355e730fa763fe73fc669f25ce998c4baae6c0edd00c6e1a1c3496110179d08c60f3b3cea335e7e6cf5ece37dbcb2e0eb20fb579531f2f359e
7
- data.tar.gz: 47f8a1c753ce0f59c8e14d24296330ebcb5aebdeea1a00d147d5302e1f0f7fd33e77a5e6d987a503749877658eec6d4576b6e5f7d334449a0568c6ce6649a2a6
6
+ metadata.gz: 72aab818f6684b44bb6f3a32745f09746391a357209495e34744f44dbaf8051586583134ecbbb08ea62ef23278ca648020d6c1c845b129dfb0bb1e4c43779ece
7
+ data.tar.gz: cf21d9790ee894059d9cf6dc384864e491bed33ffee9a39347ad93c04dd5ca2aa061500998b9363b8caca32d51a17281ca3d64aa06b25a1700c576e1aabcc2fd
@@ -1,44 +1,10 @@
1
1
  Notifiable.configure do |config|
2
2
 
3
- #
4
- # Rails Engine
5
- #
6
-
7
3
  # The controller class that the DeviceTokenController should extend
8
4
  config.api_controller_class = ApplicationController
9
5
 
10
6
  # The class representing the holder of the device
11
7
  config.user_class = User
12
-
13
- #
14
- # APNS
15
- #
16
-
17
- # The path to your apns private key
18
- config.apns_certificate = File.join(Rails.root, 'config', 'apns-production.pem')
19
-
20
- # The passphrase for your private key
21
- # Defaults to nil
22
- #config.apns_passphrase = 'YOUR-PASSPHRASE-HERE'
23
-
24
- # The apns gateway to use
25
- # Defaults to 'gateway.push.apple.com', can also be 'gateway.sandbox.push.apple.com'
26
- #config.apns_gateway = 'gateway.push.apple.com'
27
-
28
- #
29
- # GCM
30
- #
31
-
32
- # Your GCM API Key
33
- #config.gcm_api_key = 'YOUR-KEY-HERE'
34
-
35
- # The batch size
36
- # Defaults to 1000
37
- #config.gcm_batch_size = 1000
38
-
39
- #
40
- # Global
41
- #
42
8
 
43
9
  # Set the delivery method to test, preventing notifications from being sent
44
10
  # Defaults to :send
@@ -2,8 +2,9 @@ module Notifiable
2
2
  class Batch
3
3
  attr_accessor :notifiers
4
4
 
5
- def initialize
5
+ def initialize(config = {})
6
6
  @notifiers = {}
7
+ @config = config
7
8
  end
8
9
 
9
10
  def add(notification, user)
@@ -11,9 +12,11 @@ module Notifiable
11
12
  provider = d.provider.to_sym
12
13
 
13
14
  unless @notifiers[provider]
14
- clazz = Notifiable.notifier_classes[provider]
15
+ clazz = Notifiable.notifier_classes[provider]
15
16
  raise "Notifier #{provider} not configured" unless clazz
16
17
  @notifiers[provider] = clazz.new
18
+ @notifiers[provider].env = Rails.env
19
+ @config[provider].each_pair {|key, value| @notifiers[provider].send("#{key}=", value) if @notifiers[provider].methods.include?("#{key}=".to_sym) } if @config[provider]
17
20
  end
18
21
 
19
22
  notifier = @notifiers[provider]
@@ -5,8 +5,12 @@ module Notifiable
5
5
 
6
6
  has_many :notification_device_tokens, :class_name => 'Notifiable::NotificationDeviceToken'
7
7
 
8
- def apns_message
9
- @apns_message ||= (self.message.bytesize > 232 ? "#{self.message.byteslice(0, 229)}..." : self.message)
8
+ def provider_value(provider, key)
9
+ if self.payload && self.payload[provider] && self.payload[provider][key]
10
+ self.payload[provider][key]
11
+ elsif self.respond_to? key
12
+ self.send(key)
13
+ end
10
14
  end
11
15
  end
12
16
  end
@@ -2,6 +2,8 @@ module Notifiable
2
2
 
3
3
  class NotifierBase
4
4
 
5
+ attr_accessor :env
6
+
5
7
  def send_notification(notification, device_token)
6
8
  # todo - add before hook
7
9
  enqueue(notification, device_token)
@@ -13,7 +15,7 @@ module Notifiable
13
15
  save_receipts
14
16
  end
15
17
 
16
- protected
18
+ protected
17
19
  def flush
18
20
 
19
21
  end
@@ -26,6 +28,10 @@ module Notifiable
26
28
  end
27
29
  end
28
30
 
31
+ def test_env?
32
+ self.env == "test"
33
+ end
34
+
29
35
  private
30
36
  def receipts
31
37
  @receipts ||= []
@@ -1,3 +1,3 @@
1
1
  module Notifiable
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/notifiable.rb CHANGED
@@ -13,18 +13,6 @@ module Notifiable
13
13
  mattr_accessor :api_controller_class
14
14
 
15
15
  mattr_accessor :user_class
16
-
17
- mattr_accessor :apns_gateway
18
- @@apns_gateway = 'gateway.push.apple.com'
19
-
20
- mattr_accessor :apns_certificate
21
-
22
- mattr_accessor :apns_passphrase
23
-
24
- mattr_accessor :gcm_api_key
25
-
26
- mattr_accessor :gcm_batch_size
27
- @@gcm_batch_size = 1000
28
16
 
29
17
  mattr_accessor :delivery_method
30
18
  @@delivery_method = :send
@@ -35,32 +23,12 @@ module Notifiable
35
23
  def self.configure
36
24
  yield self
37
25
  end
38
-
39
- def self.apns_gateway_config
40
- {
41
- :gateway => apns_gateway,
42
- :certificate => apns_certificate,
43
- :passphrase => apns_passphrase
44
- }
45
- end
46
-
47
- def self.apns_feedback_config
48
- {
49
- :gateway => self.apns_gateway_config[:gateway].gsub('gateway', 'feedback'),
50
- :certificate => self.apns_gateway_config[:certificate],
51
- :passphrase => self.apns_gateway_config[:passphrase]
52
- }
53
- end
54
26
 
55
- def self.batch()
56
- b = Batch.new
27
+ def self.batch(config = {})
28
+ b = Batch.new(config)
57
29
  yield(b)
58
30
  b.close
59
31
  end
60
-
61
- def self.env
62
- Rails.env || 'development'
63
- end
64
32
 
65
33
  end
66
34
 
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Notifiable::Batch do
4
+ let(:user1) { FactoryGirl.create(:user) }
5
+ let(:notification) { Notifiable::Notification.new(:message => "Test message")}
6
+
7
+ it "configures the provider" do
8
+ FactoryGirl.create(:mock_token, :provider => :configurable_mock, :user_id => user1.id)
9
+
10
+ config = {:configurable_mock => {:use_sandbox => true}}
11
+
12
+ b = Notifiable::Batch.new(config)
13
+ b.add(notification, user1)
14
+
15
+ b.notifiers[:configurable_mock].env.should eql Rails.env
16
+ b.notifiers[:configurable_mock].use_sandbox.should == true
17
+ end
18
+
19
+ end
20
+
21
+ class ConfigurableMockNotifier < Notifiable::NotifierBase
22
+ attr_accessor :use_sandbox
23
+
24
+ def enqueue(notification, device_token)
25
+ processed(notification, device_token)
26
+ end
27
+ end
28
+
29
+ Notifiable.notifier_classes[:configurable_mock] = ConfigurableMockNotifier
@@ -16,11 +16,11 @@ describe Notifiable do
16
16
 
17
17
  all_notifications = Notifiable::NotificationDeviceToken.all
18
18
  first_notification_token = all_notifications[0]
19
- first_notification_token.notification.apns_message.should eql "First test message"
19
+ first_notification_token.notification.provider_value(:mock, :message).should eql "First test message"
20
20
  first_notification_token.device_token.should eql user1.device_tokens[0]
21
21
 
22
22
  second_notification_token = all_notifications[1]
23
- second_notification_token.notification.apns_message.should eql "First test message"
23
+ second_notification_token.notification.provider_value(:mock, :message).should eql "First test message"
24
24
  second_notification_token.device_token.should eql user2.device_tokens[0]
25
25
  end
26
26
 
@@ -34,27 +34,14 @@ describe Notifiable do
34
34
 
35
35
  all_notifications = Notifiable::NotificationDeviceToken.all
36
36
  first_notification_token = all_notifications[0]
37
- first_notification_token.notification.apns_message.should eql "First test message"
37
+ first_notification_token.notification.provider_value(:mock, :message).should eql "First test message"
38
38
  first_notification_token.device_token.should eql user1.device_tokens[0]
39
39
 
40
40
  second_notification_token = all_notifications[1]
41
- second_notification_token.notification.apns_message.should eql "Second test message"
41
+ second_notification_token.notification.provider_value(:mock, :message).should eql "Second test message"
42
42
  second_notification_token.device_token.should eql user2.device_tokens[0]
43
43
  end
44
44
 
45
- it "truncates long apns messages" do
46
-
47
- long_notification = Notifiable::Notification.create(:message => "First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message end")
48
-
49
- user1.send_notification(long_notification)
50
-
51
- Notifiable::NotificationDeviceToken.count.should == 1
52
-
53
- all_notifications = Notifiable::NotificationDeviceToken.all
54
- first_notification_token = all_notifications[0]
55
- first_notification_token.notification.apns_message.should eql "First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message First test message F..."
56
- end
57
-
58
45
  it "raises an error if it can't find the notification provider" do
59
46
  user = FactoryGirl.create(:user)
60
47
  Notifiable::DeviceToken.create :user_id => user.id, :token => "DEF567", :provider => :gcm
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Notifiable::Notification do
4
+
5
+ it "stores a message" do
6
+ Notifiable::Notification.create :message => "Test message"
7
+
8
+ Notifiable::Notification.first.provider_value(:mock, :message).should eql "Test message"
9
+ end
10
+
11
+ it "overrides a message" do
12
+ Notifiable::Notification.create :message => "Test message", :payload => {:mock => {:message => "A different message"}}
13
+
14
+ Notifiable::Notification.first.provider_value(:mock, :message).should eql "A different message"
15
+ end
16
+
17
+ it "returns nil for a property that doesnt exist" do
18
+ Notifiable::Notification.create :message => "Test message"
19
+
20
+ Notifiable::Notification.first.provider_value(:mock, :sound).should be_nil
21
+ end
22
+
23
+ end
@@ -33,11 +33,4 @@ TestApp::Application.configure do
33
33
 
34
34
  # Print deprecation notices to the stderr.
35
35
  config.active_support.deprecation = :stderr
36
-
37
- Notifiable.configure do |config|
38
- config.apns_certificate = nil
39
- config.apns_gateway = "localhost"
40
- config.apns_passphrase = nil
41
- end
42
-
43
36
  end
@@ -1,44 +1,10 @@
1
1
  Notifiable.configure do |config|
2
2
 
3
- #
4
- # Rails Engine
5
- #
6
-
7
3
  # The controller class that the DeviceTokenController should extend
8
4
  config.api_controller_class = ApplicationController
9
5
 
10
6
  # The class representing the holder of the device
11
7
  config.user_class = User
12
-
13
- #
14
- # APNS
15
- #
16
-
17
- # The path to your apns private key
18
- config.apns_certificate = File.join(Rails.root, 'config', 'apns-development.pem')
19
-
20
- # The passphrase for your private key
21
- # Defaults to nil
22
- #config.apns_passphrase = 'YOUR-PASSPHRASE-HERE'
23
-
24
- # The apns gateway to use
25
- # Defaults to 'gateway.push.apple.com', can also be 'gateway.sandbox.push.apple.com'
26
- #config.apns_gateway = 'gateway.push.apple.com'
27
-
28
- #
29
- # GCM
30
- #
31
-
32
- # Your GCM API Key
33
- #config.gcm_api_key = 'YOUR-KEY-HERE'
34
-
35
- # The batch size
36
- # Defaults to 1000
37
- #config.gcm_batch_size = 1000
38
-
39
- #
40
- # Global
41
- #
42
8
 
43
9
  # Set the delivery method to test, preventing notifications from being sent
44
10
  # Defaults to :send