Sha256: a6db48544f3da75ed949f8b26ff6ec065804cbdc0ded2b99517dc7089c8aafbe

Contents?: true

Size: 1013 Bytes

Versions: 2

Compression:

Stored size: 1013 Bytes

Contents

module Rapnd
  class Notification
    attr_accessor :badge, :alert, :sound, :custom, :device_token
    
    def initialize(hash)
      [:badge, :alert, :sound, :custom, :device_token].each do |k|
        self.instance_variable_set("@#{k}".to_sym, hash[k]) if hash[k]
      end
      raise 'Must provide device token' if self.device_token.nil?
      self.device_token = self.device_token.delete(' ')
    end
    
    def payload
      p = Hash.new
      [:badge, :alert, :sound, :custom].each do |k|
        p[k] = send(k) if send(k)
      end
      custom = p.delete(:custom)
      aps = {:aps => p}
      aps.merge!(:custom => custom) if custom
      aps
    end
    
    def json_payload
      j = ActiveSupport::JSON.encode(payload)
      raise PayloadInvalid.new("The payload 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

2 entries across 2 versions & 1 rubygems

Version Path
rapnd-0.1.1 lib/rapnd/notification.rb
rapnd-0.1.0 lib/rapnd/notification.rb