Sha256: 4f70c8025996b28b968c4b20624cf69230ed33d934f31d89cbd9cfb755a48b0e

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 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 :mutable_content
      attr_reader :category
      attr_reader :priority
      attr_reader :custom_data

      def initialize(alert:, badge:, sound: 'default', content_available: 1, mutable_content: 0, category: nil, priority: 10, custom_data: {})
        @alert = alert
        @badge = badge
        @sound = sound
        @content_available = content_available
        @mutable_content = mutable_content
        @category = category
        @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,
          mutable_content: @mutable_content,
          category: @category,
          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],
          mutable_content: hash[:mutable_content],
          category: hash[:category],
          priority: hash[:priority],
          custom_data: hash[:custom_data]
        )
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
moromi-apns-0.9.0 lib/moromi/apns/parameter.rb
moromi-apns-0.8.0 lib/moromi/apns/parameter.rb
moromi-apns-0.7.0 lib/moromi/apns/parameter.rb
moromi-apns-0.6.0 lib/moromi/apns/parameter.rb