mercurius 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mercurius/gcm/service.rb +25 -12
- data/lib/mercurius/testing/gcm/canonical_id_connection.rb +1 -1
- data/lib/mercurius/testing/gcm/unregistered_device_token_connection.rb +1 -1
- data/lib/mercurius/version.rb +1 -1
- data/spec/lib/apns_spec.rb +1 -1
- data/spec/lib/gcm_service_spec.rb +23 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcf94ca22b14696f4e623967e3263648e4a778d7
|
4
|
+
data.tar.gz: 925d653cd0b8061045851c12647d91817f9e87e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 228be44c3ea0a68319d2e9d6dee19c9f7d3628950ff7b3bcab64cafec887e0c454968e605a51a709339a6830f30383b62a74c4c30d8b84b18a36b5e481744814
|
7
|
+
data.tar.gz: 2b3b0b76a14c62ac88873a1f40bc79881d76118324bd3c26b6908108ea3f8a59c1f776bf70465e201317aa51d7b74df87e5277978b0e4aeccd5b487f7a023faa
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/enumerable'
|
2
|
+
|
1
3
|
module GCM
|
2
4
|
class Service
|
3
5
|
include ActiveModel::Model
|
@@ -16,26 +18,37 @@ module GCM
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def deliver(notification, *tokens, topic: nil)
|
19
|
-
responses = GCM::ResponseCollection.new
|
20
|
-
responses <<
|
21
|
-
responses <<
|
21
|
+
responses = GCM::ResponseCollection.new notification
|
22
|
+
responses << deliver_all(notification, tokens)
|
23
|
+
responses << deliver_all(notification, topic) if topic
|
22
24
|
responses
|
23
25
|
end
|
24
26
|
|
25
27
|
private
|
26
|
-
def
|
27
|
-
|
28
|
+
def deliver_all(notification, recipients)
|
29
|
+
batch(recipients).map do |batch|
|
30
|
+
if batch.many?
|
31
|
+
multicast notification, batch
|
32
|
+
else
|
33
|
+
unicast notification, batch.first
|
34
|
+
end
|
35
|
+
end
|
28
36
|
end
|
29
37
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
38
|
+
def unicast(notification, recipient)
|
39
|
+
deliver! notification.to_h.merge(to: recipient), recipient
|
40
|
+
end
|
41
|
+
|
42
|
+
def multicast(notification, recipients)
|
43
|
+
deliver! notification.to_h.merge(registration_ids: recipients), recipients
|
44
|
+
end
|
45
|
+
|
46
|
+
def deliver!(payload, recipients = [])
|
47
|
+
GCM::Response.new connection.write(payload), recipients
|
35
48
|
end
|
36
49
|
|
37
|
-
def
|
38
|
-
|
50
|
+
def batch(recipients, batch_size: BATCH_SIZE)
|
51
|
+
Array(recipients).flatten.compact.each_slice batch_size
|
39
52
|
end
|
40
53
|
end
|
41
54
|
end
|
data/lib/mercurius/version.rb
CHANGED
data/spec/lib/apns_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe APNS do
|
|
12
12
|
|
13
13
|
it 'will not change the host if an invalid mode is specified' do
|
14
14
|
APNS.host = 'host'
|
15
|
-
expect { APNS.
|
15
|
+
expect { APNS.mode = :overdrive }.to raise_error InvalidApnsModeError
|
16
16
|
expect(APNS.host).to eq 'host'
|
17
17
|
end
|
18
18
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
describe GCM::Service do
|
2
2
|
let(:service) { GCM::Service.new }
|
3
|
-
let(:
|
3
|
+
let(:notification) { GCM::Notification.new message }
|
4
|
+
let(:message) { { title: 'Hello', body: 'World' } }
|
4
5
|
|
5
6
|
it 'should default to the GCM module configs' do
|
6
7
|
expect(service.host).to eq GCM.host
|
@@ -8,50 +9,46 @@ describe GCM::Service do
|
|
8
9
|
end
|
9
10
|
|
10
11
|
describe '#deliver' do
|
11
|
-
before { stub_request :post, %r
|
12
|
+
before { stub_request :post, %r{android.googleapis.com/gcm/send} }
|
12
13
|
|
13
14
|
it 'sends a single message' do
|
14
|
-
service.deliver
|
15
|
-
expect(WebMock).to have_requested(:post, %r
|
16
|
-
with
|
15
|
+
service.deliver notification, 'token123'
|
16
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
17
|
+
.with body: message.merge(to: 'token123')
|
17
18
|
end
|
18
19
|
|
19
20
|
it 'sends to multiple tokens via splat' do
|
20
|
-
service.deliver
|
21
|
-
expect(WebMock).to have_requested(:post, %r
|
22
|
-
with
|
21
|
+
service.deliver notification, 'token123', 'token456'
|
22
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
23
|
+
.with body: message.merge(registration_ids: %w(token123 token456))
|
23
24
|
end
|
24
25
|
|
25
26
|
it 'sends to multiple tokens via array' do
|
26
|
-
service.deliver message,
|
27
|
-
expect(WebMock).to have_requested(:post, %r
|
28
|
-
with
|
27
|
+
service.deliver message, %w(token123 token456)
|
28
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
29
|
+
.with body: message.merge(registration_ids: %w(token123 token456))
|
29
30
|
end
|
30
31
|
|
31
32
|
it 'only sends 999 tokens at a time' do
|
32
33
|
tokens = (1..1000).to_a.map { |i| "token#{i}" }
|
33
34
|
service.deliver message, tokens
|
34
|
-
expect(WebMock).to have_requested(:post, %r
|
35
|
-
with
|
36
|
-
expect(WebMock).to have_requested(:post, %r[android.googleapis.com/gcm/send]).
|
37
|
-
with(body: { data: { alert: 'Hey' }, registration_ids: ['token1000'] })
|
38
|
-
end
|
39
|
-
end
|
35
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
36
|
+
.with body: message.merge(registration_ids: tokens.take(999))
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
39
|
+
.with body: message.merge(to: 'token1000')
|
40
|
+
end
|
44
41
|
|
45
42
|
it 'sends a notification to a topic' do
|
46
43
|
service.deliver message, topic: '/topics/global'
|
47
|
-
expect(WebMock).to have_requested(:post, %r
|
48
|
-
with
|
44
|
+
expect(WebMock).to have_requested(:post, %r{android.googleapis.com/gcm/send})
|
45
|
+
.with body: message.merge(to: '/topics/global')
|
49
46
|
end
|
50
47
|
end
|
51
48
|
|
52
49
|
describe 'response' do
|
53
50
|
context 'success' do
|
54
|
-
before { stub_request :post, %r
|
51
|
+
before { stub_request :post, %r{android.googleapis.com/gcm/send} }
|
55
52
|
|
56
53
|
it 'processes a 200 response' do
|
57
54
|
responses = service.deliver message, 'token123'
|
@@ -63,7 +60,7 @@ describe GCM::Service do
|
|
63
60
|
|
64
61
|
describe 'failure' do
|
65
62
|
context 401 do
|
66
|
-
before { stub_request(:post, %r
|
63
|
+
before { stub_request(:post, %r{android.googleapis.com/gcm/send}).to_return(status: 401) }
|
67
64
|
|
68
65
|
it 'has a meaningful message from #error' do
|
69
66
|
responses = service.deliver message, 'token123'
|
@@ -72,7 +69,7 @@ describe GCM::Service do
|
|
72
69
|
end
|
73
70
|
|
74
71
|
context 400 do
|
75
|
-
before { stub_request(:post, %r
|
72
|
+
before { stub_request(:post, %r{android.googleapis.com/gcm/send}).to_return(status: 400) }
|
76
73
|
|
77
74
|
it 'has a meaningful message from #error' do
|
78
75
|
responses = service.deliver message, 'token123'
|
@@ -82,7 +79,7 @@ describe GCM::Service do
|
|
82
79
|
|
83
80
|
context 500..599 do
|
84
81
|
before do
|
85
|
-
stub_request(:post, %r
|
82
|
+
stub_request(:post, %r{android.googleapis.com/gcm/send}).
|
86
83
|
to_return status: 500, headers: { 'Retry-After' => '120' }
|
87
84
|
end
|
88
85
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mercurius
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Beck
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|