Class: Houston::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/houston/notification.rb

Defined Under Namespace

Classes: APNSError

Constant Summary collapse

MAXIMUM_PAYLOAD_SIZE =
2048

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Notification

Returns a new instance of Notification.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/houston/notification.rb', line 31

def initialize(options = {})
  @token = options.delete(:token) || options.delete(:device)
  @alert = options.delete(:alert)
  @badge = options.delete(:badge)
  @sound = options.delete(:sound)
  @category = options.delete(:category)
  @expiry = options.delete(:expiry)
  @id = options.delete(:id)
  @priority = options.delete(:priority)
  @content_available = options.delete(:content_available)

  @custom_data = options
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



24
25
26
# File 'lib/houston/notification.rb', line 24

def alert
  @alert
end

#apns_error_code=(value) ⇒ Object (writeonly)

Sets the attribute apns_error_code

Parameters:

  • value

    the value to set the attribute apns_error_code to.



26
27
28
# File 'lib/houston/notification.rb', line 26

def apns_error_code=(value)
  @apns_error_code = value
end

#badgeObject

Returns the value of attribute badge.



24
25
26
# File 'lib/houston/notification.rb', line 24

def badge
  @badge
end

#categoryObject

Returns the value of attribute category.



24
25
26
# File 'lib/houston/notification.rb', line 24

def category
  @category
end

#content_availableObject

Returns the value of attribute content_available.



24
25
26
# File 'lib/houston/notification.rb', line 24

def content_available
  @content_available
end

#custom_dataObject

Returns the value of attribute custom_data.



24
25
26
# File 'lib/houston/notification.rb', line 24

def custom_data
  @custom_data
end

#expiryObject

Returns the value of attribute expiry.



24
25
26
# File 'lib/houston/notification.rb', line 24

def expiry
  @expiry
end

#idObject

Returns the value of attribute id.



24
25
26
# File 'lib/houston/notification.rb', line 24

def id
  @id
end

#priorityObject

Returns the value of attribute priority.



24
25
26
# File 'lib/houston/notification.rb', line 24

def priority
  @priority
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



25
26
27
# File 'lib/houston/notification.rb', line 25

def sent_at
  @sent_at
end

#soundObject

Returns the value of attribute sound.



24
25
26
# File 'lib/houston/notification.rb', line 24

def sound
  @sound
end

#tokenObject Also known as: device

Returns the value of attribute token.



24
25
26
# File 'lib/houston/notification.rb', line 24

def token
  @token
end

Instance Method Details

#errorObject



83
84
85
# File 'lib/houston/notification.rb', line 83

def error
  APNSError.new(APNSError::CODES[@apns_error_code]) if @apns_error_code.nonzero?
end

#mark_as_sent!Object



67
68
69
# File 'lib/houston/notification.rb', line 67

def mark_as_sent!
  @sent_at = Time.now
end

#mark_as_unsent!Object



71
72
73
# File 'lib/houston/notification.rb', line 71

def mark_as_unsent!
  @sent_at = nil
end

#messageObject



58
59
60
61
62
63
64
65
# File 'lib/houston/notification.rb', line 58

def message
  data = [device_token_item,
          payload_item,
          identifier_item,
          expiration_item,
          priority_item].compact.join
  [2, data.bytes.count, data].pack('cNa*')
end

#payloadObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/houston/notification.rb', line 45

def payload
  json = {}.merge(@custom_data || {}).inject({}){|h,(k,v)| h[k.to_s] = v; h}

  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']['category'] = @category if @category
  json['aps']['content-available'] = 1 if @content_available

  json
end

#sent?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/houston/notification.rb', line 75

def sent?
  !!@sent_at
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/houston/notification.rb', line 79

def valid?
  payload.to_json.bytesize <= MAXIMUM_PAYLOAD_SIZE
end