Module: WeebSh::API

Defined in:
lib/weeb/api.rb,
lib/weeb/api/tama.rb,
lib/weeb/api/toph.rb,
lib/weeb/api/korra.rb,
lib/weeb/api/shimakaze.rb

Overview

Main API functions

Defined Under Namespace

Modules: Korra, Shimakaze, Tama, Toph

Class Method Summary collapse

Class Method Details

.request(type, *attributes) ⇒ Object

Request an endpoint easily



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

def request(type, *attributes)
  parse_json(RestClient.send(type, *attributes))
rescue RestClient::RequestEntityTooLarge
  raise WeebSh::Err::TooLarge, 'Requested files are too large!'
rescue RestClient::Unauthorized => e
  json = parse_json(e.response)
  if json.is_a?(Hash)
    raise WeebSh::Err::BadAuth, 'Token is invalid!' if json['message'] == 'Unauthorized'
  end
rescue RestClient::BadRequest => e
  json = parse_json(e.response)
  if json.is_a?(Hash)
    raise WeebSh::Err::InvalidMIME, json['message'] if json['message'].start_with? 'The mimetype'
    raise WeebSh::Err::TagExists, json['message'] if json['message'] == 'Tags existed already or had no content'
    raise WeebSh::Err::ImagePrivate, json['message'] if json['message'] == 'This image is private'
    raise WeebSh::Err::InvalidImage, json['message'] if json['message'] == 'No image found for your query'
  end
  raise e
rescue RestClient::Forbidden => e
  json = parse_json(e.response)
  if json.is_a?(Hash)
    raise WeebSh::Err::MissingScope, json['message'] if json['message'].start_with? 'missing scope'
  end
  raise e
rescue RestClient::InternalServerError
  raise WeebSh::Err::ServerFail, 'Server Error!'
rescue RuntimeError => e
  raise e
end