Sha256: feadfaa227beeffd308942d52fbe10d83fc207bfd84201412cbfd3f9d9baee6e

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Suj
  module Pusher
    class ApnNotification
      include Suj::Pusher::Logger
      MAX_SIZE = 256

      class InvalidToken < StandardError; end
      class PayloadTooLarge < StandardError; end

      def initialize(options = {})
        @token = options[:token]
        @ttl = options[:time_to_live] || 0
        @options = options
        raise InvalidToken if @token.nil? || (@token.length != 64)
        raise PayloadTooLarge if data.size > MAX_SIZE
      end

      def payload
        @payload ||= MultiJson.dump(@options[:data] || {})
      end

      def data
        @data ||= encode_data
      end

      private

      def get_expiry
        if @ttl.to_i == 0
          return 0
        else
          return Time.now.to_i + @ttl.to_i
        end
      end

      def encode_data
        identifier = 0
        expiry = get_expiry
        size = [payload].pack("a*").size
        data_array = [1, identifier, expiry, 32, @token, size, payload]
        info("PAYLOAD: #{data_array}")
        data_array.pack("cNNnH*na*")
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
suj-pusher-0.2.0 lib/suj/pusher/apn_notification.rb
suj-pusher-0.1.5 lib/suj/pusher/apn_notification.rb
suj-pusher-0.1.4 lib/suj/pusher/apn_notification.rb
suj-pusher-0.1.3 lib/suj/pusher/apn_notification.rb
suj-pusher-0.1.0 lib/suj/pusher/apn_notification.rb