class CurseClient::API

Constants

DEFAULT_URL

Attributes

token[RW]
url[R]

Public Class Methods

new(url = DEFAULT_URL) click to toggle source
# File lib/curse_client/api.rb, line 10
def initialize(url = DEFAULT_URL)
  @url = url
end

Public Instance Methods

addon(id) click to toggle source
# File lib/curse_client/api.rb, line 34
def addon(id)
  get_json("addon/#{id}")
end
addon_file(addon_id, file_id) click to toggle source
# File lib/curse_client/api.rb, line 42
def addon_file(addon_id, file_id)
  get_json("addon/#{addon_id}/file/#{file_id}")
end
addon_files(id) click to toggle source
# File lib/curse_client/api.rb, line 38
def addon_files(id)
  get_json("addon/#{id}/files")
end
authenticate(username, password) click to toggle source
# File lib/curse_client/api.rb, line 18
def authenticate(username, password)
  response = HTTP.post("#{url}/authenticate",
                        { username: username, password: password },
                        headers: { "Content-Type" => "application/json" },
                        format: :json)
  case response
  when Net::HTTPUnauthorized
    raise UnauthorizedError, "Invalid username or password"
  when Net::HTTPSuccess
    data = JSON.parse(response.body, symbolize_names: true)
    @token = "#{data[:session][:user_id]}:#{data[:session][:token]}"
  else
    response.error!
  end
end
authenticated?() click to toggle source
# File lib/curse_client/api.rb, line 14
def authenticated?
  !token.nil?
end

Private Instance Methods

get_json(path) click to toggle source
# File lib/curse_client/api.rb, line 50
def get_json(path)
  response = HTTP.get("#{url}/#{path}",
                       headers: { "Authorization" => "Token #{token}" })
  case response
  when Net::HTTPUnauthorized
    raise UnauthorizedError, "Invalid token"
  when Net::HTTPSuccess
    return JSON.parse(response.body, symbolize_names: true)
  else
    response.error!
  end
end