class Tarpon::Request::Base

Constants

DEFAULT_HEADERS

Protected Instance Methods

perform(method:, path:, key:, headers: {}, body: nil) click to toggle source
# File lib/tarpon/request/base.rb, line 15
def perform(method:, path:, key:, headers: {}, body: nil)
  HTTP
    .timeout(Client.timeout)
    .auth("Bearer #{api_key(key)}")
    .headers(headers.merge(DEFAULT_HEADERS))
    .send(method, "#{Client.base_uri}#{path}", json: body&.compact)
    .yield_self { |http_response| handle_response(http_response) }
rescue HTTP::TimeoutError => e
  raise Tarpon::TimeoutError, e
end

Private Instance Methods

api_key(type) click to toggle source
# File lib/tarpon/request/base.rb, line 28
def api_key(type)
  Client.send("#{type}_api_key")
end
create_response(http_response) click to toggle source
# File lib/tarpon/request/base.rb, line 38
def create_response(http_response)
  Tarpon::Response.new(http_response.status, parse_body(http_response))
end
handle_error(http_response) click to toggle source
# File lib/tarpon/request/base.rb, line 51
def handle_error(http_response)
  case http_response.code
  when 401
    raise Tarpon::InvalidCredentialsError, 'Invalid credentials, fix your API keys'
  when 500..599
    raise Tarpon::ServerError, 'RevenueCat failed to fulfill the request'
  when 404
    raise Tarpon::NotFoundError
  else
    create_response(http_response)
  end
end
handle_response(http_response) click to toggle source
# File lib/tarpon/request/base.rb, line 42
def handle_response(http_response)
  case http_response.code
  when 200..299
    create_response(http_response)
  else
    handle_error(http_response)
  end
end
parse_body(http_response) click to toggle source
# File lib/tarpon/request/base.rb, line 32
def parse_body(http_response)
  return {} if http_response.body.empty?

  JSON.parse(http_response.body.to_s, symbolize_names: true)
end