Class: ApnClient::Message
- Inherits:
-
Object
- Object
- ApnClient::Message
show all
- Defined in:
- lib/apn_client/message.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Message
Creates an APN message to to be sent over SSL to the APN service.
13
14
15
16
17
18
19
|
# File 'lib/apn_client/message.rb', line 13
def initialize(attributes = {})
self.attributes = NamedArgs.symbolize_keys!(attributes)
NamedArgs.assert_valid!(attributes,
:optional => self.class.optional_attributes,
:required => self.class.required_attributes)
check_payload_size!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
Delegate attribute reading and writing (#attribute_name and #attribute_name=) to the attributes hash.
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/apn_client/message.rb', line 52
def method_missing(method_name, *args)
method_match, attribute_name, equal_sign = method_name.to_s.match(/\A([^=]+)(=)?\Z/).to_a
if attribute_name && self.class.valid_attributes.include?(attribute_name.to_sym)
if equal_sign
attributes[attribute_name.to_sym] = args.first
else
attributes[attribute_name.to_sym]
end
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/apn_client/message.rb', line 3
def attributes
@attributes
end
|
Class Method Details
.error_codes ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/apn_client/message.rb', line 30
def self.error_codes
{
:no_errors_encountered => 0,
:processing_error => 1,
:missing_device_token => 2,
:missing_topic => 3,
:missing_payload => 4,
:invalid_token_size => 5,
:invalid_topic_size => 6,
:invalid_payload_size => 7,
:invalid_token => 8,
:unknown => 255
}
end
|
.expires_at ⇒ Object
45
46
47
48
|
# File 'lib/apn_client/message.rb', line 45
def self.expires_at
seconds_per_day = 24*3600
(Time.now + 30*seconds_per_day).to_i
end
|
.optional_attributes ⇒ Object
65
66
67
|
# File 'lib/apn_client/message.rb', line 65
def self.optional_attributes
[:alert, :badge, :sound, :content_available]
end
|
.required_attributes ⇒ Object
69
70
71
|
# File 'lib/apn_client/message.rb', line 69
def self.required_attributes
[:message_id, :device_token]
end
|
.valid_attributes ⇒ Object
73
74
75
|
# File 'lib/apn_client/message.rb', line 73
def self.valid_attributes
optional_attributes + required_attributes
end
|
Instance Method Details
#==(other_message) ⇒ Object
26
27
28
|
# File 'lib/apn_client/message.rb', line 26
def ==(other_message)
other_message && other_message.is_a?(self.class) && other_message.message_id == self.message_id
end
|
#payload ⇒ Object
The payload is a JSON formated hash with alert, sound, badge, content-available, and any custom properties, example: => {“badge” => 5, “sound” => “my_sound.aiff”, “alert” => “Hello!”}
80
81
82
|
# File 'lib/apn_client/message.rb', line 80
def payload
payload_hash.to_json
end
|
#payload_hash ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/apn_client/message.rb', line 84
def payload_hash
result = {}
result['aps'] = {}
result['aps']['alert'] = alert if alert
result['aps']['badge'] = badge if badge and badge > 0
if sound
result['aps']['sound'] = sound if sound.is_a? String
result['aps']['sound'] = "1.aiff" if sound.is_a? TrueClass
end
result['aps']['content-available'] = 1 if content_available
result
end
|
#payload_size ⇒ Object
97
98
99
|
# File 'lib/apn_client/message.rb', line 97
def payload_size
payload.bytesize
end
|
#to_hash ⇒ Object
101
102
103
|
# File 'lib/apn_client/message.rb', line 101
def to_hash
attributes
end
|
#to_json ⇒ Object
105
106
107
|
# File 'lib/apn_client/message.rb', line 105
def to_json
to_hash.to_json
end
|
#to_s ⇒ Object
We use the enhanced format. See the Apple documentation for details.
22
23
24
|
# File 'lib/apn_client/message.rb', line 22
def to_s
[1, message_id, self.class.expires_at, 0, 32, device_token, 0, payload_size, payload].pack('ciiccH*cca*')
end
|