Module: OwO::API

Defined in:
lib/owo/api.rb

Overview

List of methods representing endpoints in OwO’s API

Constant Summary collapse

UPLOAD_URL =
'/upload/pomf'.freeze
SHORTEN_URL =
'/shorten/polr'.freeze

Class Method Summary collapse

Class Method Details

.request(type, *attributes) ⇒ Object

Handles requests given by the client

[View source]

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

Requests a url to be shortened

[View source]

50
51
52
53
54
55
# 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) ⇒ Object

Requests a file(s) to be uploaded

[View source]

41
42
43
44
45
46
47
# 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