Class: Houston::Notification
- Inherits:
-
Object
- Object
- Houston::Notification
- Defined in:
- lib/houston/notification.rb
Constant Summary collapse
- MAXIMUM_PAYLOAD_SIZE =
256
Instance Attribute Summary collapse
-
#alert ⇒ Object
Returns the value of attribute alert.
-
#badge ⇒ Object
Returns the value of attribute badge.
-
#content_available ⇒ Object
Returns the value of attribute content_available.
-
#custom_data ⇒ Object
Returns the value of attribute custom_data.
-
#expiry ⇒ Object
Returns the value of attribute expiry.
-
#id ⇒ Object
Returns the value of attribute id.
-
#priority ⇒ Object
Returns the value of attribute priority.
-
#sent_at ⇒ Object
readonly
Returns the value of attribute sent_at.
-
#sound ⇒ Object
Returns the value of attribute sound.
-
#token ⇒ Object
(also: #device)
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Notification
constructor
A new instance of Notification.
- #mark_as_sent! ⇒ Object
- #mark_as_unsent! ⇒ Object
- #message ⇒ Object
- #payload ⇒ Object
- #sent? ⇒ Boolean
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Notification
Returns a new instance of Notification.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/houston/notification.rb', line 13 def initialize( = {}) @token = .delete(:token) || .delete(:device) @alert = .delete(:alert) @badge = .delete(:badge) @sound = .delete(:sound) @expiry = .delete(:expiry) @id = .delete(:id) @priority = .delete(:priority) @content_available = .delete(:content_available) @custom_data = end |
Instance Attribute Details
#alert ⇒ Object
Returns the value of attribute alert.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def alert @alert end |
#badge ⇒ Object
Returns the value of attribute badge.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def badge @badge end |
#content_available ⇒ Object
Returns the value of attribute content_available.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def content_available @content_available end |
#custom_data ⇒ Object
Returns the value of attribute custom_data.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def custom_data @custom_data end |
#expiry ⇒ Object
Returns the value of attribute expiry.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def expiry @expiry end |
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def id @id end |
#priority ⇒ Object
Returns the value of attribute priority.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def priority @priority end |
#sent_at ⇒ Object (readonly)
Returns the value of attribute sent_at.
8 9 10 |
# File 'lib/houston/notification.rb', line 8 def sent_at @sent_at end |
#sound ⇒ Object
Returns the value of attribute sound.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def sound @sound end |
#token ⇒ Object Also known as: device
Returns the value of attribute token.
7 8 9 |
# File 'lib/houston/notification.rb', line 7 def token @token end |
Instance Method Details
#mark_as_sent! ⇒ Object
46 47 48 |
# File 'lib/houston/notification.rb', line 46 def mark_as_sent! @sent_at = Time.now end |
#mark_as_unsent! ⇒ Object
50 51 52 |
# File 'lib/houston/notification.rb', line 50 def mark_as_unsent! @sent_at = nil end |
#message ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/houston/notification.rb', line 37 def data = [device_token_item, payload_item, identifier_item, expiration_item, priority_item].compact.join [2, data.bytes.count, data].pack('cNa*') end |
#payload ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/houston/notification.rb', line 26 def payload json = {}.merge(@custom_data || {}) json['aps'] ||= {} json['aps']['alert'] = @alert if @alert json['aps']['badge'] = @badge.to_i rescue 0 if @badge json['aps']['sound'] = @sound if @sound json['aps']['content-available'] = 1 if @content_available json end |
#sent? ⇒ Boolean
54 55 56 |
# File 'lib/houston/notification.rb', line 54 def sent? !!@sent_at end |
#valid? ⇒ Boolean
58 59 60 |
# File 'lib/houston/notification.rb', line 58 def valid? payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE end |