Module: OwO::API

Defined in:
lib/owo/api.rb

Overview

List of methods representing endpoints in OwO’s API

Constant Summary collapse

UPLOAD_URL =
'https://api.awau.moe/upload/pomf'.freeze
SHORTEN_URL =
'https://api.awau.moe/shorten/polr'.freeze

Class Method Summary collapse

Class Method Details

.parse_json(raw) ⇒ Object



39
40
41
42
43
# File 'lib/owo/api.rb', line 39

def parse_json(raw)
  JSON.parse(raw)
rescue JSON::ParserError
  raw
end

.request(type, *attributes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/owo/api.rb', line 14

def request(type, *attributes)
  raw = RestClient.send(type, *attributes, :'User-Agent' => "OwO.rb v#{OwO::VERSION} (https://github.com/whats-this/owo.rb)")
  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



53
54
55
56
57
58
# File 'lib/owo/api.rb', line 53

def shorten(opts, url)
  request(
    :get,
    "#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
  ).sub(/awau\.moe/, opts['s'])
end

.upload(opts, files) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/owo/api.rb', line 45

def upload(opts, files)
  request(
    :post,
    "#{UPLOAD_URL}?key=#{opts['t']}",
    'files'.to_sym => files
  )
end