Class: ADCK::Message
- Inherits:
-
Object
show all
- Defined in:
- lib/adck/message.rb
Defined Under Namespace
Classes: InvalidAttribute, PayloadTooLarge
Constant Summary
collapse
- ALERT_FIELDS =
[
:body, :action_loc_key, :loc_key, :loc_args, :launch_image
]
- FIELDS =
ALERT_FIELDS+[
:alert, :badge, :sound, :other
]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(message) ⇒ Message
Returns a new instance of Message.
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/adck/message.rb', line 12
def initialize(message)
if message.is_a? Hash
@validate = message[:validate]
@freeze = message[:freeze]
@truncate = message.delete(:truncate)
end
self.other ||= {}
set_values_from_arg(message)
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
10
11
12
|
# File 'lib/adck/message.rb', line 10
def connection
@connection
end
|
#truncate ⇒ Object
Returns the value of attribute truncate.
10
11
12
|
# File 'lib/adck/message.rb', line 10
def truncate
@truncate
end
|
Class Method Details
.build(opts) ⇒ Object
132
133
134
|
# File 'lib/adck/message.rb', line 132
def self.build(opts)
opts.is_a?(self) ? opts : new(opts)
end
|
Instance Method Details
#action_loc_key=(val) ⇒ Object
59
60
61
62
63
|
# File 'lib/adck/message.rb', line 59
def action_loc_key=val
@action_loc_key = val.nil? ? false : val
end
|
#alert(test = false) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/adck/message.rb', line 24
def alert test=false
a = {}
if test
a[:body] = ''
elsif body
if truncate && body.bytesize > @max_body_length
truncate_with = truncate.is_a?(String) ? truncate : '...'
set_body = body[0...(@max_body_length-truncate_with.length)]
set_body << truncate_with
else
set_body = body
end
a[:body] = set_body
end
if action_loc_key a[:'action-loc-key'] = action_loc_key
elsif action_loc_key == false
a[:'action-loc-key'] = nil
end
a[:'loc-key'] = loc_key unless loc_key.nil?
a[:'loc-args'] = loc_args unless loc_args.nil?
a[:'launch-image'] = launch_image unless launch_image.nil?
a
end
|
#alert=(val) ⇒ Object
55
56
57
|
# File 'lib/adck/message.rb', line 55
def alert=val
set_values_from_arg val, ALERT_FIELDS
end
|
#aps(test = false) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/adck/message.rb', line 65
def aps test=false
a = {}
_alert = alert(test)
a[:alert] = _alert unless _alert.empty?
a[:badge] = badge if badge
a[:sound] = sound if sound
a
end
|
#bytesize ⇒ Object
112
113
114
|
# File 'lib/adck/message.rb', line 112
def bytesize
@package ? @package.bytesize : to_json.bytesize
end
|
#dup ⇒ Object
146
147
148
149
150
|
# File 'lib/adck/message.rb', line 146
def dup
m = super()
m.instance_variable_set :@package, nil
m
end
|
#package ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/adck/message.rb', line 120
def package
if @freeze == false
validate!
else
unless @package
@package = validate!
freeze
end
@package
end
end
|
#payload(options = {}) ⇒ Object
Also known as:
as_json
79
80
81
|
# File 'lib/adck/message.rb', line 79
def payload(options={})
other.merge(aps: aps)
end
|
#send_to(device_tokens) ⇒ Object
136
137
138
139
140
141
142
143
144
|
# File 'lib/adck/message.rb', line 136
def send_to(device_tokens)
conn = connection || Connection.new
notifications = Array(device_tokens).collect do |token|
Notification.new(token,self)
end
conn.send_notifications(notifications)
end
|
#test_payload ⇒ Object
74
75
76
77
|
# File 'lib/adck/message.rb', line 74
def test_payload
allowed = MultiJson.dump(other.merge(aps: aps(true))).bytesize
@max_body_length = 255-allowed
end
|
#to_json(options = {}) ⇒ Object
116
117
118
|
# File 'lib/adck/message.rb', line 116
def to_json(options={})
MultiJson.dump(payload(options))
end
|
#validate! ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/adck/message.rb', line 84
def validate!
return to_json if @validate == false
if loc_args && !loc_args.is_a?(Array)
raise InvalidAttribute, 'loc-args should be an array'
end
if body && !body.is_a?(String)
raise InvalidAttribute, 'body needs to be a string'
end
if action_loc_key && (action_loc_key != false || !action_loc_key.is_a?(String))
raise InvalidAttribute, 'action-loc-key needs to be set to false/nil/string'
end
if badge && !badge.is_a?(Integer)
raise InvalidAttribute, 'badge needs to be a number or nil'
end
json = to_json
if json.bytesize > 255
raise PayloadTooLarge, "Payload must be less than 256 bytes, is #{json.bytesize}bytes"
end
json
end
|