rpush 1.0.0 → 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +12 -14
  4. data/bin/rpush +11 -2
  5. data/lib/generators/rpush_generator.rb +2 -0
  6. data/lib/generators/templates/add_adm.rb +5 -5
  7. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +1 -1
  8. data/lib/generators/templates/add_app_to_rapns.rb +2 -2
  9. data/lib/generators/templates/add_fail_after_to_rpush_notifications.rb +1 -1
  10. data/lib/generators/templates/add_gcm.rb +32 -32
  11. data/lib/generators/templates/add_rpush.rb +67 -67
  12. data/lib/generators/templates/add_wpns.rb +2 -2
  13. data/lib/generators/templates/create_rapns_apps.rb +5 -5
  14. data/lib/generators/templates/create_rapns_feedback.rb +2 -2
  15. data/lib/generators/templates/create_rapns_notifications.rb +15 -15
  16. data/lib/generators/templates/rpush.rb +10 -7
  17. data/lib/generators/templates/rpush_2_0_0_updates.rb +23 -0
  18. data/lib/rpush.rb +4 -28
  19. data/lib/rpush/client/active_model.rb +21 -0
  20. data/lib/rpush/client/active_model/adm/app.rb +23 -0
  21. data/lib/rpush/client/active_model/adm/data_validator.rb +14 -0
  22. data/lib/rpush/client/active_model/adm/notification.rb +28 -0
  23. data/lib/rpush/client/active_model/apns/app.rb +37 -0
  24. data/lib/rpush/client/active_model/apns/binary_notification_validator.rb +16 -0
  25. data/lib/rpush/client/active_model/apns/device_token_format_validator.rb +14 -0
  26. data/lib/rpush/client/active_model/apns/notification.rb +90 -0
  27. data/lib/rpush/client/active_model/gcm/app.rb +19 -0
  28. data/lib/rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +14 -0
  29. data/lib/rpush/client/active_model/gcm/notification.rb +31 -0
  30. data/lib/rpush/client/active_model/notification.rb +26 -0
  31. data/lib/rpush/client/active_model/payload_data_size_validator.rb +13 -0
  32. data/lib/rpush/client/active_model/registration_ids_count_validator.rb +13 -0
  33. data/lib/rpush/client/active_model/wpns/app.rb +13 -0
  34. data/lib/rpush/client/active_model/wpns/notification.rb +17 -0
  35. data/lib/rpush/client/active_record.rb +19 -0
  36. data/lib/rpush/client/active_record/adm/app.rb +11 -0
  37. data/lib/rpush/client/active_record/adm/notification.rb +11 -0
  38. data/lib/rpush/client/active_record/apns/app.rb +11 -0
  39. data/lib/rpush/client/active_record/apns/feedback.rb +20 -0
  40. data/lib/rpush/client/active_record/apns/notification.rb +46 -0
  41. data/lib/rpush/client/active_record/app.rb +17 -0
  42. data/lib/rpush/client/active_record/gcm/app.rb +11 -0
  43. data/lib/rpush/client/active_record/gcm/notification.rb +11 -0
  44. data/lib/rpush/client/active_record/notification.rb +38 -0
  45. data/lib/rpush/client/active_record/wpns/app.rb +11 -0
  46. data/lib/rpush/client/active_record/wpns/notification.rb +11 -0
  47. data/lib/rpush/client/redis.rb +35 -0
  48. data/lib/rpush/client/redis/adm/app.rb +14 -0
  49. data/lib/rpush/client/redis/adm/notification.rb +11 -0
  50. data/lib/rpush/client/redis/apns/app.rb +11 -0
  51. data/lib/rpush/client/redis/apns/feedback.rb +20 -0
  52. data/lib/rpush/client/redis/apns/notification.rb +11 -0
  53. data/lib/rpush/client/redis/app.rb +22 -0
  54. data/lib/rpush/client/redis/gcm/app.rb +11 -0
  55. data/lib/rpush/client/redis/gcm/notification.rb +11 -0
  56. data/lib/rpush/client/redis/notification.rb +68 -0
  57. data/lib/rpush/client/redis/wpns/app.rb +11 -0
  58. data/lib/rpush/client/redis/wpns/notification.rb +11 -0
  59. data/lib/rpush/configuration.rb +27 -6
  60. data/lib/rpush/daemon.rb +36 -56
  61. data/lib/rpush/daemon/adm/delivery.rb +50 -52
  62. data/lib/rpush/daemon/apns.rb +6 -5
  63. data/lib/rpush/daemon/apns/delivery.rb +20 -44
  64. data/lib/rpush/daemon/apns/feedback_receiver.rb +11 -8
  65. data/lib/rpush/daemon/app_runner.rb +67 -60
  66. data/lib/rpush/daemon/batch.rb +54 -40
  67. data/lib/rpush/daemon/delivery.rb +13 -3
  68. data/lib/rpush/daemon/delivery_error.rb +10 -2
  69. data/lib/rpush/daemon/dispatcher/apns_tcp.rb +106 -0
  70. data/lib/rpush/daemon/dispatcher/http.rb +3 -3
  71. data/lib/rpush/daemon/dispatcher/tcp.rb +3 -3
  72. data/lib/rpush/daemon/dispatcher_loop.rb +15 -6
  73. data/lib/rpush/daemon/errors.rb +18 -0
  74. data/lib/rpush/daemon/feeder.rb +28 -39
  75. data/lib/rpush/daemon/gcm/delivery.rb +19 -20
  76. data/lib/rpush/daemon/interruptible_sleep.rb +26 -45
  77. data/lib/rpush/daemon/loggable.rb +2 -4
  78. data/lib/rpush/daemon/proc_title.rb +16 -0
  79. data/lib/rpush/daemon/queue_payload.rb +12 -0
  80. data/lib/rpush/daemon/reflectable.rb +3 -5
  81. data/lib/rpush/daemon/retry_header_parser.rb +6 -6
  82. data/lib/rpush/daemon/ring_buffer.rb +16 -0
  83. data/lib/rpush/daemon/service_config_methods.rb +23 -7
  84. data/lib/rpush/daemon/signal_handler.rb +51 -0
  85. data/lib/rpush/daemon/store/active_record.rb +71 -38
  86. data/lib/rpush/daemon/store/active_record/reconnectable.rb +15 -15
  87. data/lib/rpush/daemon/store/interface.rb +19 -0
  88. data/lib/rpush/daemon/store/redis.rb +149 -0
  89. data/lib/rpush/daemon/tcp_connection.rb +6 -11
  90. data/lib/rpush/daemon/wpns/delivery.rb +21 -30
  91. data/lib/rpush/deprecatable.rb +4 -3
  92. data/lib/rpush/deprecation.rb +7 -10
  93. data/lib/rpush/embed.rb +7 -2
  94. data/lib/rpush/logger.rb +11 -15
  95. data/lib/rpush/push.rb +0 -1
  96. data/lib/rpush/reflection.rb +6 -12
  97. data/lib/rpush/version.rb +1 -1
  98. data/lib/tasks/quality.rake +34 -0
  99. data/spec/.rubocop.yml +4 -0
  100. data/spec/functional/adm_spec.rb +3 -6
  101. data/spec/functional/apns_spec.rb +118 -24
  102. data/spec/functional/embed_spec.rb +22 -20
  103. data/spec/functional/gcm_spec.rb +4 -7
  104. data/spec/functional/new_app_spec.rb +61 -0
  105. data/spec/functional/retry_spec.rb +46 -0
  106. data/spec/functional/wpns_spec.rb +3 -6
  107. data/spec/functional_spec_helper.rb +26 -0
  108. data/spec/integration/rpush_spec.rb +13 -0
  109. data/spec/integration/support/gcm_success_response.json +1 -0
  110. data/spec/spec_helper.rb +60 -0
  111. data/spec/support/active_record_setup.rb +48 -0
  112. data/{config → spec/support/config}/database.yml +0 -0
  113. data/spec/support/install.sh +43 -7
  114. data/spec/support/simplecov_helper.rb +9 -5
  115. data/spec/support/simplecov_quality_formatter.rb +10 -6
  116. data/spec/unit/apns_feedback_spec.rb +3 -3
  117. data/spec/unit/{adm → client/active_record/adm}/app_spec.rb +3 -3
  118. data/spec/unit/{adm → client/active_record/adm}/notification_spec.rb +5 -7
  119. data/spec/unit/client/active_record/apns/app_spec.rb +29 -0
  120. data/spec/unit/client/active_record/apns/feedback_spec.rb +9 -0
  121. data/spec/unit/client/active_record/apns/notification_spec.rb +231 -0
  122. data/spec/unit/client/active_record/app_spec.rb +30 -0
  123. data/spec/unit/client/active_record/gcm/app_spec.rb +4 -0
  124. data/spec/unit/{gcm → client/active_record/gcm}/notification_spec.rb +5 -7
  125. data/spec/unit/client/active_record/notification_spec.rb +15 -0
  126. data/spec/unit/client/active_record/wpns/app_spec.rb +4 -0
  127. data/spec/unit/client/active_record/wpns/notification_spec.rb +21 -0
  128. data/spec/unit/configuration_spec.rb +12 -5
  129. data/spec/unit/daemon/adm/delivery_spec.rb +57 -54
  130. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +3 -3
  131. data/spec/unit/daemon/apns/delivery_spec.rb +90 -83
  132. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +24 -17
  133. data/spec/unit/daemon/app_runner_spec.rb +66 -123
  134. data/spec/unit/daemon/batch_spec.rb +52 -115
  135. data/spec/unit/daemon/delivery_spec.rb +15 -1
  136. data/spec/unit/daemon/dispatcher/http_spec.rb +3 -2
  137. data/spec/unit/daemon/dispatcher/tcp_spec.rb +10 -9
  138. data/spec/unit/daemon/dispatcher_loop_spec.rb +7 -12
  139. data/spec/unit/daemon/feeder_spec.rb +40 -39
  140. data/spec/unit/daemon/gcm/delivery_spec.rb +108 -89
  141. data/spec/unit/daemon/reflectable_spec.rb +2 -2
  142. data/spec/unit/daemon/retryable_error_spec.rb +1 -1
  143. data/spec/unit/daemon/service_config_methods_spec.rb +6 -3
  144. data/spec/unit/daemon/signal_handler_spec.rb +72 -0
  145. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +9 -9
  146. data/spec/unit/daemon/store/active_record_spec.rb +38 -47
  147. data/spec/unit/daemon/tcp_connection_spec.rb +22 -34
  148. data/spec/unit/daemon/too_many_requests_error_spec.rb +1 -1
  149. data/spec/unit/daemon/wpns/delivery_spec.rb +61 -50
  150. data/spec/unit/daemon_spec.rb +46 -81
  151. data/spec/unit/embed_spec.rb +4 -2
  152. data/spec/unit/logger_spec.rb +30 -40
  153. data/spec/unit/notification_shared.rb +9 -79
  154. data/spec/unit/push_spec.rb +3 -8
  155. data/spec/unit/reflection_spec.rb +25 -25
  156. data/spec/unit/rpush_spec.rb +1 -2
  157. data/spec/unit_spec_helper.rb +33 -88
  158. metadata +119 -67
  159. data/lib/rpush/TODO +0 -3
  160. data/lib/rpush/adm/app.rb +0 -15
  161. data/lib/rpush/adm/data_validator.rb +0 -11
  162. data/lib/rpush/adm/notification.rb +0 -29
  163. data/lib/rpush/apns/app.rb +0 -29
  164. data/lib/rpush/apns/binary_notification_validator.rb +0 -12
  165. data/lib/rpush/apns/device_token_format_validator.rb +0 -12
  166. data/lib/rpush/apns/feedback.rb +0 -16
  167. data/lib/rpush/apns/notification.rb +0 -84
  168. data/lib/rpush/app.rb +0 -18
  169. data/lib/rpush/daemon/apns/certificate_expired_error.rb +0 -20
  170. data/lib/rpush/daemon/apns/disconnection_error.rb +0 -20
  171. data/lib/rpush/gcm/app.rb +0 -11
  172. data/lib/rpush/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +0 -11
  173. data/lib/rpush/gcm/notification.rb +0 -30
  174. data/lib/rpush/notification.rb +0 -69
  175. data/lib/rpush/notifier.rb +0 -52
  176. data/lib/rpush/payload_data_size_validator.rb +0 -10
  177. data/lib/rpush/railtie.rb +0 -11
  178. data/lib/rpush/registration_ids_count_validator.rb +0 -10
  179. data/lib/rpush/wpns/app.rb +0 -9
  180. data/lib/rpush/wpns/notification.rb +0 -26
  181. data/lib/tasks/cane.rake +0 -18
  182. data/lib/tasks/rpush.rake +0 -16
  183. data/spec/unit/apns/app_spec.rb +0 -29
  184. data/spec/unit/apns/feedback_spec.rb +0 -9
  185. data/spec/unit/apns/notification_spec.rb +0 -208
  186. data/spec/unit/app_spec.rb +0 -30
  187. data/spec/unit/daemon/apns/disconnection_error_spec.rb +0 -18
  188. data/spec/unit/daemon/interruptible_sleep_spec.rb +0 -68
  189. data/spec/unit/gcm/app_spec.rb +0 -4
  190. data/spec/unit/notification_spec.rb +0 -15
  191. data/spec/unit/notifier_spec.rb +0 -49
  192. data/spec/unit/wpns/app_spec.rb +0 -4
  193. data/spec/unit/wpns/notification_spec.rb +0 -30
@@ -6,11 +6,11 @@ class AddWpns < ActiveRecord::Migration
6
6
  end
7
7
 
8
8
  def self.up
9
- add_column :rapns_notifications, :uri, :string, :null => true
9
+ add_column :rapns_notifications, :uri, :string, null: true
10
10
  end
11
11
 
12
12
  def self.down
13
- AddWpns::Rapns::Notification.where(:type => 'Rapns::Wpns::Notification').delete_all
13
+ AddWpns::Rapns::Notification.where(type: 'Rapns::Wpns::Notification').delete_all
14
14
  remove_column :rapns_notifications, :uri
15
15
  end
16
16
  end
@@ -1,11 +1,11 @@
1
1
  class CreateRapnsApps < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :rapns_apps do |t|
4
- t.string :key, :null => false
5
- t.string :environment, :null => false
6
- t.text :certificate, :null => false
7
- t.string :password, :null => true
8
- t.integer :connections, :null => false, :default => 1
4
+ t.string :key, null: false
5
+ t.string :environment, null: false
6
+ t.text :certificate, null: false
7
+ t.string :password, null: true
8
+ t.integer :connections, null: false, default: 1
9
9
  t.timestamps
10
10
  end
11
11
  end
@@ -1,8 +1,8 @@
1
1
  class CreateRapnsFeedback < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :rapns_feedback do |t|
4
- t.string :device_token, :null => false, :limit => 64
5
- t.timestamp :failed_at, :null => false
4
+ t.string :device_token, null: false, limit: 64
5
+ t.timestamp :failed_at, null: false
6
6
  t.timestamps
7
7
  end
8
8
 
@@ -1,28 +1,28 @@
1
1
  class CreateRapnsNotifications < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :rapns_notifications do |t|
4
- t.integer :badge, :null => true
5
- t.string :device_token, :null => false, :limit => 64
6
- t.string :sound, :null => true, :default => "1.aiff"
7
- t.string :alert, :null => true
8
- t.text :attributes_for_device, :null => true
9
- t.integer :expiry, :null => false, :default => 1.day.to_i
10
- t.boolean :delivered, :null => false, :default => false
11
- t.timestamp :delivered_at, :null => true
12
- t.boolean :failed, :null => false, :default => false
13
- t.timestamp :failed_at, :null => true
14
- t.integer :error_code, :null => true
15
- t.string :error_description, :null => true
16
- t.timestamp :deliver_after, :null => true
4
+ t.integer :badge, null: true
5
+ t.string :device_token, null: false, limit: 64
6
+ t.string :sound, null: true, default: "1.aiff"
7
+ t.string :alert, null: true
8
+ t.text :attributes_for_device, null: true
9
+ t.integer :expiry, null: false, default: 1.day.to_i
10
+ t.boolean :delivered, null: false, default: false
11
+ t.timestamp :delivered_at, null: true
12
+ t.boolean :failed, null: false, default: false
13
+ t.timestamp :failed_at, null: true
14
+ t.integer :error_code, null: true
15
+ t.string :error_description, null: true
16
+ t.timestamp :deliver_after, null: true
17
17
  t.timestamps
18
18
  end
19
19
 
20
- add_index :rapns_notifications, [:delivered, :failed, :deliver_after], :name => 'index_rapns_notifications_multi'
20
+ add_index :rapns_notifications, [:delivered, :failed, :deliver_after], name: 'index_rapns_notifications_multi'
21
21
  end
22
22
 
23
23
  def self.down
24
24
  if index_name_exists?(:rapns_notifications, 'index_rapns_notifications_multi', true)
25
- remove_index :rapns_notifications, :name => 'index_rapns_notifications_multi'
25
+ remove_index :rapns_notifications, name: 'index_rapns_notifications_multi'
26
26
  end
27
27
  drop_table :rapns_notifications
28
28
  end
@@ -2,6 +2,12 @@
2
2
 
3
3
  Rpush.configure do |config|
4
4
 
5
+ # Supported clients are :active_record and :redis.
6
+ # config.client = :active_record
7
+
8
+ # Options passed to Redis.new
9
+ # config.redis_options = {}
10
+
5
11
  # Run in the foreground?
6
12
  # config.foreground = false
7
13
 
@@ -11,15 +17,12 @@
11
17
  # Frequency in seconds to check for feedback
12
18
  # config.feedback_poll = 60
13
19
 
14
- # Disable APNs error checking after notification delivery.
15
- # config.check_for_errors = true
16
-
17
- # ActiveRecord notifications batch size.
20
+ # The maximum number of notifications to load from the store every `push_poll` seconds.
21
+ # If some notifications are still enqueued internally, Rpush will load the batch_size less
22
+ # the number enqueued. An exception to this is if the service is able to receive multiple
23
+ # notification payloads over the connection with a single write, such as APNs.
18
24
  # config.batch_size = 100
19
25
 
20
- # Perform updates to the storage backend in batches to reduce IO.
21
- # config.batch_storage_updates = true
22
-
23
26
  # Path to write PID file. Relative to Rails root unless absolute.
24
27
  # config.pid_file = '/path/to/rpush.pid'
25
28
 
@@ -0,0 +1,23 @@
1
+ class Rpush200Updates < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :rpush_notifications, :processing, :boolean, null: false, default: false
4
+ add_column :rpush_notifications, :priority, :integer, null: true
5
+
6
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
7
+ remove_index :rpush_notifications, name: :index_rpush_notifications_multi
8
+ end
9
+
10
+ add_index :rpush_notifications, [:processing, :delivered, :failed, :deliver_after], name: 'index_rpush_notifications_multi'
11
+ end
12
+
13
+ def self.down
14
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
15
+ remove_index :rpush_notifications, name: :index_rpush_notifications_multi
16
+ end
17
+
18
+ add_index :rpush_notifications, [:app_id, :delivered, :failed, :deliver_after], name: 'index_rpush_notifications_multi'
19
+
20
+ remove_column :rpush_notifications, :priority
21
+ remove_column :rpush_notifications, :processing
22
+ end
23
+ end
@@ -1,13 +1,11 @@
1
- require 'active_record'
2
1
  require 'multi_json'
2
+ require 'active_support/all'
3
3
 
4
4
  module Rpush
5
5
  def self.attr_accessible_available?
6
6
  require 'rails'
7
7
  ::Rails::VERSION::STRING < '4'
8
8
  end
9
-
10
- require 'rpush/railtie' if defined?(Rails)
11
9
  end
12
10
 
13
11
  require 'rpush/version'
@@ -15,33 +13,11 @@ require 'rpush/deprecation'
15
13
  require 'rpush/deprecatable'
16
14
  require 'rpush/logger'
17
15
  require 'rpush/multi_json_helper'
18
- require 'rpush/notification'
19
- require 'rpush/app'
20
16
  require 'rpush/configuration'
21
17
  require 'rpush/reflection'
22
18
  require 'rpush/embed'
23
19
  require 'rpush/push'
24
20
  require 'rpush/apns_feedback'
25
- require 'rpush/notifier'
26
- require 'rpush/payload_data_size_validator'
27
- require 'rpush/registration_ids_count_validator'
28
-
29
- require 'rpush/apns/binary_notification_validator'
30
- require 'rpush/apns/device_token_format_validator'
31
- require 'rpush/apns/notification'
32
- require 'rpush/apns/feedback'
33
- require 'rpush/apns/app'
34
-
35
- require 'rpush/gcm/expiry_collapse_key_mutual_inclusion_validator'
36
- require 'rpush/gcm/notification'
37
- require 'rpush/gcm/app'
38
-
39
- require 'rpush/wpns/notification'
40
- require 'rpush/wpns/app'
41
-
42
- require 'rpush/adm/data_validator'
43
- require 'rpush/adm/notification'
44
- require 'rpush/adm/app'
45
21
 
46
22
  module Rpush
47
23
  def self.jruby?
@@ -53,10 +29,10 @@ module Rpush
53
29
  end
54
30
 
55
31
  def self.logger
56
- @logger ||= Logger.new(:foreground => Rpush.config.foreground)
32
+ @logger ||= Logger.new
57
33
  end
58
34
 
59
- def self.logger=(logger)
60
- @logger = logger
35
+ class << self
36
+ attr_writer :logger
61
37
  end
62
38
  end
@@ -0,0 +1,21 @@
1
+ require 'active_model'
2
+
3
+ require 'rpush/client/active_model/notification'
4
+ require 'rpush/client/active_model/payload_data_size_validator'
5
+ require 'rpush/client/active_model/registration_ids_count_validator'
6
+
7
+ require 'rpush/client/active_model/apns/binary_notification_validator'
8
+ require 'rpush/client/active_model/apns/device_token_format_validator'
9
+ require 'rpush/client/active_model/apns/app'
10
+ require 'rpush/client/active_model/apns/notification'
11
+
12
+ require 'rpush/client/active_model/adm/data_validator'
13
+ require 'rpush/client/active_model/adm/app'
14
+ require 'rpush/client/active_model/adm/notification'
15
+
16
+ require 'rpush/client/active_model/gcm/expiry_collapse_key_mutual_inclusion_validator'
17
+ require 'rpush/client/active_model/gcm/app'
18
+ require 'rpush/client/active_model/gcm/notification'
19
+
20
+ require 'rpush/client/active_model/wpns/app'
21
+ require 'rpush/client/active_model/wpns/notification'
@@ -0,0 +1,23 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Adm
5
+ module App
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ validates :client_id, :client_secret, presence: true
9
+ end
10
+ end
11
+
12
+ def access_token_expired?
13
+ access_token_expiration.nil? || access_token_expiration < Time.now
14
+ end
15
+
16
+ def service_name
17
+ 'adm'
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Adm
5
+ class DataValidator < ::ActiveModel::Validator
6
+ def validate(record)
7
+ return unless record.collapse_key.nil? && record.data.nil?
8
+ record.errors[:data] << 'must be set unless collapse_key is specified'
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Adm
5
+ module Notification
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ validates :registration_ids, presence: true
9
+
10
+ validates_with Rpush::Client::ActiveModel::PayloadDataSizeValidator, limit: 6144
11
+ validates_with Rpush::Client::ActiveModel::RegistrationIdsCountValidator, limit: 100
12
+
13
+ validates_with Rpush::Client::ActiveModel::Adm::DataValidator
14
+ end
15
+ end
16
+
17
+ def as_json
18
+ json = { 'data' => data }
19
+ json['consolidationKey'] = collapse_key if collapse_key
20
+ # number of seconds before message is expired
21
+ json['expiresAfter'] = expiry if expiry
22
+ json
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Apns
5
+ module App
6
+ def self.included(base)
7
+ base.instance_eval do
8
+ validates :environment, presence: true, inclusion: { in: %w(development production sandbox) }
9
+ validates :certificate, presence: true
10
+ validate :certificate_has_matching_private_key
11
+ end
12
+ end
13
+
14
+ def service_name
15
+ 'apns'
16
+ end
17
+
18
+ private
19
+
20
+ def certificate_has_matching_private_key
21
+ result = false
22
+ if certificate.present?
23
+ begin
24
+ x509 = OpenSSL::X509::Certificate.new(certificate)
25
+ pkey = OpenSSL::PKey::RSA.new(certificate, password)
26
+ result = x509 && pkey
27
+ rescue OpenSSL::OpenSSLError
28
+ errors.add :certificate, 'Certificate value must contain a certificate and a private key.'
29
+ end
30
+ end
31
+ result
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Apns
5
+ class BinaryNotificationValidator < ::ActiveModel::Validator
6
+ MAX_BYTES = 256
7
+
8
+ def validate(record)
9
+ return unless record.payload_size > MAX_BYTES
10
+ record.errors[:base] << "APN notification cannot be larger than #{MAX_BYTES} bytes. Try condensing your alert and device attributes."
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Apns
5
+ class DeviceTokenFormatValidator < ::ActiveModel::Validator
6
+ def validate(record)
7
+ return if record.device_token =~ /^[a-z0-9]{64}$/i
8
+ record.errors[:device_token] << "is invalid"
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,90 @@
1
+ module Rpush
2
+ module Client
3
+ module ActiveModel
4
+ module Apns
5
+ module Notification
6
+ APNS_DEFAULT_EXPIRY = 1.day.to_i
7
+ APNS_PRIORITY_IMMEDIATE = 10
8
+ APNS_PRIORITY_CONSERVE_POWER = 5
9
+ APNS_PRIORITIES = [APNS_PRIORITY_IMMEDIATE, APNS_PRIORITY_CONSERVE_POWER]
10
+
11
+ def self.included(base)
12
+ base.instance_eval do
13
+ validates :device_token, presence: true
14
+ validates :badge, numericality: true, allow_nil: true
15
+ validates :priority, inclusion: { in: APNS_PRIORITIES }, allow_nil: true
16
+
17
+ validates_with Rpush::Client::ActiveModel::Apns::DeviceTokenFormatValidator
18
+ validates_with Rpush::Client::ActiveModel::Apns::BinaryNotificationValidator
19
+
20
+ base.const_set('APNS_DEFAULT_EXPIRY', APNS_DEFAULT_EXPIRY) unless base.const_defined?('APNS_DEFAULT_EXPIRY')
21
+ base.const_set('APNS_PRIORITY_IMMEDIATE', APNS_PRIORITY_IMMEDIATE) unless base.const_defined?('APNS_PRIORITY_IMMEDIATE')
22
+ base.const_set('APNS_PRIORITY_CONSERVE_POWER', APNS_PRIORITY_CONSERVE_POWER) unless base.const_defined?('APNS_PRIORITY_CONSERVE_POWER')
23
+ end
24
+ end
25
+
26
+ def device_token=(token)
27
+ write_attribute(:device_token, token.delete(" <>")) unless token.nil?
28
+ end
29
+
30
+ MDM_KEY = '__rpush_mdm__'
31
+ def mdm=(magic)
32
+ self.data = (data || {}).merge(MDM_KEY => magic)
33
+ end
34
+
35
+ CONTENT_AVAILABLE_KEY = '__rpush_content_available__'
36
+ def content_available=(bool)
37
+ return unless bool
38
+ self.data = (data || {}).merge(CONTENT_AVAILABLE_KEY => true)
39
+ end
40
+
41
+ def as_json
42
+ json = ActiveSupport::OrderedHash.new
43
+
44
+ if data && data.key?(MDM_KEY)
45
+ json['mdm'] = data[MDM_KEY]
46
+ else
47
+ json['aps'] = ActiveSupport::OrderedHash.new
48
+ json['aps']['alert'] = alert if alert
49
+ json['aps']['badge'] = badge if badge
50
+ json['aps']['sound'] = sound if sound
51
+
52
+ if data && data[CONTENT_AVAILABLE_KEY]
53
+ json['aps']['content-available'] = 1
54
+ end
55
+
56
+ if data
57
+ non_aps_attributes = data.reject { |k, _| k == CONTENT_AVAILABLE_KEY }
58
+ non_aps_attributes.each { |k, v| json[k.to_s] = v }
59
+ end
60
+ end
61
+
62
+ json
63
+ end
64
+
65
+ def to_binary(options = {})
66
+ frame_id = options[:for_validation] ? 0 : id
67
+ frame = ""
68
+ frame << [1, 32, device_token].pack("cnH*")
69
+ frame << [2, payload.bytesize, payload].pack("cna*")
70
+ frame << [3, 4, frame_id].pack("cnN")
71
+ frame << [4, 4, expiry || APNS_DEFAULT_EXPIRY].pack("cnN")
72
+ frame << [5, 1, priority_for_frame].pack("cnc")
73
+ [2, frame.bytesize].pack("cN") + frame
74
+ end
75
+
76
+ private
77
+
78
+ def priority_for_frame
79
+ # It is an error to use APNS_PRIORITY_IMMEDIATE for a notification that only contains content-available.
80
+ if as_json['aps'].keys == ['content-available']
81
+ APNS_PRIORITY_CONSERVE_POWER
82
+ else
83
+ priority || APNS_PRIORITY_IMMEDIATE
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end