rpush 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +99 -0
  3. data/LICENSE +7 -0
  4. data/README.md +189 -0
  5. data/bin/rpush +36 -0
  6. data/config/database.yml +44 -0
  7. data/lib/generators/rpush_generator.rb +44 -0
  8. data/lib/generators/templates/add_adm.rb +23 -0
  9. data/lib/generators/templates/add_alert_is_json_to_rapns_notifications.rb +9 -0
  10. data/lib/generators/templates/add_app_to_rapns.rb +11 -0
  11. data/lib/generators/templates/add_fail_after_to_rpush_notifications.rb +9 -0
  12. data/lib/generators/templates/add_gcm.rb +102 -0
  13. data/lib/generators/templates/add_rpush.rb +349 -0
  14. data/lib/generators/templates/add_wpns.rb +16 -0
  15. data/lib/generators/templates/create_rapns_apps.rb +16 -0
  16. data/lib/generators/templates/create_rapns_feedback.rb +18 -0
  17. data/lib/generators/templates/create_rapns_notifications.rb +29 -0
  18. data/lib/generators/templates/rename_rapns_to_rpush.rb +63 -0
  19. data/lib/generators/templates/rpush.rb +104 -0
  20. data/lib/rpush.rb +62 -0
  21. data/lib/rpush/TODO +3 -0
  22. data/lib/rpush/adm/app.rb +15 -0
  23. data/lib/rpush/adm/data_validator.rb +11 -0
  24. data/lib/rpush/adm/notification.rb +29 -0
  25. data/lib/rpush/apns/app.rb +29 -0
  26. data/lib/rpush/apns/binary_notification_validator.rb +12 -0
  27. data/lib/rpush/apns/device_token_format_validator.rb +12 -0
  28. data/lib/rpush/apns/feedback.rb +16 -0
  29. data/lib/rpush/apns/notification.rb +84 -0
  30. data/lib/rpush/apns_feedback.rb +13 -0
  31. data/lib/rpush/app.rb +18 -0
  32. data/lib/rpush/configuration.rb +75 -0
  33. data/lib/rpush/daemon.rb +140 -0
  34. data/lib/rpush/daemon/adm.rb +9 -0
  35. data/lib/rpush/daemon/adm/delivery.rb +222 -0
  36. data/lib/rpush/daemon/apns.rb +16 -0
  37. data/lib/rpush/daemon/apns/certificate_expired_error.rb +20 -0
  38. data/lib/rpush/daemon/apns/delivery.rb +64 -0
  39. data/lib/rpush/daemon/apns/disconnection_error.rb +20 -0
  40. data/lib/rpush/daemon/apns/feedback_receiver.rb +79 -0
  41. data/lib/rpush/daemon/app_runner.rb +187 -0
  42. data/lib/rpush/daemon/batch.rb +115 -0
  43. data/lib/rpush/daemon/constants.rb +59 -0
  44. data/lib/rpush/daemon/delivery.rb +28 -0
  45. data/lib/rpush/daemon/delivery_error.rb +19 -0
  46. data/lib/rpush/daemon/dispatcher/http.rb +21 -0
  47. data/lib/rpush/daemon/dispatcher/tcp.rb +30 -0
  48. data/lib/rpush/daemon/dispatcher_loop.rb +54 -0
  49. data/lib/rpush/daemon/dispatcher_loop_collection.rb +33 -0
  50. data/lib/rpush/daemon/feeder.rb +68 -0
  51. data/lib/rpush/daemon/gcm.rb +9 -0
  52. data/lib/rpush/daemon/gcm/delivery.rb +222 -0
  53. data/lib/rpush/daemon/interruptible_sleep.rb +61 -0
  54. data/lib/rpush/daemon/loggable.rb +31 -0
  55. data/lib/rpush/daemon/reflectable.rb +13 -0
  56. data/lib/rpush/daemon/retry_header_parser.rb +23 -0
  57. data/lib/rpush/daemon/retryable_error.rb +20 -0
  58. data/lib/rpush/daemon/service_config_methods.rb +33 -0
  59. data/lib/rpush/daemon/store/active_record.rb +154 -0
  60. data/lib/rpush/daemon/store/active_record/reconnectable.rb +68 -0
  61. data/lib/rpush/daemon/tcp_connection.rb +143 -0
  62. data/lib/rpush/daemon/too_many_requests_error.rb +20 -0
  63. data/lib/rpush/daemon/wpns.rb +9 -0
  64. data/lib/rpush/daemon/wpns/delivery.rb +132 -0
  65. data/lib/rpush/deprecatable.rb +23 -0
  66. data/lib/rpush/deprecation.rb +23 -0
  67. data/lib/rpush/embed.rb +28 -0
  68. data/lib/rpush/gcm/app.rb +11 -0
  69. data/lib/rpush/gcm/expiry_collapse_key_mutual_inclusion_validator.rb +11 -0
  70. data/lib/rpush/gcm/notification.rb +30 -0
  71. data/lib/rpush/logger.rb +63 -0
  72. data/lib/rpush/multi_json_helper.rb +16 -0
  73. data/lib/rpush/notification.rb +69 -0
  74. data/lib/rpush/notifier.rb +52 -0
  75. data/lib/rpush/payload_data_size_validator.rb +10 -0
  76. data/lib/rpush/push.rb +16 -0
  77. data/lib/rpush/railtie.rb +11 -0
  78. data/lib/rpush/reflection.rb +58 -0
  79. data/lib/rpush/registration_ids_count_validator.rb +10 -0
  80. data/lib/rpush/version.rb +3 -0
  81. data/lib/rpush/wpns/app.rb +9 -0
  82. data/lib/rpush/wpns/notification.rb +26 -0
  83. data/lib/tasks/cane.rake +18 -0
  84. data/lib/tasks/rpush.rake +16 -0
  85. data/lib/tasks/test.rake +38 -0
  86. data/spec/functional/adm_spec.rb +43 -0
  87. data/spec/functional/apns_spec.rb +58 -0
  88. data/spec/functional/embed_spec.rb +49 -0
  89. data/spec/functional/gcm_spec.rb +42 -0
  90. data/spec/functional/wpns_spec.rb +41 -0
  91. data/spec/support/cert_with_password.pem +90 -0
  92. data/spec/support/cert_without_password.pem +59 -0
  93. data/spec/support/install.sh +32 -0
  94. data/spec/support/simplecov_helper.rb +20 -0
  95. data/spec/support/simplecov_quality_formatter.rb +8 -0
  96. data/spec/tmp/.gitkeep +0 -0
  97. data/spec/unit/adm/app_spec.rb +58 -0
  98. data/spec/unit/adm/notification_spec.rb +45 -0
  99. data/spec/unit/apns/app_spec.rb +29 -0
  100. data/spec/unit/apns/feedback_spec.rb +9 -0
  101. data/spec/unit/apns/notification_spec.rb +208 -0
  102. data/spec/unit/apns_feedback_spec.rb +21 -0
  103. data/spec/unit/app_spec.rb +30 -0
  104. data/spec/unit/configuration_spec.rb +45 -0
  105. data/spec/unit/daemon/adm/delivery_spec.rb +243 -0
  106. data/spec/unit/daemon/apns/certificate_expired_error_spec.rb +11 -0
  107. data/spec/unit/daemon/apns/delivery_spec.rb +101 -0
  108. data/spec/unit/daemon/apns/disconnection_error_spec.rb +18 -0
  109. data/spec/unit/daemon/apns/feedback_receiver_spec.rb +117 -0
  110. data/spec/unit/daemon/app_runner_spec.rb +292 -0
  111. data/spec/unit/daemon/batch_spec.rb +232 -0
  112. data/spec/unit/daemon/delivery_error_spec.rb +13 -0
  113. data/spec/unit/daemon/delivery_spec.rb +38 -0
  114. data/spec/unit/daemon/dispatcher/http_spec.rb +33 -0
  115. data/spec/unit/daemon/dispatcher/tcp_spec.rb +38 -0
  116. data/spec/unit/daemon/dispatcher_loop_collection_spec.rb +37 -0
  117. data/spec/unit/daemon/dispatcher_loop_spec.rb +71 -0
  118. data/spec/unit/daemon/feeder_spec.rb +98 -0
  119. data/spec/unit/daemon/gcm/delivery_spec.rb +310 -0
  120. data/spec/unit/daemon/interruptible_sleep_spec.rb +68 -0
  121. data/spec/unit/daemon/reflectable_spec.rb +27 -0
  122. data/spec/unit/daemon/retryable_error_spec.rb +14 -0
  123. data/spec/unit/daemon/service_config_methods_spec.rb +33 -0
  124. data/spec/unit/daemon/store/active_record/reconnectable_spec.rb +114 -0
  125. data/spec/unit/daemon/store/active_record_spec.rb +357 -0
  126. data/spec/unit/daemon/tcp_connection_spec.rb +287 -0
  127. data/spec/unit/daemon/too_many_requests_error_spec.rb +14 -0
  128. data/spec/unit/daemon/wpns/delivery_spec.rb +159 -0
  129. data/spec/unit/daemon_spec.rb +159 -0
  130. data/spec/unit/deprecatable_spec.rb +32 -0
  131. data/spec/unit/deprecation_spec.rb +15 -0
  132. data/spec/unit/embed_spec.rb +50 -0
  133. data/spec/unit/gcm/app_spec.rb +4 -0
  134. data/spec/unit/gcm/notification_spec.rb +36 -0
  135. data/spec/unit/logger_spec.rb +127 -0
  136. data/spec/unit/notification_shared.rb +105 -0
  137. data/spec/unit/notification_spec.rb +15 -0
  138. data/spec/unit/notifier_spec.rb +49 -0
  139. data/spec/unit/push_spec.rb +43 -0
  140. data/spec/unit/reflection_spec.rb +30 -0
  141. data/spec/unit/rpush_spec.rb +9 -0
  142. data/spec/unit/wpns/app_spec.rb +4 -0
  143. data/spec/unit/wpns/notification_spec.rb +30 -0
  144. data/spec/unit_spec_helper.rb +101 -0
  145. metadata +276 -0
@@ -0,0 +1,16 @@
1
+ class AddWpns < ActiveRecord::Migration
2
+ module Rapns
3
+ class Notification < ActiveRecord::Base
4
+ self.table_name = 'rapns_notifications'
5
+ end
6
+ end
7
+
8
+ def self.up
9
+ add_column :rapns_notifications, :uri, :string, :null => true
10
+ end
11
+
12
+ def self.down
13
+ AddWpns::Rapns::Notification.where(:type => 'Rapns::Wpns::Notification').delete_all
14
+ remove_column :rapns_notifications, :uri
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ class CreateRapnsApps < ActiveRecord::Migration
2
+ def self.up
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
9
+ t.timestamps
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :rapns_apps
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class CreateRapnsFeedback < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :rapns_feedback do |t|
4
+ t.string :device_token, :null => false, :limit => 64
5
+ t.timestamp :failed_at, :null => false
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :rapns_feedback, :device_token
10
+ end
11
+
12
+ def self.down
13
+ if index_name_exists?(:rapns_feedback, :index_rapns_feedback_on_device_token, true)
14
+ remove_index :rapns_feedback, name: :index_rapns_feedback_on_device_token
15
+ end
16
+ drop_table :rapns_feedback
17
+ end
18
+ end
@@ -0,0 +1,29 @@
1
+ class CreateRapnsNotifications < ActiveRecord::Migration
2
+ def self.up
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
17
+ t.timestamps
18
+ end
19
+
20
+ add_index :rapns_notifications, [:delivered, :failed, :deliver_after], :name => 'index_rapns_notifications_multi'
21
+ end
22
+
23
+ def self.down
24
+ if index_name_exists?(:rapns_notifications, 'index_rapns_notifications_multi', true)
25
+ remove_index :rapns_notifications, :name => 'index_rapns_notifications_multi'
26
+ end
27
+ drop_table :rapns_notifications
28
+ end
29
+ end
@@ -0,0 +1,63 @@
1
+ class RenameRapnsToRpush < ActiveRecord::Migration
2
+ module Rpush
3
+ class App < ActiveRecord::Base
4
+ self.table_name = 'rpush_apps'
5
+ end
6
+
7
+ class Notification < ActiveRecord::Base
8
+ self.table_name = 'rpush_notifications'
9
+ end
10
+ end
11
+
12
+ def self.update_type(model, from, to)
13
+ model.where(type: from).update_all(type: to)
14
+ end
15
+
16
+ def self.up
17
+ rename_table :rapns_notifications, :rpush_notifications
18
+ rename_table :rapns_apps, :rpush_apps
19
+ rename_table :rapns_feedback, :rpush_feedback
20
+
21
+ if index_name_exists?(:rpush_notifications, :index_rapns_notifications_multi, true)
22
+ rename_index :rpush_notifications, :index_rapns_notifications_multi, :index_rpush_notifications_multi
23
+ end
24
+
25
+ if index_name_exists?(:rpush_feedback, :index_rapns_feedback_on_device_token, true)
26
+ rename_index :rpush_feedback, :index_rapns_feedback_on_device_token, :index_rpush_feedback_on_device_token
27
+ end
28
+
29
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rapns::Apns::Notification', 'Rpush::Apns::Notification')
30
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rapns::Gcm::Notification', 'Rpush::Gcm::Notification')
31
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rapns::Adm::Notification', 'Rpush::Adm::Notification')
32
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rapns::Wpns::Notification', 'Rpush::Wpns::Notification')
33
+
34
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rapns::Apns::App', 'Rpush::Apns::App')
35
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rapns::Gcm::App', 'Rpush::Gcm::App')
36
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rapns::Adm::App', 'Rpush::Adm::App')
37
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rapns::Wpns::App', 'Rpush::Wpns::App')
38
+ end
39
+
40
+ def self.down
41
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rpush::Apns::Notification', 'Rapns::Apns::Notification')
42
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rpush::Gcm::Notification', 'Rapns::Gcm::Notification')
43
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rpush::Adm::Notification', 'Rapns::Adm::Notification')
44
+ update_type(RenameRapnsToRpush::Rpush::Notification, 'Rpush::Wpns::Notification', 'Rapns::Wpns::Notification')
45
+
46
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Apns::App', 'Rapns::Apns::App')
47
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Gcm::App', 'Rapns::Gcm::App')
48
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Adm::App', 'Rapns::Adm::App')
49
+ update_type(RenameRapnsToRpush::Rpush::App, 'Rpush::Wpns::App', 'Rapns::Wpns::App')
50
+
51
+ if index_name_exists?(:rpush_notifications, :index_rpush_notifications_multi, true)
52
+ rename_index :rpush_notifications, :index_rpush_notifications_multi, :index_rapns_notifications_multi
53
+ end
54
+
55
+ if index_name_exists?(:rpush_feedback, :index_rpush_feedback_on_device_token, true)
56
+ rename_index :rpush_feedback, :index_rpush_feedback_on_device_token, :index_rapns_feedback_on_device_token
57
+ end
58
+
59
+ rename_table :rpush_notifications, :rapns_notifications
60
+ rename_table :rpush_apps, :rapns_apps
61
+ rename_table :rpush_feedback, :rapns_feedback
62
+ end
63
+ end
@@ -0,0 +1,104 @@
1
+ # Rpush configuration. Options set here are overridden by command-line options.
2
+
3
+ Rpush.configure do |config|
4
+
5
+ # Run in the foreground?
6
+ # config.foreground = false
7
+
8
+ # Frequency in seconds to check for new notifications.
9
+ # config.push_poll = 2
10
+
11
+ # Frequency in seconds to check for feedback
12
+ # config.feedback_poll = 60
13
+
14
+ # Disable APNs error checking after notification delivery.
15
+ # config.check_for_errors = true
16
+
17
+ # ActiveRecord notifications batch size.
18
+ # config.batch_size = 100
19
+
20
+ # Perform updates to the storage backend in batches to reduce IO.
21
+ # config.batch_storage_updates = true
22
+
23
+ # Path to write PID file. Relative to Rails root unless absolute.
24
+ # config.pid_file = '/path/to/rpush.pid'
25
+
26
+ # Define a custom logger.
27
+ # config.logger = MyLogger.new
28
+
29
+ end
30
+
31
+ Rpush.reflect do |on|
32
+
33
+ # Called with a Rpush::Apns::Feedback instance when feedback is received
34
+ # from the APNs that a notification has failed to be delivered.
35
+ # Further notifications should not be sent to the device.
36
+ # on.apns_feedback do |feedback|
37
+ # end
38
+
39
+ # Called when a notification is queued internally for delivery.
40
+ # The internal queue for each app runner can be inspected:
41
+ #
42
+ # Rpush::Daemon::AppRunner.runners.each do |app_id, runner|
43
+ # runner.app
44
+ # runner.queue_size
45
+ # end
46
+ #
47
+ # on.notification_enqueued do |notification|
48
+ # end
49
+
50
+ # Called when a notification is successfully delivered.
51
+ # on.notification_delivered do |notification|
52
+ # end
53
+
54
+ # Called when notification delivery failed.
55
+ # Call 'error_code' and 'error_description' on the notification for the cause.
56
+ # on.notification_failed do |notification|
57
+ # end
58
+
59
+ # Called when a notification will be retried at a later date.
60
+ # Call 'deliver_after' on the notification for the next delivery date
61
+ # and 'retries' for the number of times this notification has been retried.
62
+ # on.notification_will_retry do |notification|
63
+ # end
64
+
65
+ # Called when a TCP connection is lost and will be reconnected.
66
+ # on.tcp_connection_lost do |app, error|
67
+ # end
68
+
69
+ # Called for each recipient which successfully receives a notification. This
70
+ # can occur more than once for the same notification when there are multiple
71
+ # recipients.
72
+ # on.gcm_delivered_to_recipient do |notification, registration_id|
73
+ # end
74
+
75
+ # Called for each recipient which fails to receive a notification. This
76
+ # can occur more than once for the same notification when there are multiple
77
+ # recipients. (do not handle invalid registration IDs here)
78
+ # on.gcm_failed_to_recipient do |notification, error, registration_id|
79
+ # end
80
+
81
+ # Called when the GCM returns a canonical registration ID.
82
+ # You will need to replace old_id with canonical_id in your records.
83
+ # on.gcm_canonical_id do |old_id, canonical_id|
84
+ # end
85
+
86
+ # Called when the GCM returns a failure that indicates an invalid registration id.
87
+ # You will need to delete the registration_id from your records.
88
+ # on.gcm_invalid_registration_id do |app, error, registration_id|
89
+ # end
90
+
91
+ # Called when an SSL certificate will expire within 1 month.
92
+ # Implement on.error to catch errors raised when the certificate expires.
93
+ # on.ssl_certificate_will_expire do |app, expiration_time|
94
+ # end
95
+
96
+ # Called when the ADM returns a canonical registration ID.
97
+ # You will need to replace old_id with canonical_id in your records.
98
+ # on.adm_canonical_id do |old_id, canonical_id|
99
+ # end
100
+
101
+ # Called when an exception is raised.
102
+ # on.error do |error|
103
+ # end
104
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_record'
2
+ require 'multi_json'
3
+
4
+ module Rpush
5
+ def self.attr_accessible_available?
6
+ require 'rails'
7
+ ::Rails::VERSION::STRING < '4'
8
+ end
9
+
10
+ require 'rpush/railtie' if defined?(Rails)
11
+ end
12
+
13
+ require 'rpush/version'
14
+ require 'rpush/deprecation'
15
+ require 'rpush/deprecatable'
16
+ require 'rpush/logger'
17
+ require 'rpush/multi_json_helper'
18
+ require 'rpush/notification'
19
+ require 'rpush/app'
20
+ require 'rpush/configuration'
21
+ require 'rpush/reflection'
22
+ require 'rpush/embed'
23
+ require 'rpush/push'
24
+ 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
+
46
+ module Rpush
47
+ def self.jruby?
48
+ defined? JRUBY_VERSION
49
+ end
50
+
51
+ def self.require_for_daemon
52
+ require 'rpush/daemon'
53
+ end
54
+
55
+ def self.logger
56
+ @logger ||= Logger.new(:foreground => Rpush.config.foreground)
57
+ end
58
+
59
+ def self.logger=(logger)
60
+ @logger = logger
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ * Should internal errors mark a notification as failed?
2
+ * Check cert expiry when connected.
3
+ * Lazy connect on socket write? - would allow to catch initial expiry error and mark on notification.
@@ -0,0 +1,15 @@
1
+ module Rpush
2
+ module Adm
3
+ class App < Rpush::App
4
+ validates :client_id, :client_secret, :presence => true
5
+
6
+ def access_token_expired?
7
+ self.access_token_expiration.nil? || self.access_token_expiration < Time.now
8
+ end
9
+
10
+ def service_name
11
+ 'adm'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module Rpush
2
+ module Adm
3
+ class DataValidator < ActiveModel::Validator
4
+ def validate(record)
5
+ if record.collapse_key.nil? && record.data.nil?
6
+ record.errors[:data] << "must be set unless collapse_key is specified"
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module Rpush
2
+ module Adm
3
+ class Notification < Rpush::Notification
4
+ validates :registration_ids, :presence => true
5
+
6
+ validates_with Rpush::PayloadDataSizeValidator, limit: 6144
7
+ validates_with Rpush::RegistrationIdsCountValidator, limit: 100
8
+
9
+ validates_with Rpush::Adm::DataValidator
10
+
11
+ def as_json
12
+ json = {
13
+ 'data' => data
14
+ }
15
+
16
+ if collapse_key
17
+ json['consolidationKey'] = collapse_key
18
+ end
19
+
20
+ # number of seconds before message is expired
21
+ if expiry
22
+ json['expiresAfter'] = expiry
23
+ end
24
+
25
+ json
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module Rpush
2
+ module Apns
3
+ class App < Rpush::App
4
+ validates :environment, :presence => true, :inclusion => { :in => %w(development production sandbox) }
5
+ validates :certificate, :presence => true
6
+ validate :certificate_has_matching_private_key
7
+
8
+ def service_name
9
+ 'apns'
10
+ end
11
+
12
+ private
13
+
14
+ def certificate_has_matching_private_key
15
+ result = false
16
+ if certificate.present?
17
+ begin
18
+ x509 = OpenSSL::X509::Certificate.new(certificate)
19
+ pkey = OpenSSL::PKey::RSA.new(certificate, password)
20
+ result = !x509.nil? && !pkey.nil?
21
+ rescue OpenSSL::OpenSSLError
22
+ errors.add :certificate, 'Certificate value must contain a certificate and a private key.'
23
+ end
24
+ end
25
+ result
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ module Rpush
2
+ module Apns
3
+ class BinaryNotificationValidator < ActiveModel::Validator
4
+
5
+ def validate(record)
6
+ if record.payload_size > 256
7
+ record.errors[:base] << "APN notification cannot be larger than 256 bytes. Try condensing your alert and device attributes."
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Rpush
2
+ module Apns
3
+ class DeviceTokenFormatValidator < ActiveModel::Validator
4
+
5
+ def validate(record)
6
+ if record.device_token !~ /^[a-z0-9]{64}$/i
7
+ record.errors[:device_token] << "is invalid"
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ module Rpush
2
+ module Apns
3
+ class Feedback < ActiveRecord::Base
4
+ self.table_name = 'rpush_feedback'
5
+
6
+ if Rpush.attr_accessible_available?
7
+ attr_accessible :device_token, :failed_at, :app
8
+ end
9
+
10
+ validates :device_token, :presence => true
11
+ validates :failed_at, :presence => true
12
+
13
+ validates_with Rpush::Apns::DeviceTokenFormatValidator
14
+ end
15
+ end
16
+ end