Sha256: bf8e7660830b4f3b49ce2a2749c2aa711df38ee9b7344db47e1c6a1ddd267cf8

Contents?: true

Size: 998 Bytes

Versions: 6

Compression:

Stored size: 998 Bytes

Contents

module Rapnd
  class Notification
    attr_accessor :badge, :alert, :sound, :custom_properties, :device_token
    
    def initialize(hash)
      [:badge, :alert, :sound, :device_token, :custom_properties].each do |k|
        self.instance_variable_set("@#{k}".to_sym, hash[k]) if hash[k]
      end
      raise "Must provide device token: #{hash}" if self.device_token.nil?
      self.device_token = self.device_token.delete(' ')
    end
    
    def payload
      p = Hash.new
      [:badge, :alert, :sound].each do |k|
        p[k] = send(k) if send(k)
      end
      aps = {:aps => p}
      aps.merge!(custom_properties) if custom_properties
      aps
    end
    
    def json_payload
      j = ActiveSupport::JSON.encode(payload)
      raise "The payload #{j} is larger than allowed: #{j.length}" if j.size > 256
      j
    end
    
    def to_bytes
      j = json_payload
      [0, 0, 32, self.device_token, 0, j.bytesize, j].pack("cccH*cca*").force_encoding('ASCII-8BIT')
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rapnd-0.3.1 lib/rapnd/notification.rb
rapnd-0.3.0 lib/rapnd/notification.rb
rapnd-0.2.5 lib/rapnd/notification.rb
rapnd-0.2.4 lib/rapnd/notification.rb
rapnd-0.2.3 lib/rapnd/notification.rb
rapnd-0.2.2 lib/rapnd/notification.rb