module OwO::API

List of methods representing endpoints in OwO's API

Constants

SHORTEN_URL
UPLOAD_URL

Public Instance Methods

request(type, *attributes) click to toggle source

Handles requests given by the client

# File lib/owo/api.rb, line 15
def request(type, *attributes)
  raw = RestClient.send(type, *attributes, :'User-Agent' => "WhatsThisClient (https://github.com/whats-this/owo.rb, v#{OwO::VERSION})")
  json = parse_json(raw)
  return json
rescue RestClient::RequestEntityTooLarge
  raise OwO::Err::TooLarge, 'Requested files are too large!'
rescue RestClient::Unauthorized
  raise OwO::Err::BadToken, 'Token is invalid!'
rescue RestClient::BadRequest => e
  raw = e.response
  json = parse_json(raw)
  raise OwO::Err::TooManyFiles, 'You requested too many files!' if json.is_a?(Hash) && json['description'] == 'too many files'
  raise OwO::Err::BadURL, 'Your URL is invalid!' if !json.is_a?(Hash) && raw == 'invalid URL'
  err = if json.is_a?(Hash)
          json['description']
        else
          raw
        end
  raise err
rescue RestClient::InternalServerError
  raise OwO::Err::ServerFail, 'Server Error!'
rescue RuntimeError => e
  raise e
end
shorten(opts, url) click to toggle source

Requests a url to be shortened

# File lib/owo/api.rb, line 50
def shorten(opts, url)
  request(
    :get,
    "#{opts['a']}#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
  ).sub(/awau\.moe/, opts['u'])
end
upload(opts, files) click to toggle source

Requests a file(s) to be uploaded

# File lib/owo/api.rb, line 41
def upload(opts, files)
  request(
    :post,
    "#{opts['a']}#{UPLOAD_URL}?key=#{opts['t']}",
    'files'.to_sym => files
  )
end

Private Instance Methods

parse_json(raw) click to toggle source
# File lib/owo/api.rb, line 59
def parse_json(raw)
  JSON.parse(raw)
rescue JSON::ParserError
  raw
end