Sha256: f2bf494376315e0949922424b0c7abdd786eae467522cf8df0041290541daedf

Contents?: true

Size: 1021 Bytes

Versions: 32

Compression:

Stored size: 1021 Bytes

Contents

class Agilibox::FCM::Request < Agilibox::Service
  URL = "https://fcm.googleapis.com/fcm/send"

  class << self
    attr_writer :api_key

    def api_key
      @api_key ||= ENV["FCM_API_KEY"]
    end
  end

  initialize_with :request_body

  attr_reader :response_json

  def call
    response_body = HTTParty.post(URL,
      :body    => request_body.to_json,
      :headers => {
        "Content-Type"  => "application/json",
        "Authorization" => "key=#{self.class.api_key}",
      },
    ).body

    @response_json = JSON.parse(response_body).deep_symbolize_keys

    self
  rescue JSON::ParserError
    @response_json = {success: 0, failure: 1, results: [{:error => "InvalidJsonResponse"}]}
    self
  end

  def ok?
    response_json[:success].positive? && response_json[:failure].zero?
  end

  def error?
    !ok?
  end

  def errors
    response_json[:results].map { |r| r[:error] }.compact
  end

  def invalid_token?
    errors.include?("NotRegistered") || errors.include?("InvalidRegistration")
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
agilibox-1.5.7 app/libs/agilibox/fcm/request.rb
agilibox-1.5.6 app/libs/agilibox/fcm/request.rb
agilibox-1.5.5 app/libs/agilibox/fcm/request.rb
agilibox-1.5.4 app/libs/agilibox/fcm/request.rb
agilibox-1.5.3 app/libs/agilibox/fcm/request.rb
agilibox-1.5.2 app/libs/agilibox/fcm/request.rb
agilibox-1.5.1 app/libs/agilibox/fcm/request.rb
agilibox-1.5.0 app/libs/agilibox/fcm/request.rb
agilibox-1.4.4 app/libs/agilibox/fcm/request.rb
agilibox-1.4.3 app/libs/agilibox/fcm/request.rb
agilibox-1.4.2 app/libs/agilibox/fcm/request.rb
agilibox-1.4.1 app/libs/agilibox/fcm/request.rb