Sha256: 97a0f890b2be4bb034258f8ea124683cfedea692b6adbfe5e419248a787b1ba8

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

require 'json'

module Moromi
  module Aws
    module Sns
      module Message
        class Parameter
          # @param [Moromi::Aws::Sns::Message::Apns] apns
          # @param [Moromi::Aws::Sns::Message::Fcm] gcm
          def initialize(apns: nil, gcm: nil)
            @apns = apns
            @gcm = gcm
          end

          def to_hash
            {
              klass_name: self.class.name,
              apns: {name: @apns&.class&.name, data: @apns&.to_hash},
              gcm: {name: @gcm&.class&.name, data: @gcm&.to_hash}
            }
          end

          def to_json
            {
              'default': '',
              'APNS_SANDBOX': @apns&.to_message_json,
              'APNS': @apns&.to_message_json,
              'GCM': @gcm&.to_message_json
            }.compact.to_json
          end

          def self.build_from_hash(hash)
            apns_klass = hash.dig(:apns, :name)&.safe_constantize
            apns = apns_klass&.build_from_hash(hash.dig(:apns, :data))

            gcm_klass = hash.dig(:gcm, :name)&.safe_constantize
            gcm = gcm_klass&.build_from_hash(hash.dig(:gcm, :data))

            new(apns: apns, gcm: gcm)
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moromi-aws-sns-0.9.0 lib/moromi/aws/sns/message/parameter.rb
moromi-aws-sns-0.8.0 lib/moromi/aws/sns/message/parameter.rb
moromi-aws-sns-0.7.0 lib/moromi/aws/sns/message/parameter.rb