Class: Houston::Notification

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Notification

Returns a new instance of Notification.



11
12
13
14
15
16
17
18
19
# File 'lib/houston/notification.rb', line 11

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

  @custom_data = options
end

Instance Attribute Details

#alertObject

Returns the value of attribute alert.



5
6
7
# File 'lib/houston/notification.rb', line 5

def alert
  @alert
end

#badgeObject

Returns the value of attribute badge.



5
6
7
# File 'lib/houston/notification.rb', line 5

def badge
  @badge
end

#content_availableObject

Returns the value of attribute content_available.



5
6
7
# File 'lib/houston/notification.rb', line 5

def content_available
  @content_available
end

#custom_dataObject

Returns the value of attribute custom_data.



5
6
7
# File 'lib/houston/notification.rb', line 5

def custom_data
  @custom_data
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



6
7
8
# File 'lib/houston/notification.rb', line 6

def sent_at
  @sent_at
end

#soundObject

Returns the value of attribute sound.



5
6
7
# File 'lib/houston/notification.rb', line 5

def sound
  @sound
end

#tokenObject Also known as: device

Returns the value of attribute token.



5
6
7
# File 'lib/houston/notification.rb', line 5

def token
  @token
end

Instance Method Details

#mark_as_sent!Object



39
40
41
# File 'lib/houston/notification.rb', line 39

def mark_as_sent!
  @sent_at = Time.now
end

#messageObject



32
33
34
35
36
37
# File 'lib/houston/notification.rb', line 32

def message
  json = payload.to_json
  hex_token = [@token.gsub(/[<\s>]/, '')].pack('H*')

  [0, 0, 32, hex_token, 0, json.bytes.count, json].pack('ccca*cca*')
end

#payloadObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/houston/notification.rb', line 21

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/houston/notification.rb', line 43

def sent?
  !!@sent_at
end