crusade-apns 0.8.0 → 0.8.2

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: a3f95ddb07469fa790dd4692470ec4119cfc944d
4
- data.tar.gz: 02daf2f36ebcf502067db47495465b3faf8d975a
3
+ metadata.gz: f3f43365dbbb7d83fda84a7b56659c3fb3842c4b
4
+ data.tar.gz: 9fc653b9f4a6eddb2beee30e90c57ca3f15b1fb3
5
5
  SHA512:
6
- metadata.gz: 00225b43140c6039f924d8dba31adfddace4a1e34e0f8b5ba11755d33322d7d590c319be17afd5f023bb074853bb65063920ff70a5ebe1a5b0161e78c5e4ec05
7
- data.tar.gz: ef540d4d464d8c3e52020228cc8b8c5470db85388a6f6c29780d1b685578683ba370d3885405b1cd9a21aa063845647a4b1d297703503376ba522fe157008ebb
6
+ metadata.gz: d0d8f657de2b178403731bd77b27a57daef52b66c66b90d209fed973141d5ed0956855984200aab810a488ce1db497e0f88684f6ca8e33a47f5ae96683d4b03c
7
+ data.tar.gz: c6977b46d2636894e3cf1291fa3d74a3194f9a672455c5b4f0beed6f3f7b30abbea68d373fd9a7fcf225cca95405863b7732ce90f0d46a38e10affa8c0f9c708
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in crusade-apns.gemspec
4
- gemspec
4
+ gemspec
@@ -1,14 +1,18 @@
1
+ require 'delegate'
2
+
1
3
  module Crusade
2
4
  module APNS
3
- class Notification
4
- attr_accessor :title, :body, :action, :url_args, :device_token
5
-
6
- def initialize(attrs = {} )
7
- self.title = attrs.fetch(:title) { nil }
8
- self.body = attrs.fetch(:body) { nil }
9
- self.action = attrs.fetch(:action) { nil }
10
- self.url_args = attrs.fetch(:url_args) { [] }
11
- self.device_token = attrs.fetch(:device_token) { nil }
5
+ class Notification < SimpleDelegator
6
+ attr_reader :device_token
7
+
8
+ def initialize(notification, device_token = nil)
9
+ if notification.is_a? Hash
10
+ device_token = notification[:device_token]
11
+ notification = Crusade::Notification.new notification
12
+ end
13
+ super notification
14
+ self.notification = notification
15
+ self.device_token = device_token
12
16
  end
13
17
 
14
18
  def to_json
@@ -21,6 +25,9 @@ module Crusade
21
25
 
22
26
  private
23
27
 
28
+ attr_accessor :notification
29
+ attr_writer :device_token
30
+
24
31
  def payload
25
32
  {
26
33
  "aps" => aps
@@ -15,6 +15,8 @@ module Crusade
15
15
  end
16
16
 
17
17
  def send notification
18
+ return :invalid unless Array(notification.url_args).count == url_args_in_configuration
19
+
18
20
  info "sending notification #{notification.inspect}"
19
21
  connection.send encoder.encode notification
20
22
  end
@@ -27,6 +29,10 @@ module Crusade
27
29
  def connection
28
30
  @connection ||= Crusade::APNS::SocketConnection.new(configuration)
29
31
  end
32
+
33
+ def url_args_in_configuration
34
+ configuration.url_format.scan("%@").count
35
+ end
30
36
  end
31
37
  end
32
38
  end
@@ -1,5 +1,5 @@
1
1
  module Crusade
2
2
  module APNS
3
- VERSION = "0.8.0"
3
+ VERSION = "0.8.2"
4
4
  end
5
5
  end
@@ -29,15 +29,11 @@ eof
29
29
  end
30
30
 
31
31
  it 'sends the binary representation of the notification' do
32
- subject.send Fixtures.default_notification
32
+ subject.send Fixtures.default_apns_notification
33
33
 
34
34
  Timeout::timeout(1, ThreadError) { while(server.received.size == 0) ; end }
35
35
 
36
36
  server.received.last.must_equal Base64.decode64 binary
37
37
  end
38
38
  end
39
-
40
- describe 'configuration' do
41
- it { subject.configuration.must_be_same_as options }
42
- end
43
39
  end
@@ -1,13 +1,15 @@
1
1
  require 'tmpdir'
2
2
 
3
3
  require 'crusade/apns/push_notification'
4
+ require 'crusade/notification'
5
+ require 'crusade/apns/notification'
4
6
 
5
7
  class Fixtures
6
8
  def self.default_configuration
7
9
  OpenStruct.new({
8
10
  site_name: 'Bay Airlines',
9
11
  push_id: 'web.com.example.domain',
10
- url_format: 'http://domain.example.com/%@/?flight=%@',
12
+ url_format: 'http://domain.example.com/?flight=%@',
11
13
  webservice_url: 'https://example.com/push',
12
14
  allowed_domains: ['http://example.com'],
13
15
  temp_dir: Dir.mktmpdir,
@@ -19,7 +21,7 @@ class Fixtures
19
21
  end
20
22
 
21
23
  def self.default_notification
22
- Crusade::APNS::Notification.new({
24
+ Crusade::Notification.new({
23
25
  title: 'the title',
24
26
  body: 'the body',
25
27
  action: 'submit',
@@ -27,4 +29,8 @@ class Fixtures
27
29
  device_token: '1234a'
28
30
  })
29
31
  end
32
+
33
+ def self.default_apns_notification
34
+ Crusade::APNS::Notification.new default_notification, '1234a'
35
+ end
30
36
  end
@@ -13,7 +13,7 @@ describe Crusade::APNS::NotificationEncoder do
13
13
  subject { Crusade::APNS::NotificationEncoder.new options }
14
14
 
15
15
  describe 'encode' do
16
- let(:notification) { Fixtures.default_notification }
16
+ let(:notification) { Fixtures.default_apns_notification }
17
17
  let(:expected) do
18
18
  <<-eof
19
19
  AQAAAAAAAVGAACASNKAAXHsiYXBzIjp7ImFsZXJ0Ijp7InRpdGxlIjoidGhl
@@ -1,9 +1,12 @@
1
1
  require File.expand_path '../../test_helper.rb', __FILE__
2
2
 
3
+ require 'crusade/notification'
3
4
  require 'crusade/apns/notification'
4
5
 
5
6
  describe Crusade::APNS::Notification do
6
7
  let(:described_class) { Crusade::APNS::Notification }
8
+ let(:notification) { Crusade::Notification.new attrs }
9
+ let(:device_token) { '1234a' }
7
10
 
8
11
  let(:attrs) do
9
12
  {
@@ -11,78 +14,71 @@ describe Crusade::APNS::Notification do
11
14
  body: 'the body',
12
15
  action: 'submit',
13
16
  url_args: ['a'],
14
- device_token: '1234a'
17
+ device_token: device_token
15
18
  }
16
19
  end
17
20
 
18
21
  describe 'initialize' do
19
- subject { described_class.new }
22
+ describe 'with a generic notification' do
23
+ subject { described_class.new notification, device_token }
20
24
 
21
- it 'initializes the title' do
22
- subject.title.must_be_nil
23
- end
24
-
25
- it 'initializes the body' do
26
- subject.body.must_be_nil
27
- end
25
+ it 'delegates the generic properties' do
26
+ subject.title.must_equal 'the title'
27
+ end
28
28
 
29
- it 'initializes the action' do
30
- subject.action.must_be_nil
29
+ it 'has a device token' do
30
+ subject.device_token.must_equal device_token
31
+ end
31
32
  end
32
33
 
33
- it 'initializes the url_args' do
34
- subject.url_args.must_equal []
35
- end
34
+ describe 'with a hash of attributes' do
35
+ subject { described_class.new attrs }
36
36
 
37
- it 'initializes the device_token' do
38
- subject.device_token.must_be_nil
39
- end
37
+ it 'internally builds a generic notification' do
38
+ subject.title.must_equal 'the title'
39
+ end
40
40
 
41
- describe 'with a hash' do
42
- subject { described_class.new attrs }
41
+ it 'has a device token' do
42
+ subject.device_token.must_equal device_token
43
+ end
43
44
 
44
- it { subject.title.must_equal 'the title' }
45
- it { subject.body.must_equal 'the body' }
46
- it { subject.action.must_equal 'submit' }
47
- it { subject.url_args.must_equal ['a'] }
48
- it { subject.device_token.must_equal '1234a' }
49
45
  end
46
+ end
50
47
 
51
- describe 'payload and co' do
52
- subject { described_class.new attrs }
53
-
54
- let(:parsed_payload) { JSON.parse(subject.to_json) }
55
- let(:expected_payload) do
56
- {
57
- "aps" => {
58
- "alert" => {
59
- "title" => 'the title',
60
- "body" => 'the body',
61
- "action" => 'submit'
62
- },
63
- "url-args" => ['a']
64
- }
48
+ describe 'payload and co' do
49
+ subject { described_class.new notification }
50
+
51
+ let(:parsed_payload) { JSON.parse(subject.to_json) }
52
+ let(:expected_payload) do
53
+ {
54
+ "aps" => {
55
+ "alert" => {
56
+ "title" => 'the title',
57
+ "body" => 'the body',
58
+ "action" => 'submit'
59
+ },
60
+ "url-args" => ['a']
65
61
  }
66
- end
67
-
68
- it { parsed_payload.must_equal expected_payload }
69
- it { subject.json_size.must_equal 92 }
62
+ }
63
+ end
70
64
 
71
- describe 'no url_args' do
72
- before do
73
- subject.url_args = []
74
- end
65
+ it { parsed_payload.must_equal expected_payload }
66
+ it { subject.json_size.must_equal 92 }
75
67
 
76
- it { parsed_payload['aps'].has_key?('url-args').must_be_false }
68
+ describe 'no url_args' do
69
+ before do
70
+ notification.url_args = []
77
71
  end
78
72
 
79
- describe 'no action' do
80
- before do
81
- subject.action = nil
82
- end
73
+ it { parsed_payload['aps'].has_key?('url-args').must_be_false }
74
+ end
83
75
 
84
- it { parsed_payload['aps']['alert'].has_key?('action').must_be_false }
76
+ describe 'no action' do
77
+ before do
78
+ notification.action = nil
85
79
  end
80
+
81
+ it { parsed_payload['aps']['alert'].has_key?('action').must_be_false }
86
82
  end
87
83
  end
88
84
  end
@@ -0,0 +1,46 @@
1
+ require File.expand_path '../../test_helper.rb', __FILE__
2
+
3
+ require 'ostruct'
4
+
5
+ class FakeEncoder
6
+ def encode notification
7
+ '123'
8
+ end
9
+ end
10
+
11
+ describe Crusade::APNS::PushNotification do
12
+ let(:options) { Fixtures.default_configuration }
13
+ subject { Crusade::APNS::PushNotification.new options, connection }
14
+ let(:connection) { MiniTest::Mock.new }
15
+ let(:notification) { OpenStruct.new(url_args: %w(1)) }
16
+
17
+ describe 'send' do
18
+ let(:encoder) { FakeEncoder.new }
19
+
20
+ before do
21
+
22
+ end
23
+
24
+ it 'sends the encoded notification' do
25
+ Crusade::APNS::NotificationEncoder.stub(:new, encoder) do
26
+ connection.expect :send, true, ['123']
27
+
28
+ subject.send notification
29
+
30
+ connection.verify.must_be_true
31
+ end
32
+ end
33
+
34
+ it 'returns :invalid if the number of url args is not the same as in the configuration' do
35
+ notification.url_args = %w(1 2 3)
36
+
37
+ result = subject.send notification
38
+
39
+ result.must_equal :invalid
40
+ end
41
+ end
42
+
43
+ describe 'configuration' do
44
+ it { subject.configuration.must_be_same_as options }
45
+ end
46
+ end
@@ -30,7 +30,7 @@ describe Crusade::APNS::WebsiteFileGenerator do
30
30
  "websiteName" => "Bay Airlines",
31
31
  "websitePushID" => "web.com.example.domain",
32
32
  "allowedDomains" => ['http://example.com'],
33
- "urlFormatString" => "http://domain.example.com/%@/?flight=%@",
33
+ "urlFormatString" => "http://domain.example.com/?flight=%@",
34
34
  "authenticationToken" => "token",
35
35
  "webServiceURL" => "https://example.com/push"
36
36
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crusade-apns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Rouchy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-22 00:00:00.000000000 Z
11
+ date: 2013-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: crusade
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.8.0
19
+ version: 0.8.2
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 0.8.0
26
+ version: 0.8.2
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rubyzip
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -141,7 +141,6 @@ files:
141
141
  - lib/crusade/apns/configuration.rb
142
142
  - lib/crusade/apns/crypto/user_token_generator.rb
143
143
  - lib/crusade/apns/notification.rb
144
- - lib/crusade/apns/notification_builder.rb
145
144
  - lib/crusade/apns/notification_encoder.rb
146
145
  - lib/crusade/apns/push_notification.rb
147
146
  - lib/crusade/apns/push_package/directory_structure_generator.rb
@@ -179,9 +178,9 @@ files:
179
178
  - test/support/zip_support.rb
180
179
  - test/test_helper.rb
181
180
  - test/unit/configuration_test.rb
182
- - test/unit/notification_builder_test.rb
183
181
  - test/unit/notification_encoder_test.rb
184
182
  - test/unit/notification_test.rb
183
+ - test/unit/push_notification_test.rb
185
184
  - test/unit/push_package/directory_structure_generator_test.rb
186
185
  - test/unit/push_package/manifest_generator_test.rb
187
186
  - test/unit/push_package/signature_generator_test.rb
@@ -238,9 +237,9 @@ test_files:
238
237
  - test/support/zip_support.rb
239
238
  - test/test_helper.rb
240
239
  - test/unit/configuration_test.rb
241
- - test/unit/notification_builder_test.rb
242
240
  - test/unit/notification_encoder_test.rb
243
241
  - test/unit/notification_test.rb
242
+ - test/unit/push_notification_test.rb
244
243
  - test/unit/push_package/directory_structure_generator_test.rb
245
244
  - test/unit/push_package/manifest_generator_test.rb
246
245
  - test/unit/push_package/signature_generator_test.rb
@@ -1,46 +0,0 @@
1
- require 'crusade/apns/notification'
2
-
3
- module Crusade
4
- module APNS
5
- class NotificationBuilder
6
-
7
- def initialize
8
- self.notification = Crusade::APNS::Notification.new
9
- end
10
-
11
- def build
12
- notification
13
- end
14
-
15
- def with_title(title)
16
- notification.title = title
17
- self
18
- end
19
-
20
- def with_body(body)
21
- notification.body = body
22
- self
23
- end
24
-
25
- def with_action(action)
26
- notification.action = action
27
- self
28
- end
29
-
30
- def with_url_args(url_args)
31
- notification.url_args = url_args
32
- self
33
- end
34
-
35
- def with_device_token(device_token)
36
- notification.device_token = device_token
37
- self
38
- end
39
-
40
- private
41
-
42
- attr_accessor :notification
43
-
44
- end
45
- end
46
- end
@@ -1,76 +0,0 @@
1
- require File.expand_path '../../test_helper.rb', __FILE__
2
-
3
- require 'crusade/apns/notification_builder'
4
-
5
- describe Crusade::APNS::NotificationBuilder do
6
- let(:described_class) { Crusade::APNS::NotificationBuilder }
7
- subject { described_class.new }
8
-
9
- describe 'build' do
10
- it' creates a notification' do
11
- notification = subject.build
12
-
13
- notification.is_a?(Crusade::APNS::Notification).must_be_true
14
- end
15
- end
16
-
17
- describe 'with_title' do
18
- it 'returns the builder' do
19
- subject.with_title('test').must_be_same_as subject
20
- end
21
-
22
- it 'sets the title of the notification' do
23
- notif = subject.with_title('test').build
24
-
25
- notif.title.must_equal 'test'
26
- end
27
- end
28
-
29
- describe 'with_body' do
30
- it 'returns the builder' do
31
- subject.with_body('test').must_be_same_as subject
32
- end
33
-
34
- it 'sets the body of the notification' do
35
- notif = subject.with_body('test').build
36
-
37
- notif.body.must_equal 'test'
38
- end
39
- end
40
-
41
- describe 'with_action' do
42
- it 'returns the builder' do
43
- subject.with_action('test').must_be_same_as subject
44
- end
45
-
46
- it 'sets the action of the notification' do
47
- notif = subject.with_action('test').build
48
-
49
- notif.action.must_equal 'test'
50
- end
51
- end
52
-
53
- describe 'with_url_args' do
54
- it 'returns the builder' do
55
- subject.with_url_args('test').must_be_same_as subject
56
- end
57
-
58
- it 'sets the url_args of the notification' do
59
- notif = subject.with_url_args(['test']).build
60
-
61
- notif.url_args.must_equal ['test']
62
- end
63
- end
64
-
65
- describe 'with_device_token' do
66
- it 'returns the builder' do
67
- subject.with_device_token('test').must_be_same_as subject
68
- end
69
-
70
- it 'sets the url_args of the notification' do
71
- notif = subject.with_device_token('test').build
72
-
73
- notif.device_token.must_equal 'test'
74
- end
75
- end
76
- end