mercurius 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 68382188ef199ed571c96140d38fbec9c6e83981
4
- data.tar.gz: 9642812648df51e4312892f026ef67475a910e06
3
+ metadata.gz: dcf94ca22b14696f4e623967e3263648e4a778d7
4
+ data.tar.gz: 925d653cd0b8061045851c12647d91817f9e87e4
5
5
  SHA512:
6
- metadata.gz: 091b2149bae4a550f06bfff6192fc6e349c03d15e3029b659f76579c0ad8116fe62808221ee21a25c48ce7441272e2aee79da7d5e2b84533ac740417a50622bd
7
- data.tar.gz: e9c41c7b95fc140548ab2cf28176f2db9fb6b07f9f343f5067523cf2785c966dcfa433331a655416be722e4b3fa1c788c925a58a35b0c113a0635248e372a70e
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(notification)
20
- responses << deliver_to_tokens(notification, tokens)
21
- responses << deliver_to_topic(notification, topic) if topic
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 each_batch_of_tokens(tokens, batch_size: BATCH_SIZE, &block)
27
- Array(tokens).flatten.compact.each_slice(batch_size)
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 deliver_to_tokens(notification, tokens)
31
- each_batch_of_tokens(tokens).map do |tokens|
32
- payload = notification.to_h.merge registration_ids: tokens
33
- GCM::Response.new(connection.write(payload), tokens)
34
- end
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 deliver_to_topic(notification, topic)
38
- GCM::Response.new connection.write(notification.to_h.merge(to: topic))
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
@@ -7,7 +7,7 @@ module GCM
7
7
  end
8
8
 
9
9
  def write(json)
10
- tokens = json[:registration_ids]
10
+ tokens = json[:registration_ids] || Array(json[:to])
11
11
 
12
12
  json = {
13
13
  'multicast_id' => '123',
@@ -7,7 +7,7 @@ module GCM
7
7
  end
8
8
 
9
9
  def write(json)
10
- tokens = json[:registration_ids]
10
+ tokens = json[:registration_ids] || Array(json[:to])
11
11
 
12
12
  json = {
13
13
  'multicast_id' => '123',
@@ -1,3 +1,3 @@
1
1
  module Mercurius
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -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.set_mode(:overdrive) }.to raise_error
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(:message) { GCM::Notification.new(data: { alert: 'Hey' }) }
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[android.googleapis.com/gcm/send] }
12
+ before { stub_request :post, %r{android.googleapis.com/gcm/send} }
12
13
 
13
14
  it 'sends a single message' do
14
- service.deliver message, 'token123'
15
- expect(WebMock).to have_requested(:post, %r[android.googleapis.com/gcm/send]).
16
- with(body: { data: { alert: 'Hey' }, registration_ids: ['token123'] })
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 message, 'token123', 'token456'
21
- expect(WebMock).to have_requested(:post, %r[android.googleapis.com/gcm/send]).
22
- with(body: { data: { alert: 'Hey' }, registration_ids: ['token123', 'token456'] })
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, ['token123', 'token456']
27
- expect(WebMock).to have_requested(:post, %r[android.googleapis.com/gcm/send]).
28
- with(body: { data: { alert: 'Hey' }, registration_ids: ['token123', 'token456'] })
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[android.googleapis.com/gcm/send]).
35
- with(body: { data: { alert: 'Hey' }, registration_ids: tokens.take(999) })
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
- describe '#deliver_topic' do
42
- before { stub_request :post, %r[android.googleapis.com/gcm/send] }
43
- let(:message) { { notification: { title: 'hello', body: 'world' } } }
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[android.googleapis.com/gcm/send]).
48
- with(body: message.merge(to: '/topics/global'))
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[android.googleapis.com/gcm/send] }
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[android.googleapis.com/gcm/send]).to_return(status: 401) }
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[android.googleapis.com/gcm/send]).to_return(status: 400) }
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[android.googleapis.com/gcm/send]).
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.0
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-01-12 00:00:00.000000000 Z
11
+ date: 2016-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json