Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FirebaseTokenService
- EXCEPTIONS = [
- SocketError,
- RestClient::BadRequest,
- RestClient::Exceptions::OpenTimeout,
- RestClient::RequestTimeout,
- RestClient::InternalServerError,
- RestClient::BadGateway,
- RestClient::ServiceUnavailable,
- RestClient::GatewayTimeout,
- RestClient::BandwidthLimitExceeded,
- Errno::ECONNREFUSED
- ].freeze
- API_KEY = Rails.application.credentials.fetch(:firebase_api_key, '')
- FIREBASE_URL = "https://identitytoolkit.googleapis.com/v1/accounts:lookup?key=#{API_KEY}"
- attr_reader :token
- attr_accessor :error
- def initialize(token:)
- @token = token
- end
- def call
- begin
- request = RestClient::Request.new(
- url: FIREBASE_URL,
- payload: payload,
- content_type: :json,
- method: :post,
- timeout: 60,
- open_timeout: 4
- ).execute
- rescue *EXCEPTIONS=> e
- @error = e.response
- end
- response_firebase(request.try(:code) == 200 ? request : error)
- end
- private
- def response_firebase(request)
- JSON.parse(request.body)
- end
- def payload
- { idToken: token }
- end
- end
Add Comment
Please, Sign In to add comment