Method: OwO::API.request

Defined in:
lib/owo/api.rb

.request(type, *attributes) ⇒ Object

Handles requests given by the client


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