Sha256: 1532bcdaf336d7a641ed603ef2f41f266384f241a82ef169586205ce059145a8

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'json'
require 'active_support/core_ext/hash'

module Moromi
  module Apns
    class Parameter
      attr_reader :alert
      attr_reader :badge
      attr_reader :sound
      attr_reader :content_available
      attr_reader :priority
      attr_reader :custom_data

      def initialize(alert:, badge:, sound: 'default', content_available: 1, priority: 10, custom_data: {})
        @alert = alert
        @badge = badge
        @sound = sound
        @content_available = content_available
        @priority = priority
        @custom_data = custom_data
      end

      def self.make_silent_push_parameter(priority: 10, custom_data: {})
        new(alert: '', badge: nil, sound: nil, content_available: 1, priority: priority, custom_data: custom_data)
      end

      def ==(other)
        serialize == other.serialize
      end

      def serialize
        {
          alert: @alert,
          badge: @badge,
          sound: @sound,
          content_available: @content_available,
          priority: @priority,
          custom_data: @custom_data
        }.to_json
      end

      def self.unserialize(json)
        hash = JSON.parse(json).with_indifferent_access
        new(
          alert: hash[:alert],
          badge: hash[:badge],
          sound: hash[:sound],
          content_available: hash[:content_available],
          priority: hash[:priority],
          custom_data: hash[:custom_data]
        )
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moromi-apns-0.3.0 lib/moromi/apns/parameter.rb
moromi-apns-0.2.0 lib/moromi/apns/parameter.rb
moromi-apns-0.1.0 lib/moromi/apns/parameter.rb