class Anilibria::Api::Client

Constants

API_VERSION
AUTH_ENDPOINT
DryTypes
ENDPOINT

Attributes

connection[R]

Public Class Methods

api_method(method_name, api_method_name = nil, returns:, http_method: :get) click to toggle source
# File lib/anilibria/api/client.rb, line 5
def api_method(method_name, api_method_name = nil, returns:, http_method: :get)
  (@api_methods ||= []) << method_name.to_sym
  api_method_name ||= camelize(method_name.to_s)

  define_method(method_name) do |params = {}|
    response_body = call(api_method_name, params, http_method)
    returns[response_body]
  end
end
api_methods() click to toggle source
# File lib/anilibria/api/client.rb, line 15
def api_methods
  (@api_methods ||= []).dup
end
new(url: ENDPOINT) click to toggle source
# File lib/anilibria/api/client.rb, line 43
def initialize(url: ENDPOINT)
  @connection = Faraday.new(
    url: url,
    headers: { 'User-Agent' => "anilibria-api-ruby/#{VERSION}" }
  ) do |f|
    f.request :url_encoded
    f.response :json, parser_options: { symbolize_names: true }
  end
end

Private Class Methods

camelize(method_name) click to toggle source
# File lib/anilibria/api/client.rb, line 21
def camelize(method_name)
  words = method_name.split('_')
  words.drop(1).map(&:capitalize!)
  words.join
end

Public Instance Methods

api_version() click to toggle source
# File lib/anilibria/api/client.rb, line 84
def api_version
  connection.head.headers['API-Version']
end
auth(mail, passwd) click to toggle source
# File lib/anilibria/api/client.rb, line 63
def auth(mail, passwd)
  response = auth_response(mail, passwd)

  return unless auth_successful?(response)

  response[:sessionId]
end
auth!(mail, passwd) click to toggle source
# File lib/anilibria/api/client.rb, line 71
def auth!(mail, passwd)
  response = auth_response(mail, passwd)

  unless auth_successful?(response)
    raise(
      Exceptions::AuthError.new(response),
      'Failed authorization attempt'
    )
  end

  response[:sessionId]
end
call(method_name, params = {}, http_method = :get) click to toggle source
# File lib/anilibria/api/client.rb, line 53
def call(method_name, params = {}, http_method = :get)
  http_method = :get unless %i[get post put patch delete].include?(http_method)
  response = connection.public_send(http_method, method_name) do |req|
    req.params.merge!(params)
  end

  check_response!(response)
  response.body
end

Private Instance Methods

auth_response(mail, passwd) click to toggle source
# File lib/anilibria/api/client.rb, line 103
def auth_response(mail, passwd)
  response = connection.post(
    AUTH_ENDPOINT,
    { mail: mail, passwd: passwd }
  )

  begin
    JSON.parse(response.body, { symbolize_names: true })
  rescue JSON::ParserError
    {}
  end
end
auth_successful?(response) click to toggle source
# File lib/anilibria/api/client.rb, line 116
def auth_successful?(response)
  response[:key] == 'success' && response.key?(:sessionId)
end
check_response!(response) click to toggle source
# File lib/anilibria/api/client.rb, line 90
def check_response!(response)
  return if response.status == 200

  if !response.body.respond_to?(:to_hash) ||
     !response.body[:error].respond_to?(:to_hash)
    raise Exceptions::ResponseError.new(
      'Unexpected response from API', response
    )
  end

  raise Exceptions::ApiError.new(response), response.body.dig(:error, :message)
end