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

.format_user_agent(user_agent) ⇒ Object


42
43
44
45
46
# File 'lib/weeb/api.rb', line 42

def format_user_agent(user_agent)
  user_agent = "#{user_agent['botname']}/#{user_agent['version']}#{"/#{user_agent['env']}" if user_agent['env']}" if user_agent.is_a?(Hash)
  return nil unless user_agent.is_a?(String) && !user_agent.empty?
  user_agent
end

.parse_json(raw) ⇒ Object


48
49
50
51
52
# File 'lib/weeb/api.rb', line 48

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

.request(type, *attributes) ⇒ Object


12
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
# File 'lib/weeb/api.rb', line 12

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