Class: AppleShove::Notification

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Notification

Returns a new instance of Notification.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/apple_shove/notification.rb', line 6

def initialize(attributes = {})
  [:p12, :device_token, :payload].each do |req_attr|
    raise "#{req_attr} must be specified" unless attributes.keys.collect { |k| k.to_s }.include? req_attr.to_s
  end

  attributes.each { |k, v| self.send("#{k}=", v) }

  @sandbox          = false if @sandbox.nil?
  @expiration_date  ||= Time.now + 60*60*24*365
  @priority         ||= 10
end

Instance Attribute Details

#device_tokenObject

Returns the value of attribute device_token.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def device_token
  @device_token
end

#expiration_dateObject

Returns the value of attribute expiration_date.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def expiration_date
  @expiration_date
end

#p12Object

Returns the value of attribute p12.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def p12
  @p12
end

#payloadObject

Returns the value of attribute payload.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def payload
  @payload
end

#priorityObject

Returns the value of attribute priority.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def priority
  @priority
end

#sandboxObject

Returns the value of attribute sandbox.



4
5
6
# File 'lib/apple_shove/notification.rb', line 4

def sandbox
  @sandbox
end

Class Method Details

.parse(json) ⇒ Object



18
19
20
# File 'lib/apple_shove/notification.rb', line 18

def self.parse(json)
  self.new(JSON.parse(json))
end

Instance Method Details

#binary_messageObject

Apple APNS format



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/apple_shove/notification.rb', line 29

def binary_message
  payload_json  = @payload.to_json
  
  frame = [ [ 1, 32,                  @device_token         ].pack('CnH64'),
            [ 2, payload_json.length, payload_json          ].pack('Cna*'),
            [ 3, 4,                   ''                    ].pack('CnA4'),
            [ 4, 4,                   @expiration_date.to_i ].pack('CnN'),
            [ 5, 1,                   @priority             ].pack('CnC')     ].join

  [ 2, frame.length, frame ].pack('CNa*')
end

#to_json(*a) ⇒ Object



22
23
24
25
26
# File 'lib/apple_shove/notification.rb', line 22

def to_json(*a)
  hash = {}
  clean_instance_variables.each { |k| hash[k] = self.send(k) }
  hash.to_json(*a)
end