Sha256: 991c8c1b9d8f6025017ab141952c0d3a74701f007d9523ca18709d0af4145039

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

module JeraPush::Services
  class SendPushService < JeraPush::Services::BaseService
    def initialize(push:, message:, message_device:)
      super
      @push = push
      @message = message
      @message_device = message_device
    end

    def call
      return @errors if @errors.present?

      send_push

      @message
    end

    private

    def send_push
      @response = @firebase.send_to_device(push: @push)
      save_success
    rescue Google::Apis::AuthorizationError => e
      message = JSON.parse(e.body)
      save_error(message)
    rescue Google::Apis::ClientError => e
      response = JSON.parse(e.body, symbolize_names: true)
      error_code = response[:error][:details][0][:errorCode]
      save_error(response)
    end

    def save_success
      @message_device.update(status: :success)
      @message.update(success_count: @message.success_count + 1)
    end

    def save_error(error)
      @message_device.update(status: :error, error_message: error.to_json)
      @message.update(failure_count: @message.failure_count + 1)
    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_push_service.rb
jera_push-2.1.3 lib/jera_push/services/send_push_service.rb
jera_push-2.1.2 lib/jera_push/services/send_push_service.rb