ruby-push-notifications 0.2.2 → 0.3.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: a7b8ecbc7d2752899f6c7081d0c849fd7dd9cc7f
4
- data.tar.gz: 97f72204c96b4a227e129d66db78e1150f4feaa8
3
+ metadata.gz: 7a7eaae46825cd8916811bc2026c97c0714d9d65
4
+ data.tar.gz: 991a6c2960c32cc9de04cb0ef84daac41863da33
5
5
  SHA512:
6
- metadata.gz: 69c53b62f014059b8ca6ab9e516b2aa7551120ad503359bacda5b317923ca1b770e5c393358f573149a805d0cd78e9023f582629fd05328e99a46477c487c6e6
7
- data.tar.gz: 18c6182eda7142c486e0cbca88e5e04193bfb9282631c2e2a4ffa9fb9a8575b2e0754875f9ef100f23accf0f7788c04a1db5d93468741570e49bedcbe00e0fd9
6
+ metadata.gz: e536c3e750f114ad262920acbd63c9f3dd020aaded4088ac718725a33d87b558ce2caf237cf2a7aa9c41ca98300982a4280f64b587872d18380fd1581435b11e
7
+ data.tar.gz: 2118a29ead19bafc9edd4ac2b58333e11dbd060395dc35245f9cc2093b13df0a40a5e2cc33eded400e55a7c3b138bbd5ff4561d75fa190f2e6e2e7612bd1cd44
data/.ruby-style.yml CHANGED
@@ -507,7 +507,7 @@ Metrics/CyclomaticComplexity:
507
507
  Metrics/LineLength:
508
508
  Description: Limit lines to 80 characters.
509
509
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#80-character-limits
510
- Enabled: true
510
+ Enabled: false
511
511
  Max: 80
512
512
  AllowURI: true
513
513
  URISchemes:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby-push-notifications (0.2.2)
4
+ ruby-push-notifications (0.3.0)
5
5
  builder (~> 3.2)
6
6
 
7
7
  GEM
data/examples/apns.rb CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env bundle exec ruby
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
@@ -13,4 +13,7 @@ notification = RubyPushNotifications::APNS::APNSNotification.new tokens, { aps:
13
13
 
14
14
  pusher = RubyPushNotifications::APNS::APNSPusher.new(File.read('/path/to/your/apps/certificate.pem'), true)
15
15
  pusher.push [notification]
16
- p notification.results
16
+ p 'Notification sending results:'
17
+ p "Success: #{notification.success}, Failed: #{notification.failed}"
18
+ p 'Details:'
19
+ p notification.individual_results
data/examples/gcm.rb CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env bundle exec ruby
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
@@ -13,4 +13,7 @@ notification = RubyPushNotifications::GCM::GCMNotification.new registration_ids,
13
13
 
14
14
  pusher = RubyPushNotifications::GCM::GCMPusher.new "Your app's GCM key"
15
15
  pusher.push [notification]
16
- p notification.results
16
+ p 'Notification sending results:'
17
+ p "Success: #{notification.success}, Failed: #{notification.failed}"
18
+ p 'Details:'
19
+ p notification.individual_results
data/examples/mpns.rb CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env bundle exec ruby
2
2
 
3
3
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
4
 
@@ -14,4 +14,7 @@ notification = RubyPushNotifications::MPNS::MPNSNotification.new device_urls, {
14
14
 
15
15
  pusher = RubyPushNotifications::MPNS::MPNSPusher.new
16
16
  pusher.push [notification]
17
- p notification.results
17
+ p 'Notification sending results:'
18
+ p "Success: #{notification.success}, Failed: #{notification.failed}"
19
+ p 'Details:'
20
+ p notification.individual_results
@@ -10,13 +10,11 @@ module RubyPushNotifications
10
10
  #
11
11
  # @author Carlos Alonso
12
12
  class APNSNotification
13
+ include RubyPushNotifications::NotificationResultsManager
13
14
 
14
15
  # @private. 4 weeks in seconds
15
16
  WEEKS_4 = 2419200
16
17
 
17
- # @return [Array]. Array with the results from sending this notification
18
- attr_accessor :results
19
-
20
18
  # Initializes the APNS Notification
21
19
  #
22
20
  # @param [Array]. Array containing all destinations for the notification
@@ -64,7 +64,7 @@ module RubyPushNotifications
64
64
  conn.close
65
65
 
66
66
  notifications.each do |notif|
67
- notif.results = results.slice! 0, notif.count
67
+ notif.results = APNSResults.new(results.slice! 0, notif.count)
68
68
  end
69
69
  end
70
70
  end
@@ -0,0 +1,30 @@
1
+ module RubyPushNotifications
2
+ module APNS
3
+ # This class is responsible for holding and
4
+ # managing result of a pushed notification.
5
+ #
6
+ # @author Carlos Alonso
7
+ class APNSResults
8
+
9
+ # @return [Array] of each destination's individual result.
10
+ attr_reader :individual_results
11
+
12
+ # Initializes the result
13
+ #
14
+ # @param [Array] containing each destination's individual result.
15
+ def initialize(results)
16
+ @individual_results = results
17
+ end
18
+
19
+ # @return [Integer] numer of successfully pushed notifications
20
+ def success
21
+ @success ||= individual_results.count { |r| r == NO_ERROR_STATUS_CODE }
22
+ end
23
+
24
+ # @return [Integer] number of failed notifications
25
+ def failed
26
+ @failed ||= individual_results.count { |r| r != NO_ERROR_STATUS_CODE }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -41,4 +41,5 @@ end
41
41
  require 'ruby-push-notifications/apns/apns_connection'
42
42
  require 'ruby-push-notifications/apns/apns_notification'
43
43
  require 'ruby-push-notifications/apns/apns_pusher'
44
+ require 'ruby-push-notifications/apns/apns_results'
44
45
 
@@ -7,9 +7,7 @@ module RubyPushNotifications
7
7
  #
8
8
  # @author Carlos Alonso
9
9
  class GCMNotification
10
-
11
- # @return [Array]. Array with the results from sending this notification
12
- attr_accessor :results
10
+ include RubyPushNotifications::NotificationResultsManager
13
11
 
14
12
  # Initializes the notification
15
13
  #
@@ -21,6 +21,7 @@ module RubyPushNotifications
21
21
  # @return [Array] Array of a GCMResult for every receiver of the notification
22
22
  # sent indicating the result of the operation.
23
23
  attr_reader :results
24
+ alias_method :individual_results, :results
24
25
 
25
26
  # Initializes the GCMResponse and runs response parsing
26
27
  #
@@ -7,9 +7,7 @@ module RubyPushNotifications
7
7
  # (http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202945)
8
8
  #
9
9
  class MPNSNotification
10
-
11
- # @return [MPNSResponse]. MPNSResponse with the results from sending this notification.
12
- attr_accessor :results
10
+ include RubyPushNotifications::NotificationResultsManager
13
11
 
14
12
  # @return [Hash]. Payload to send.
15
13
  # Toast :title => a bold message
@@ -16,6 +16,7 @@ module RubyPushNotifications
16
16
  # @return [Array] Array of a MPNSResult for every receiver of the notification
17
17
  # sent indicating the result of the operation.
18
18
  attr_reader :results
19
+ alias_method :individual_results, :results
19
20
 
20
21
  # Initializes the MPNSResponse and runs response parsing
21
22
  #
@@ -32,7 +32,7 @@ module RubyPushNotifications
32
32
  # @return [String]. The subscription status.
33
33
  attr_accessor :subscription_status
34
34
 
35
- def initialize(device_url, headers = nil)
35
+ def initialize(device_url, headers)
36
36
  @device_url = device_url
37
37
  @notification_status = headers[X_NOTIFICATION_STATUS]
38
38
  @device_connection_status = headers[X_DEVICE_CONNECTION_STATUS]
@@ -0,0 +1,14 @@
1
+ module RubyPushNotifications
2
+ # This module contains the required behavior expected by particular notifications
3
+ #
4
+ # @author Carlos Alonso
5
+ module NotificationResultsManager
6
+ extend Forwardable
7
+
8
+ def_delegators :@results, :success, :failed, :individual_results
9
+
10
+ # The corresponding object with the results from sending this notification
11
+ # that also will respond to #success, #failed and #individual_results
12
+ attr_accessor :results
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyPushNotifications
2
- VERSION = "0.2.2"
2
+ VERSION = '0.3.0'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  require 'ruby-push-notifications/version'
2
+ require 'ruby-push-notifications/notification_results_manager'
2
3
  require 'ruby-push-notifications/apns'
3
4
  require 'ruby-push-notifications/gcm'
4
5
  require 'ruby-push-notifications/mpns'
@@ -2,28 +2,39 @@
2
2
  module RubyPushNotifications
3
3
  module APNS
4
4
  describe APNSNotification do
5
- describe 'building messages for each token' do
6
5
 
7
- let(:device_tokens) { ['12', '34'] }
8
- let(:data) { { a: 1 } }
9
- let(:notification) { APNSNotification.new device_tokens, data }
10
- let(:notif_id) { 1 }
6
+ let(:device_tokens) { ['12', '34'] }
7
+ let(:data) { { a: 1 } }
8
+ let(:notification) { APNSNotification.new device_tokens, data }
9
+ let(:notif_id) { 1 }
11
10
 
12
- it 'successfully creates the apns binary' do
13
- json = JSON.dump data
14
- expect do |b|
15
- notification.each_message notif_id, &b
16
- end.to yield_successive_args apns_binary(json, device_tokens[0], notif_id), apns_binary(json, device_tokens[1], notif_id+1)
17
- end
11
+ it 'successfully creates the apns binary' do
12
+ json = JSON.dump data
13
+ expect do |b|
14
+ notification.each_message notif_id, &b
15
+ end.to yield_successive_args apns_binary(json, device_tokens[0], notif_id), apns_binary(json, device_tokens[1], notif_id + 1)
16
+ end
18
17
 
19
- it 'caches the payload' do
20
- expect(JSON).to receive(:dump).with(data).once.and_return 'dummy string'
21
- notification.each_message(notif_id) { }
22
- end
18
+ it 'caches the payload' do
19
+ expect(JSON).to receive(:dump).with(data).once.and_return 'dummy string'
20
+ notification.each_message(notif_id) {}
21
+ end
22
+
23
+ it 'validates the data'
23
24
 
24
- it 'validates the data'
25
+ it 'validates the tokens'
25
26
 
26
- it 'validates the tokens'
27
+ it_behaves_like 'a proper results manager' do
28
+ let(:success_count) { 2 }
29
+ let(:failures_count) { 1 }
30
+ let(:individual_results) do
31
+ [
32
+ NO_ERROR_STATUS_CODE,
33
+ PROCESSING_ERROR_STATUS_CODE,
34
+ NO_ERROR_STATUS_CODE
35
+ ]
36
+ end
37
+ let(:results) { APNSResults.new individual_results }
27
38
  end
28
39
  end
29
40
  end
@@ -36,10 +36,10 @@ module RubyPushNotifications
36
36
  pusher.push [notification]
37
37
  end
38
38
 
39
- it 'saves the results into the notification' do
40
- expect do
41
- pusher.push [notification]
42
- end.to change { notification.results }.from(nil).to [NO_ERROR_STATUS_CODE]
39
+ describe 'results' do
40
+ before { pusher.push [notification] }
41
+
42
+ include_examples 'right results', 1, 0, [NO_ERROR_STATUS_CODE]
43
43
  end
44
44
  end
45
45
 
@@ -55,10 +55,10 @@ module RubyPushNotifications
55
55
  pusher.push [notification]
56
56
  end
57
57
 
58
- it 'saves the error' do
59
- expect do
60
- pusher.push [notification]
61
- end.to change { notification.results }.from(nil).to [PROCESSING_ERROR_STATUS_CODE]
58
+ describe 'results' do
59
+ before { pusher.push [notification] }
60
+
61
+ include_examples 'right results', 0, 1, [PROCESSING_ERROR_STATUS_CODE]
62
62
  end
63
63
  end
64
64
  end
@@ -82,10 +82,10 @@ module RubyPushNotifications
82
82
  pusher.push [notification]
83
83
  end
84
84
 
85
- it 'saves the results into the notification' do
86
- expect do
87
- pusher.push [notification]
88
- end.to change { notification.results }.from(nil).to [NO_ERROR_STATUS_CODE, NO_ERROR_STATUS_CODE]
85
+ describe 'results' do
86
+ before { pusher.push [notification] }
87
+
88
+ include_examples 'right results', 2, 0, [NO_ERROR_STATUS_CODE] * 2
89
89
  end
90
90
  end
91
91
 
@@ -109,10 +109,10 @@ module RubyPushNotifications
109
109
  pusher.push [notification]
110
110
  end
111
111
 
112
- it 'stores the error' do
113
- expect do
114
- pusher.push [notification]
115
- end.to change { notification.results }.from(nil).to [PROCESSING_ERROR_STATUS_CODE, NO_ERROR_STATUS_CODE]
112
+ describe 'results' do
113
+ before { pusher.push [notification] }
114
+
115
+ include_examples 'right results', 1, 1, [PROCESSING_ERROR_STATUS_CODE, NO_ERROR_STATUS_CODE]
116
116
  end
117
117
  end
118
118
 
@@ -129,10 +129,10 @@ module RubyPushNotifications
129
129
  pusher.push [notification]
130
130
  end
131
131
 
132
- it 'stores the error' do
133
- expect do
134
- pusher.push [notification]
135
- end.to change { notification.results }.from(nil).to [PROCESSING_ERROR_STATUS_CODE, NO_ERROR_STATUS_CODE]
132
+ describe 'results' do
133
+ before { pusher.push [notification] }
134
+
135
+ include_examples 'right results', 1, 1, [PROCESSING_ERROR_STATUS_CODE, NO_ERROR_STATUS_CODE]
136
136
  end
137
137
  end
138
138
 
@@ -149,10 +149,10 @@ module RubyPushNotifications
149
149
  pusher.push [notification]
150
150
  end
151
151
 
152
- it 'stores the error' do
153
- expect do
154
- pusher.push [notification]
155
- end.to change { notification.results }.from(nil).to [NO_ERROR_STATUS_CODE, PROCESSING_ERROR_STATUS_CODE]
152
+ describe 'results' do
153
+ before { pusher.push [notification] }
154
+
155
+ include_examples 'right results', 1, 1, [NO_ERROR_STATUS_CODE, PROCESSING_ERROR_STATUS_CODE]
156
156
  end
157
157
  end
158
158
  end
@@ -180,9 +180,8 @@ module RubyPushNotifications
180
180
  end
181
181
 
182
182
  it 'saves results' do
183
- expect do
184
- pusher.push notifications
185
- end.to change { notifications.map { |n| n.results } }.from([nil]*10).to([[NO_ERROR_STATUS_CODE]]*10)
183
+ pusher.push notifications
184
+ expect(notifications.map(&:individual_results)).to eq [[NO_ERROR_STATUS_CODE]] * 10
186
185
  end
187
186
  end
188
187
 
@@ -224,9 +223,8 @@ module RubyPushNotifications
224
223
  end
225
224
 
226
225
  it 'saves the statuses' do
227
- expect do
228
- pusher.push notifications
229
- end.to change { notifications.map { |n| n.results } }.from([nil]*10).to [
226
+ pusher.push notifications
227
+ expect(notifications.map(&:individual_results)).to eq [
230
228
  [PROCESSING_ERROR_STATUS_CODE],
231
229
  [NO_ERROR_STATUS_CODE],
232
230
  [MISSING_DEVICE_TOKEN_STATUS_CODE],
@@ -275,9 +273,8 @@ module RubyPushNotifications
275
273
  end
276
274
 
277
275
  it 'saves the statuses' do
278
- expect do
279
- pusher.push notifications
280
- end.to change { notifications.map { |n| n.results } }.from([nil]*10).to [
276
+ pusher.push notifications
277
+ expect(notifications.map(&:individual_results)).to eq [
281
278
  [PROCESSING_ERROR_STATUS_CODE],
282
279
  [NO_ERROR_STATUS_CODE],
283
280
  [MISSING_DEVICE_TOKEN_STATUS_CODE],
@@ -19,6 +19,19 @@ module RubyPushNotifications
19
19
  # According to https://developer.android.com/google/gcm/server-ref.html#table1
20
20
  it 'validates the data'
21
21
 
22
+ it_behaves_like 'a proper results manager' do
23
+ let(:success_count) { 2 }
24
+ let(:failures_count) { 1 }
25
+ let(:canonical_id) { 'abcd' }
26
+ let(:individual_results) { [GCMCanonicalIDResult.new(canonical_id)] }
27
+ let(:results) do
28
+ GCMResponse.new 200, JSON.dump(
29
+ success: success_count, failure: failures_count, results: [
30
+ registration_id: canonical_id
31
+ ]
32
+ )
33
+ end
34
+ end
22
35
  end
23
36
  end
24
37
  end
@@ -38,6 +38,16 @@ module RubyPushNotifications
38
38
 
39
39
  it 'validates the data'
40
40
 
41
+ it_behaves_like 'a proper results manager' do
42
+ let(:notification) { build :mpns_notification }
43
+ let(:success_count) { 1 }
44
+ let(:failures_count) { 0 }
45
+ let(:device_url) { generate :mpns_device_url }
46
+ let(:individual_results) { [MPNSResultOK.new(device_url, {})] }
47
+ let(:results) do
48
+ MPNSResponse.new [{ code: 200, device_url: device_url, headers: {} }]
49
+ end
50
+ end
41
51
  end
42
52
  end
43
53
  end
@@ -0,0 +1,31 @@
1
+ shared_examples 'right results' do |success, failed, individual_results|
2
+ it 'has the expected success value' do
3
+ expect(notification.success).to eq success
4
+ end
5
+
6
+ it 'has the expected failed value' do
7
+ expect(notification.failed).to eq failed
8
+ end
9
+
10
+ it 'has the expected individual_results' do
11
+ expect(notification.individual_results).to eq individual_results
12
+ end
13
+ end
14
+
15
+ shared_examples 'a proper results manager' do
16
+ describe 'results management' do
17
+ before { notification.results = results }
18
+
19
+ it 'gives the success count' do
20
+ expect(notification.success).to eq success_count
21
+ end
22
+
23
+ it 'gives the failures count' do
24
+ expect(notification.failed).to eq failures_count
25
+ end
26
+
27
+ it 'gives the individual results' do
28
+ expect(notification.individual_results).to eq individual_results
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-push-notifications
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Alonso
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-15 00:00:00.000000000 Z
11
+ date: 2015-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: builder
@@ -119,6 +119,7 @@ files:
119
119
  - lib/ruby-push-notifications/apns/apns_connection.rb
120
120
  - lib/ruby-push-notifications/apns/apns_notification.rb
121
121
  - lib/ruby-push-notifications/apns/apns_pusher.rb
122
+ - lib/ruby-push-notifications/apns/apns_results.rb
122
123
  - lib/ruby-push-notifications/gcm.rb
123
124
  - lib/ruby-push-notifications/gcm/gcm_connection.rb
124
125
  - lib/ruby-push-notifications/gcm/gcm_error.rb
@@ -132,6 +133,7 @@ files:
132
133
  - lib/ruby-push-notifications/mpns/mpns_pusher.rb
133
134
  - lib/ruby-push-notifications/mpns/mpns_response.rb
134
135
  - lib/ruby-push-notifications/mpns/mpns_result.rb
136
+ - lib/ruby-push-notifications/notification_results_manager.rb
135
137
  - lib/ruby-push-notifications/version.rb
136
138
  - ruby-push-notifications.gemspec
137
139
  - spec/factories.rb
@@ -150,6 +152,7 @@ files:
150
152
  - spec/spec_helper.rb
151
153
  - spec/support/dummy.pem
152
154
  - spec/support/factory_girl.rb
155
+ - spec/support/results_shared_examples.rb
153
156
  homepage: https://github.com/calonso/ruby-push-notifications
154
157
  licenses:
155
158
  - MIT
@@ -191,4 +194,5 @@ test_files:
191
194
  - spec/spec_helper.rb
192
195
  - spec/support/dummy.pem
193
196
  - spec/support/factory_girl.rb
197
+ - spec/support/results_shared_examples.rb
194
198
  has_rdoc: