class Paperdrive::Client

Client to aceess API

Constants

API_BASEURL

Public Class Methods

new(token: nil) click to toggle source

build instances by
using arguments: pass the token as `token:`
or using environment variables: set the token to ENV

@param [String] token: Paperdrive API access token @return [Paperdrive::Client] instance

# File lib/paperdrive/client.rb, line 81
def initialize(token: nil)
  token ||= ENV['PAPERDRIVE_TOKEN']
  raise Paperdrive::AuthTokenNotSpecified unless token || token.is_a?(String)

  @token = token
end

Public Instance Methods

inspect() click to toggle source

masking tokens to prevent casually displaying tokens

@return [String] inspected and token masked string

Calls superclass method
# File lib/paperdrive/client.rb, line 91
def inspect
  inspected = super
  inspected.gsub!(@token, 'MASKED_TOKEN') if @token
end

Protected Instance Methods

request(method, path, params) click to toggle source

make crud request

# File lib/paperdrive/client.rb, line 99
def request(method, path, params)
  raise Paperdrive::InvalidRequestMethod unless %i[get post put delete].include?(method.to_sym)

  raw_response = connection.send(method, build_path(path), params)
  Paperdrive::Response.new(raw_response)
end

Private Instance Methods

build_path(path) click to toggle source

build path which has `api_token:` query param

# File lib/paperdrive/client.rb, line 118
def build_path(path)
  "#{path}?#{{ api_token: @token }.to_query}"
end
connection() click to toggle source

memonize Faraday connection

# File lib/paperdrive/client.rb, line 109
def connection
  @connection ||= Faraday.new(url: API_BASEURL) do |faraday|
    faraday.request :json
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end
end