Sha256: 0fab4f99422234400c148ea8b5513f86951be5b809e891f418f93f1bef3698ff

Contents?: true

Size: 1013 Bytes

Versions: 21

Compression:

Stored size: 1013 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].pluck(:error).compact
  end

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

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
agilibox-2.0.0 app/libs/agilibox/fcm/request.rb
agilibox-1.11.0 app/libs/agilibox/fcm/request.rb
agilibox-1.10.5 app/libs/agilibox/fcm/request.rb
agilibox-1.10.2 app/libs/agilibox/fcm/request.rb
agilibox-1.10.1 app/libs/agilibox/fcm/request.rb
agilibox-1.10.0 app/libs/agilibox/fcm/request.rb
agilibox-1.9.20 app/libs/agilibox/fcm/request.rb
agilibox-1.9.19 app/libs/agilibox/fcm/request.rb
agilibox-1.9.18 app/libs/agilibox/fcm/request.rb
agilibox-1.9.17 app/libs/agilibox/fcm/request.rb
agilibox-1.9.16 app/libs/agilibox/fcm/request.rb
agilibox-1.9.15 app/libs/agilibox/fcm/request.rb
agilibox-1.9.14 app/libs/agilibox/fcm/request.rb
agilibox-1.9.13 app/libs/agilibox/fcm/request.rb
agilibox-1.9.12 app/libs/agilibox/fcm/request.rb
agilibox-1.9.11 app/libs/agilibox/fcm/request.rb
agilibox-1.9.10 app/libs/agilibox/fcm/request.rb
agilibox-1.9.9 app/libs/agilibox/fcm/request.rb
agilibox-1.9.8 app/libs/agilibox/fcm/request.rb
agilibox-1.9.7 app/libs/agilibox/fcm/request.rb