Sha256: 2eee9d737d7ab0285743e04c46b525998b6363ec8fd425f38f754819efed3180

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

module JeraPush
  module Services
    class SendMessage

      def initialize(*args)
        @type = args.first[:type].to_sym
        @message = args.first[:message]
        @devices = args.first[:devices]
      end

      def call
        return false unless valid?

        message_content = format_hash(@message)
        case @type
        when :broadcast
          # Topicos não estão funcionando.
          # JeraPush::Message.send_broadcast(content: message_content)
        when :specific
          target_devices = JeraPush::Device.where(id: @devices.uniq.map(&:to_i))
          push = JeraPush::PushBody.new(
            title: message_content[:title],
            body: message_content[:body],
            data: message_content,
            devices: target_devices
          )
          result = JeraPush::Services::SendToDevicesService.new(push: push).call
          result
        end
      end

      def valid?
        valid = @message.present? && @type.present?
        @type.to_sym == :broadcast ? valid : valid && @devices.present?
      end

      def format_hash(messages = [])
        return false if messages.blank?
        messages.collect do |obj|
          hash = { obj[:key].to_sym => obj[:value] }
          hash.delete_if { |key, value| key.blank? || value.blank? }
        end.reduce(:merge)
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jera_push-2.1.4 lib/jera_push/services/send_message.rb
jera_push-2.1.3 lib/jera_push/services/send_message.rb
jera_push-2.1.2 lib/jera_push/services/send_message.rb