Sha256: f014518b5c59d6daea143267274013d7de51d5a2b527212f46550d4a6c824995

Contents?: true

Size: 783 Bytes

Versions: 3

Compression:

Stored size: 783 Bytes

Contents

require 'securerandom'
require 'json'

module Apnotic

  class Notification
    attr_reader :token
    attr_accessor :alert, :badge, :sound, :content_available, :category, :custom_payload
    attr_accessor :apns_id, :expiration, :priority, :topic

    def initialize(token)
      @token   = token
      @apns_id = SecureRandom.uuid
    end

    def body
      JSON.dump(to_hash).force_encoding(Encoding::BINARY)
    end

    private

    def to_hash
      aps = { alert: alert }
      aps.merge!(badge: badge) if badge
      aps.merge!(sound: sound) if sound
      aps.merge!(content_available: content_available) if content_available
      aps.merge!(category: category) if category

      n = { aps: aps }
      n.merge!(custom_payload) if custom_payload
      n
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
apnotic-0.8.1 lib/apnotic/notification.rb
apnotic-0.8.0 lib/apnotic/notification.rb
apnotic-0.7.0 lib/apnotic/notification.rb