adck 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,7 +31,7 @@ You can set the defaults for all connections created by setting them on the `ADC
31
31
  ### Quick send one notification
32
32
 
33
33
  device_token = '123abc456def'
34
-
34
+
35
35
  ADCK(device_token, 'POPUP!')
36
36
 
37
37
  is equivalent to
@@ -41,7 +41,7 @@ is equivalent to
41
41
  also the same
42
42
 
43
43
  ADCK.send_notification(device_token, body: 'POPUP!')
44
-
44
+
45
45
  All of these quick sendings use the default server values and open and close the connection for each notification.
46
46
 
47
47
  ### Multiple notifications
@@ -57,7 +57,7 @@ The `Message` creates the payload which is sent to the iPhone.
57
57
  To send many messages quickly
58
58
 
59
59
  n2 = ADCK::Notification.new(device_token, 'POPUP2!)
60
-
60
+
61
61
  ADCK.send_notifications([n1,n2])
62
62
 
63
63
  or you can
@@ -89,11 +89,11 @@ Messages are passed all the way though so in the previous example where we ran `
89
89
  Here are some examples of messages:
90
90
 
91
91
  ADCK::Message.new(body: 'This is what the user sees')
92
-
92
+
93
93
  Now lets add a sound and a badge count:
94
-
94
+
95
95
  ADCK::Message.new(body: 'This is what the user sees', sound: 'default', badge: 5)
96
-
96
+
97
97
  What if we don't want to show an action button, just want to pop up a notification with an "OK" button.
98
98
 
99
99
  ADCK::Message.new(body: 'Something you cant do anything about', action_loc_key: nil)
@@ -109,7 +109,7 @@ This will open and close a new connection to the server. If you want to use an e
109
109
 
110
110
  conn = ADCK::Connection.new
111
111
  m.connection = conn
112
-
112
+
113
113
  conn.open
114
114
  m.send_to([token1,token2,…,tokenN])
115
115
  m.send_to([token1,token2,…,tokenN])
@@ -123,11 +123,11 @@ There's one caviat, the message is compiled and frozen upon sending to improve p
123
123
  m.body = 'Cute Kitty2' #=> RuntimeError: can't modify frozen ADCK::Message
124
124
 
125
125
  You can however dupe
126
-
126
+
127
127
  m = m.dup
128
128
  m.body = 'Cute Kitty2'
129
129
  m.send_to([token1,token2,…,tokenN])
130
-
130
+
131
131
  or you can tell the message to not freeze it
132
132
 
133
133
  m = ADCK::Message.new(body: 'Cute Kitty!', action_loc_key: "PET_IT", freeze: false)
@@ -135,15 +135,22 @@ or you can tell the message to not freeze it
135
135
  m.body = 'Cute Kitty2'
136
136
  m.send_to([token1,token2,…,tokenN])
137
137
 
138
+ #### Extra parameters
139
+
140
+ * freeze: if set to false the message wont be frozen when packaged up
141
+ * validate: disable valdation of size and other values
142
+ * truncate: Truncate the value of `body` if it would cause the message to be too
143
+ large
144
+
138
145
  ### Connection
139
146
 
140
147
  A good example of when you'd want to use multiple connections is if you have different `.pem` files (for different apps maybe) that you want to use.
141
148
 
142
149
  conn_iphone = ADCK::Connection.new(pem: Rails.root+'config/pem/iphone.pem')
143
150
  conn_ipad = ADCK::Connection.new(pem: Rails.root+'config/pem/ipad.pem')
144
-
151
+
145
152
  n = ADCK::Notification.new(token, "Sent to both iPhone and iPad apps")
146
153
  n2 = ADCK::Notification.new(token2, "Sent to iPad app")
147
-
154
+
148
155
  conn_iphone.send_notification(n)
149
156
  conn_ipad.send_notifications([n,n2])
@@ -7,23 +7,37 @@ module ADCK
7
7
  :alert, :badge, :sound, :other
8
8
  ]
9
9
  attr_accessor *FIELDS
10
- attr_accessor :connection
10
+ attr_accessor :connection, :truncate
11
11
 
12
12
  def initialize(message)
13
13
  if message.is_a? Hash
14
14
  @validate = message[:validate]
15
15
  @freeze = message[:freeze]
16
+ @truncate = message.delete(:truncate)
16
17
  end
17
18
 
18
- set_values_from_arg(message)
19
-
20
19
  self.other ||= {}
20
+
21
+ set_values_from_arg(message)
21
22
  end
22
23
 
23
- def alert
24
+ def alert test=false
24
25
  a = {}
25
26
 
26
- a[:body] = body if body
27
+ if test
28
+ a[:body] = ''
29
+ elsif body
30
+ if truncate && body.bytesize > @max_body_length
31
+ truncate_with = truncate.is_a?(String) ? truncate : '...'
32
+
33
+ set_body = body[0...(@max_body_length-truncate_with.length)]
34
+ set_body << truncate_with
35
+ else
36
+ set_body = body
37
+ end
38
+
39
+ a[:body] = set_body
40
+ end
27
41
 
28
42
  if action_loc_key # is not false or nil
29
43
  a[:'action-loc-key'] = action_loc_key
@@ -48,15 +62,20 @@ module ADCK
48
62
  @action_loc_key = val.nil? ? false : val
49
63
  end
50
64
 
51
- def aps
65
+ def aps test=false
52
66
  a = {}
53
- _alert = alert
67
+ _alert = alert(test)
54
68
  a[:alert] = _alert unless _alert.empty?
55
69
  a[:badge] = badge if badge
56
70
  a[:sound] = sound if sound
57
71
  a
58
72
  end
59
73
 
74
+ def test_payload
75
+ allowed = MultiJson.dump(other.merge(aps: aps(true))).bytesize
76
+ @max_body_length = 255-allowed
77
+ end
78
+
60
79
  def payload(options={})
61
80
  other.merge(aps: aps)
62
81
  end
@@ -144,6 +163,8 @@ module ADCK
144
163
  else
145
164
  raise "Message needs to have either a hash or string"
146
165
  end
166
+
167
+ test_payload
147
168
  end
148
169
 
149
170
  class PayloadTooLarge < RuntimeError; end
@@ -1,3 +1,3 @@
1
1
  module ADCK
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe ADCK::Message do
4
+
5
+ context "message is too long" do
6
+ let(:message) {'hello ' *100}
7
+
8
+ it "should truncate with dots default" do
9
+ msg = ADCK::Message.new(body: message, truncate: true)
10
+ json = msg.package
11
+
12
+ MultiJson.load(json)['aps']['alert']['body'][-3,3].should == '...'
13
+ end
14
+
15
+ it "should truncate with defined value" do
16
+ msg = ADCK::Message.new(body: message, truncate: '[end]')
17
+ json = msg.package
18
+
19
+ MultiJson.load(json)['aps']['alert']['body'][-5,5].should == '[end]'
20
+ end
21
+
22
+ it "should raise error" do
23
+ msg = ADCK::Message.new(body: message)
24
+ expect {msg.package}.to raise_error(ADCK::Message::PayloadTooLarge)
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,5 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe ADCK::Notification do
4
+
5
+ end
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
2
  gem 'rspec', '>= 1.2.8'
3
3
  require 'rspec'
4
- require File.join(File.dirname(__FILE__), '..', 'lib', 'apns')
4
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'adck')
5
5
  require 'base64'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: adck
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-13 00:00:00.000000000 Z
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -63,7 +63,8 @@ files:
63
63
  - lib/adck/message.rb
64
64
  - lib/adck/notification.rb
65
65
  - lib/adck/version.rb
66
- - spec/apns/notification_spec.rb
66
+ - spec/adck/message_spec.rb
67
+ - spec/adck/notification_spec.rb
67
68
  - spec/spec_helper.rb
68
69
  homepage: https://github.com/delight-labs/ADCK
69
70
  licenses: []
@@ -90,5 +91,6 @@ signing_key:
90
91
  specification_version: 3
91
92
  summary: Simple Apple push notification service gem
92
93
  test_files:
93
- - spec/apns/notification_spec.rb
94
+ - spec/adck/message_spec.rb
95
+ - spec/adck/notification_spec.rb
94
96
  - spec/spec_helper.rb
@@ -1,44 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
-
3
- describe ADCK::Notification do
4
-
5
- it "should take a string as the message" do
6
- n = ADCK::Notification.new('device_token', 'Hello')
7
- n.alert.should == 'Hello'
8
- end
9
-
10
- it "should take a hash as the message" do
11
- n = ADCK::Notification.new('device_token', {:alert => 'Hello iPhone', :badge => 3})
12
- n.alert.should == "Hello iPhone"
13
- n.badge.should == 3
14
- end
15
-
16
- describe '#packaged_message' do
17
-
18
- it "should return JSON with notification information" do
19
- n = ADCK::Notification.new('device_token', {:alert => 'Hello iPhone', :badge => 3, :sound => 'awesome.caf'})
20
- n.packaged_message.should == "{\"aps\":{\"alert\":\"Hello iPhone\",\"badge\":3,\"sound\":\"awesome.caf\"}}"
21
- end
22
-
23
- it "should not include keys that are empty in the JSON" do
24
- n = ADCK::Notification.new('device_token', {:badge => 3})
25
- n.packaged_message.should == "{\"aps\":{\"badge\":3}}"
26
- end
27
-
28
- end
29
-
30
- describe '#package_token' do
31
- it "should package the token" do
32
- n = ADCK::Notification.new('<5b51030d d5bad758 fbad5004 bad35c31 e4e0f550 f77f20d4 f737bf8d 3d5524c6>', 'a')
33
- Base64.encode64(n.packaged_token).should == "W1EDDdW611j7rVAEutNcMeTg9VD3fyDU9ze/jT1VJMY=\n"
34
- end
35
- end
36
-
37
- describe '#packaged_notification' do
38
- it "should package the token" do
39
- n = ADCK::Notification.new('device_token', {:alert => 'Hello iPhone', :badge => 3, :sound => 'awesome.caf'})
40
- Base64.encode64(n.packaged_notification).should == "AAAg3vLO/YTnAEB7ImFwcyI6eyJhbGVydCI6IkhlbGxvIGlQaG9uZSIsImJh\nZGdlIjozLCJzb3VuZCI6ImF3ZXNvbWUuY2FmIn19\n"
41
- end
42
- end
43
-
44
- end