class Base::Endpoint

The base class for an endpoint.

It handles request lifecycle and error handling and offers a Faraday::Connection

Attributes

connection[R]
path[R]

Public Class Methods

new(access_token:, url:) click to toggle source

Initializes the endpoint with an access_token and url.

# File lib/base/endpoint.rb, line 31
def initialize(access_token:, url:)
  @connection =
    Faraday.new(
      "#{url}/v1/#{path}/",
      headers: { 'Authorization' => "Bearer #{access_token}" }
    ) do |conn|
      conn.request :multipart
      conn.request :url_encoded

      conn.use RaiseError
      conn.use Faraday::Adapter::NetHttp
    end
end

Public Instance Methods

io(body) click to toggle source
# File lib/base/endpoint.rb, line 62
def io(body)
  case body
  when IO
    body
  when String
    StringIO.new(body)
  else
    IO.try_convert(body)
  end
end
parse(body) click to toggle source
# File lib/base/endpoint.rb, line 54
def parse(body)
  object = JSON.parse(body, object_class: OpenStruct)

  object.created_at = Time.rfc2822(object.created_at) if object.created_at

  object
end
request() { || ... } click to toggle source

Handles errors that happen in its block.

# File lib/base/endpoint.rb, line 46
def request
  yield
rescue Unauthorized, InvalidRequest => e
  raise e
rescue StandardError => e
  raise UnkownError.new(e)
end