Sha256: c87e0e155ed87dcb433df951b9728e4325db2bbc27d58863c5db2dbfcfc3f0c5

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

module Rapnd
  class Notification
    attr_accessor :badge, :alert, :sound, :content_available, :custom_properties, :device_token
    
    def initialize(hash)
      [:badge, :alert, :sound, :device_token, :content_available, :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, :content_available].each do |k|
        p[k.to_s.gsub('_','-').to_sym] = 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)
      while j.bytesize > 256 && payload[:alert].size > 0 do
        payload[:alert] = payload[:alert][0..-2]
        j = ActiveSupport::JSON.encode(payload)        
      end
      if j.bytesize > 256
        raise "The payload #{j} is larger than allowed: #{j.length}" if j.size > 256
      end
      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-mikec54088-0.5.5 lib/rapnd/notification.rb
rapnd-mikec54088-0.5.4 lib/rapnd/notification.rb
rapnd-mikec54088-0.5.3 lib/rapnd/notification.rb
rapnd-mikec54088-0.5.2 lib/rapnd/notification.rb
rapnd-mikec54088-0.5.1 lib/rapnd/notification.rb
rapnd-mikec54088-0.5.0 lib/rapnd/notification.rb