mercurius 0.1.6 → 0.1.7

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: 2ab94d197c802c11b53e6fc8dc34090237b456af
4
- data.tar.gz: 4dbcf381ccc7185f1cb27514ac24ff3d80bf8332
3
+ metadata.gz: ad8436c06b411a85bb1392ae8829e647150a8813
4
+ data.tar.gz: 7b6021819a0c16f5b65c609e391c86eef4b10215
5
5
  SHA512:
6
- metadata.gz: 098bd1d77a6e27e53e52cc873aeee6b8428569d47fbf855af51cd2624ab0470c7cfede9e0dc8b1e092100e779d78c6d0843751bacbba837e332102b2351e77f7
7
- data.tar.gz: fee9b5c35b361e65a8d4496e9127f5627b2078ab249349477b9827fe24855125c0664bf66fc5946735d2a7fc7539eb87d9856cfd3d8f8904724c5165b08f21a3
6
+ metadata.gz: f82468ba84b84c9fce6f0a8dc0b977724eda79378b1c70f1c6511be8f4d16e4baff266979c3a94dd73b08c3b4c04ce979960dae2b4afe26dd790041a7d42197e
7
+ data.tar.gz: 4c563b18ed858c1433ae0ff53199597028464d73f40f2b9361c0a9dd36add2fb1fc95055318c240c04809725cc5473f1315a1d1f76609695d954fe7ef178dce2
data/README.md CHANGED
@@ -71,6 +71,23 @@ The deliver method can be called in the following ways:
71
71
  token_array = ['token123', 'token456']
72
72
  apns_service.deliver apns_notification, token_array # multiple recipients
73
73
 
74
+ ## Testing
75
+
76
+ `GCM::Service` and `APNS::Service` can accept which type of connection they use to deliver pushes.
77
+ Mercurius provides several mock connections for convenient testing. You can create a custom fake
78
+ connection if you require a special use case.
79
+
80
+ ```ruby
81
+ # Tokens passed will return successfully from GCM
82
+ GCM::Service.new connection: GCM::SuccessfulConnection.new
83
+
84
+ # Tokens passed will return a NotRegistered error from the fake GCM
85
+ GCM::Service.new connection: GCM::UnregisteredDeviceTokenConnection.new('token123')
86
+
87
+ # Tokens passed as keys will return their mapped value as the canonical ID
88
+ GCM::Service.new connection: GCM::CanonicalIdConnection.new('token123' => 'canonical123')
89
+ ```
90
+
74
91
  ## Contributing
75
92
 
76
93
  Please fork, modify, and send a pull request if you want to help improve this gem.
@@ -12,10 +12,7 @@ module APNS
12
12
  attr_accessor :host, :port, :pem
13
13
 
14
14
  def mode=(mode)
15
- raise InvalidApnsModeError.new unless HOSTS.include? mode.to_sym
16
- @host = HOSTS[mode.to_sym]
15
+ @host = HOSTS.fetch(mode.to_sym) { raise InvalidApnsModeError.new }
17
16
  end
18
-
19
17
  end
20
-
21
18
  end
@@ -4,15 +4,15 @@ module APNS
4
4
 
5
5
  MAX_NUMBER_OF_RETRIES = 3
6
6
 
7
- attr_accessor :host, :port, :pem
8
- attr_reader :connection, :attempts
7
+ attr_accessor :host, :port, :pem, :connection
8
+ attr_reader :attempts
9
9
 
10
10
  def initialize(*)
11
11
  super
12
12
  @host ||= APNS.host
13
13
  @port ||= APNS.port
14
- @pem ||= APNS.pem
15
- @connection = APNS::Connection.new(@host, @port, @pem)
14
+ @pem ||= APNS.pem
15
+ @connection ||= APNS::Connection.new(@host, @port, @pem)
16
16
  @attempts = 0
17
17
  end
18
18
 
data/lib/mercurius/gcm.rb CHANGED
@@ -5,5 +5,4 @@ module GCM
5
5
  class << self
6
6
  attr_accessor :host, :key
7
7
  end
8
-
9
8
  end
@@ -4,14 +4,14 @@ module GCM
4
4
 
5
5
  BATCH_SIZE = 999
6
6
 
7
- attr_accessor :host, :key
8
- attr_reader :connection, :attempts
7
+ attr_accessor :host, :key, :connection
8
+ attr_reader :attempts
9
9
 
10
10
  def initialize(*)
11
11
  super
12
12
  @host ||= GCM.host
13
- @key ||= GCM.key
14
- @connection = GCM::Connection.new(@host, @key)
13
+ @key ||= GCM.key
14
+ @connection ||= GCM::Connection.new(@host, @key)
15
15
  @attempts = 0
16
16
  end
17
17
 
@@ -1,7 +1,8 @@
1
- require 'mercurius/testing/base'
2
- require 'mercurius/testing/delivery'
3
- require 'mercurius/testing/result'
4
- require 'mercurius/testing/service'
1
+ require 'mercurius/testing/fake_response'
5
2
 
6
- APNS::Service.send :prepend, Mercurius::Testing::Service
7
- GCM::Service.send :prepend, Mercurius::Testing::Service
3
+ require 'mercurius/testing/gcm/token_serializer'
4
+ require 'mercurius/testing/gcm/successful_connection'
5
+ require 'mercurius/testing/gcm/canonical_id_connection'
6
+ require 'mercurius/testing/gcm/unregistered_device_token_connection'
7
+
8
+ require 'mercurius/testing/apns/successful_connection'
@@ -0,0 +1,28 @@
1
+ module APNS
2
+ class SuccessfulConnection
3
+ def initialize
4
+ @_open = false
5
+ end
6
+
7
+ def open
8
+ @_open = true
9
+ @ssl = FakeSocket.new
10
+ @socket = FakeSocket.new
11
+ end
12
+
13
+ def close
14
+ @_open = false
15
+ end
16
+
17
+ def closed?
18
+ not @_open
19
+ end
20
+
21
+ def write(data)
22
+ @ssl.write data
23
+ end
24
+
25
+ def read
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ module Mercurius
2
+ class FakeResponse
3
+ attr_accessor :body, :status
4
+
5
+ def initialize(options = {})
6
+ @body = options[:body]
7
+ @status = options[:status]
8
+ end
9
+
10
+ def success?
11
+ (200..399).include? @status
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,30 @@
1
+ module GCM
2
+ class CanonicalIdConnection
3
+ include GCM::TokenSerializer
4
+
5
+ def initialize(canonical_ids_map)
6
+ @canonical_ids_map = canonical_ids_map
7
+ end
8
+
9
+ def write(json)
10
+ tokens = json[:registration_ids]
11
+
12
+ json = {
13
+ 'multicast_id' => '123',
14
+ 'success' => tokens.size,
15
+ 'failure' => 0,
16
+ 'canonical_ids' => number_of_tokens_mapped_to_canonical_ids(tokens),
17
+ 'results' => canonical_token_json(tokens, @canonical_ids_map)
18
+ }.to_json
19
+
20
+ Mercurius::FakeResponse.new body: json, status: 200
21
+ end
22
+
23
+ private
24
+ def number_of_tokens_mapped_to_canonical_ids(tokens)
25
+ tokens.count do |token|
26
+ @canonical_ids_map.key? token
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ module GCM
2
+ class SuccessfulConnection
3
+ include GCM::TokenSerializer
4
+
5
+ def write(json)
6
+ tokens = json[:registration_ids]
7
+
8
+ json = {
9
+ 'multicast_id' => '123',
10
+ 'success' => tokens.size,
11
+ 'failure' => 0,
12
+ 'canonical_ids' => 0,
13
+ 'results' => valid_token_json(tokens)
14
+ }.to_json
15
+
16
+ Mercurius::FakeResponse.new body: json, status: 200
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ module GCM
2
+ module TokenSerializer
3
+ def valid_token_json(tokens)
4
+ tokens.map do |token|
5
+ { 'message_id' => SecureRandom.hex }
6
+ end
7
+ end
8
+
9
+ def invalid_token_json(tokens, error)
10
+ tokens.map do |token|
11
+ { 'error' => error }
12
+ end
13
+ end
14
+
15
+ def canonical_token_json(tokens, canonical_ids_map)
16
+ tokens.map do |token|
17
+ hash = { 'message_id' => SecureRandom.hex }
18
+ if canonical_id = canonical_ids_map[token]
19
+ hash['registration_id'] = canonical_id
20
+ end
21
+ hash
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module GCM
2
+ class UnregisteredDeviceTokenConnection
3
+ include GCM::TokenSerializer
4
+
5
+ def initialize(*invalid_tokens)
6
+ @invalid_tokens = invalid_tokens
7
+ end
8
+
9
+ def write(json)
10
+ invalid, valid = json[:registration_ids].partition do |token|
11
+ @invalid_tokens.include? token
12
+ end
13
+
14
+ json = {
15
+ 'multicast_id' => '123',
16
+ 'success' => valid.size,
17
+ 'failure' => invalid.size,
18
+ 'canonical_ids' => 0,
19
+ 'results' => valid_token_json(valid).concat(invalid_token_json(invalid, 'NotRegistered'))
20
+ }.to_json
21
+
22
+ Mercurius::FakeResponse.new body: json, status: 200
23
+ end
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Mercurius
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
@@ -1,45 +1,58 @@
1
- %w(service base result delivery).each do |rb|
2
- require File.expand_path("../../../lib/mercurius/testing/#{rb}", __FILE__)
3
- end
4
-
5
1
  describe 'Test mode' do
2
+ context 'GCM' do
3
+ let(:message) { GCM::Notification.new(data: { alert: 'Hey' }) }
6
4
 
7
- class GCM::MockService < GCM::Service
8
- include Mercurius::Testing::Service
9
- end
10
-
11
- class APNS::MockService < APNS::Service
12
- include Mercurius::Testing::Service
13
- end
5
+ describe 'Normal test connection' do
6
+ let(:service) do
7
+ GCM::Service.new connection: GCM::SuccessfulConnection.new
8
+ end
14
9
 
15
- after do
16
- Mercurius::Testing::Base.reset
17
- end
10
+ it 'Returns all devices sent successfully' do
11
+ responses = service.deliver message, 'token123', 'token456'
12
+ expect(responses.results.succeeded.size).to eq 2
13
+ expect(responses.results.failed.size).to eq 0
14
+ end
15
+ end
18
16
 
19
- context 'GCM' do
20
- let(:service) { GCM::MockService.new }
21
- let(:message) { GCM::Notification.new(data: { alert: 'Hey' }) }
17
+ describe 'Invalid device token connection' do
18
+ let(:service) do
19
+ GCM::Service.new connection: GCM::UnregisteredDeviceTokenConnection.new('token456')
20
+ end
21
+
22
+ it 'Returns invalid device errors for the tokens provided' do
23
+ responses = service.deliver message, 'token123', 'token456'
24
+ expect(responses.results.failed.size).to eq 1
25
+ expect(responses.results.failed[0].token).to eq 'token456'
26
+ expect(responses.results.failed[0].error).to eq 'NotRegistered'
27
+ end
28
+ end
22
29
 
23
- it 'returns the deliveries sent to GCM' do
24
- result = service.deliver message, 'token123'
25
- delivery = Mercurius::Testing::Base.deliveries[0]
26
- expect(delivery.device_tokens).to include 'token123'
27
- expect(delivery.notification.data).to eq Hash[alert: 'Hey']
28
- expect(result).to be_a_kind_of Mercurius::Testing::Result
30
+ describe 'Canonical ID token connection' do
31
+ let(:service) do
32
+ GCM::Service.new connection: GCM::CanonicalIdConnection.new('token456' => 'canonicalToken999')
33
+ end
34
+
35
+ it 'returns canonical IDs for the tokens provided' do
36
+ responses = service.deliver message, 'token123', 'token456'
37
+ expect(responses.results.with_canonical_ids.size).to eq 1
38
+ expect(responses.results.with_canonical_ids[0].token).to eq 'token456'
39
+ expect(responses.results.with_canonical_ids[0].canonical_id).to eq 'canonicalToken999'
40
+ end
41
+
42
+ it 'matches the canonical count with the amount provided to the connection' do
43
+ responses = service.deliver message, 'token123', 'token456'
44
+ expect(responses[0].to_h['canonical_ids']).to eq 1
45
+ end
29
46
  end
30
47
  end
31
48
 
32
49
  context 'APNS' do
33
- let(:service) { APNS::MockService.new }
50
+ let(:service) { APNS::Service.new connection: APNS::SuccessfulConnection.new }
34
51
  let(:message) { APNS::Notification.new(alert: 'Hey') }
35
52
 
36
53
  it 'returns the deliveries sent to APNS' do
37
54
  result = service.deliver message, 'token123'
38
- delivery = Mercurius::Testing::Base.deliveries[0]
39
- expect(delivery.device_tokens).to include 'token123'
40
- expect(delivery.notification.alert).to eq 'Hey'
41
- expect(result).to be_a_kind_of Mercurius::Testing::Result
55
+ # Not sure what to test here...
42
56
  end
43
57
  end
44
-
45
58
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'mercurius'
2
+ require 'mercurius/testing'
2
3
  require 'webmock/rspec'
3
4
  require 'support/fake_socket'
4
5
 
5
- WebMock.disable_net_connect!
6
+ WebMock.disable_net_connect!
@@ -26,5 +26,4 @@ class FakeSocket < StringIO
26
26
  def closed?
27
27
  @_open == false
28
28
  end
29
-
30
- end
29
+ end
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.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Beck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-03 00:00:00.000000000 Z
11
+ date: 2015-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -132,7 +132,6 @@ extra_rdoc_files: []
132
132
  files:
133
133
  - ".gitignore"
134
134
  - ".rspec"
135
- - ".ruby-gemset"
136
135
  - ".ruby-version"
137
136
  - ".travis.yml"
138
137
  - Gemfile
@@ -160,10 +159,12 @@ files:
160
159
  - lib/mercurius/gcm/result_collection.rb
161
160
  - lib/mercurius/gcm/service.rb
162
161
  - lib/mercurius/testing.rb
163
- - lib/mercurius/testing/base.rb
164
- - lib/mercurius/testing/delivery.rb
165
- - lib/mercurius/testing/result.rb
166
- - lib/mercurius/testing/service.rb
162
+ - lib/mercurius/testing/apns/successful_connection.rb
163
+ - lib/mercurius/testing/fake_response.rb
164
+ - lib/mercurius/testing/gcm/canonical_id_connection.rb
165
+ - lib/mercurius/testing/gcm/successful_connection.rb
166
+ - lib/mercurius/testing/gcm/token_serializer.rb
167
+ - lib/mercurius/testing/gcm/unregistered_device_token_connection.rb
167
168
  - lib/mercurius/version.rb
168
169
  - mercurius.gemspec
169
170
  - spec/lib/apns_notification_spec.rb
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- mercurius
@@ -1,21 +0,0 @@
1
- module Mercurius
2
- module Testing
3
- class Base
4
-
5
- def self.deliveries
6
- @@deliveries ||= []
7
- end
8
-
9
- def self.reset
10
- @@deliveries = []
11
- end
12
-
13
- def self.deliveries_to(device_token)
14
- deliveries.select do |delivery|
15
- delivery.device_tokens.include? device_token
16
- end
17
- end
18
-
19
- end
20
- end
21
- end
@@ -1,6 +0,0 @@
1
- module Mercurius
2
- module Testing
3
- class Delivery < Struct.new(:notification, :device_tokens)
4
- end
5
- end
6
- end
@@ -1,32 +0,0 @@
1
- module Mercurius
2
- module Testing
3
- class Result
4
- attr_reader :responses
5
-
6
- def initialize(notification)
7
- @notification = notification
8
- @responses = []
9
- end
10
-
11
- def success?
12
- true
13
- end
14
-
15
- def process_response(*)
16
- end
17
-
18
- def failed_responses
19
- []
20
- end
21
-
22
- def failed_device_tokens
23
- []
24
- end
25
-
26
- def has_canonical_ids?
27
- false
28
- end
29
-
30
- end
31
- end
32
- end
@@ -1,17 +0,0 @@
1
- module Mercurius
2
- module Testing
3
- module Service
4
-
5
- def deliver(notification, *device_tokens)
6
- Mercurius::Testing::Base.deliveries << Mercurius::Testing::Delivery.new(notification, Array(device_tokens).flatten)
7
- Mercurius::Testing::Result.new notification
8
- end
9
-
10
- def deliver_topic(notification, topic)
11
- Mercurius::Testing::Base.deliveries << Mercurius::Testing::Delivery.new(notification, [topic])
12
- Mercurius::Testing::Result.new notification
13
- end
14
-
15
- end
16
- end
17
- end