class Tripletexer::APIClient

Constants

ENDPOINT

Attributes

debug[R]
object_class[R]
proxy[R]
session_token[R]

Public Class Methods

new(object_class: Hash, proxy: nil, debug: false) click to toggle source
# File lib/tripletexer/api_client.rb, line 14
def initialize(object_class: Hash, proxy: nil, debug: false)
  @object_class = object_class
  @proxy = proxy
  @debug = debug
end

Public Instance Methods

connection() click to toggle source
# File lib/tripletexer/api_client.rb, line 20
def connection
  return @connection if session_token && @connection
  @connection = init_connection
end
delete(path, *args, &block) click to toggle source
# File lib/tripletexer/api_client.rb, line 46
def delete(path, *args, &block)
  call(:delete, path, *args, &block)
end
get(path, *args, &block) click to toggle source
# File lib/tripletexer/api_client.rb, line 34
def get(path, *args, &block)
  call(:get, path, *args, &block)
end
post(path, *args, &block) click to toggle source
# File lib/tripletexer/api_client.rb, line 38
def post(path, *args, &block)
  call(:post, path, *args, &block)
end
put(path, *args, &block) click to toggle source
# File lib/tripletexer/api_client.rb, line 42
def put(path, *args, &block)
  call(:put, path, *args, &block)
end
reset_connection() click to toggle source
# File lib/tripletexer/api_client.rb, line 25
def reset_connection
  @session_token = @connection = nil
end
session_token=(new_session_token) click to toggle source
# File lib/tripletexer/api_client.rb, line 29
def session_token=(new_session_token)
  reset_connection
  @session_token = new_session_token
end

Private Instance Methods

call(method, path, *args, &block) click to toggle source
# File lib/tripletexer/api_client.rb, line 67
def call(method, path, *args, &block)
  normalized_path = URI.escape(path)
  response = connection.public_send(method, normalized_path, *args, &block)
  handle_response(response)
end
handle_response(response) click to toggle source
# File lib/tripletexer/api_client.rb, line 73
def handle_response(response)
  body = response.body
  case response.status
  when 200, 201, 204
    body
  when 400, 422
    raise ::Tripletexer::Errors::BadRequest, body
  when 401
    raise ::Tripletexer::Errors::Unauthorized, body
  when 403
    raise ::Tripletexer::Errors::Forbidden, body
  when 404
    raise ::Tripletexer::Errors::NotFound, body
  when 409
    raise ::Tripletexer::Errors::Conflict, body
  when 500
    raise ::Tripletexer::Errors::InternalError, body
  else
    raise NotImplementedError, "don't know how to handle #{response.status} http status code"
  end
end
init_connection() click to toggle source
# File lib/tripletexer/api_client.rb, line 54
def init_connection
  Faraday.new(url: ENDPOINT) do |faraday|
    faraday.response :logger if debug
    faraday.response :json, parser_options: { object_class: object_class }, content_type: %r[/json$]
    faraday.headers = {
      'Content-Type' => 'application/json'
    }
    faraday.adapter :net_http
    faraday.basic_auth(0, session_token) if session_token
    faraday.proxy = proxy if proxy # https://github.com/lostisland/faraday/issues/733
  end
end