Class: OwO::WhatsThis

Inherits:
Object
  • Object
show all
Defined in:
lib/owo.rb

Overview

The main module for interaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, url: 'owo.whats-th.is', api_url: 'https://api.awau.moe') ⇒ WhatsThis

Create a class to upload files and shorten URLs with OWO.

Parameters:

  • token (String)

    the token required to use OwO.

  • url (String) (defaults to: 'owo.whats-th.is')

    the domain to use when returning URLs.

  • api_url (String) (defaults to: 'https://api.awau.moe')

    the url to use when using the OwO API. (This requires https:// or http://)

Raises:



22
23
24
25
26
27
28
29
# File 'lib/owo.rb', line 22

def initialize(token, url: 'owo.whats-th.is', api_url: 'https://api.awau.moe')
  @token = token
  @url = url
  @api_url = api_url

  raise OwO::Err::BadToken, 'Token is empty!' if token.empty?
  raise OwO::Err::BadToken, 'Input your OwO token to test the client.' if token == 'TOKEN'
end

Instance Attribute Details

#api_urlString

Returns the url being used to use the OwO API.

Returns:

  • (String)

    the url being used to use the OwO API.



16
17
18
# File 'lib/owo.rb', line 16

def api_url
  @api_url
end

#tokenString

Returns the token used for OwO.

Returns:

  • (String)

    the token used for OwO.



10
11
12
# File 'lib/owo.rb', line 10

def token
  @token
end

#urlString

Returns the domain to use when returning URLs.

Returns:

  • (String)

    the domain to use when returning URLs.



13
14
15
# File 'lib/owo.rb', line 13

def url
  @url
end

Instance Method Details

#shorten(*urls) ⇒ String+

Shortens a link with OwO.

Parameters:

  • urls (String, Array<String>)

    URLs to shorten

Returns:

  • (String, Array<String>)

    Returns the URLs of the shortened URLs

Raises:



63
64
65
66
67
68
69
# File 'lib/owo.rb', line 63

def shorten(*urls)
  urls = urls.flatten
  raise OwO::Err::NoContent, 'There is a empty string in your arguments!' if urls.include? ''
  raise OwO::Err::NoContent, 'Theres no URLs provided!' if urls.empty?
  result = urls.flatten.map { |x| OwO::API.shorten(opts, x) }
  result[0] if urls.length == 1 || !urls.first.is_a?(Array)
end

#upload(*files) ⇒ String+

Upload a single file or multiple files to OwO.

Parameters:

  • files (File, String, Array<File, String>)

    Files to upload, can be either String or File

Returns:

  • (String, Array<String>)

    Returns the URLs of the uploaded files

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/owo.rb', line 34

def upload(*files)
  ogfiles = files
  files = files.flatten
  raise OwO::Err::NoContent, 'There is a empty string in your arguments!' if files.include? ''
  raise OwO::Err::NoContent, 'Theres no files provided!' if files.empty?
  files = files.map do |x|
    return x if x.respond_to? :read
    begin
      File.new(File.absolute_path(x), 'rb')
    rescue Errno::ENOENT, Errno::EACCES, Errno::ENAMETOOLONG => e
      errstring = 'Unknown'
      case e.class.name
      when 'Errno::ENOENT'
        errstring = 'File Not Found'
      when 'Errno::EACCES'
        errstring = 'Permission Denied'
      when 'Errno::ENAMETOOLONG'
        errstring = 'Name Too Long'
      end
      raise OwO::Err::FileError, "Error initializing file '${x}' | #{errstring} | #{e.class.name}"
    end
  end
  result = OwO::API.upload(opts, files)['files'].map { |x| "https://#{@url}/#{x['url']}" }
  result[0] if ogfiles.length == 1 || !ogfiles.first.is_a?(Array)
end