owo 1.2.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/owo.rb +71 -71
- data/lib/owo/api.rb +60 -71
- data/lib/owo/err.rb +68 -61
- data/lib/owo/ver.rb +3 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca1e08a5e54f9dd371a76883c245b11e827011cf
|
4
|
+
data.tar.gz: 225324af52d912ea4735cc8d840b3f377f3c2680
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5cfd7c0eff2c14e6fda5daf9481cadeb055af4e2ae4f7ef910c2c6c9cb17244e94bf8d27953da4cbeed6abeccd064e2a8a61630b5f7ee7b644249a54f54a5179
|
7
|
+
data.tar.gz: ddd779bd56dc7819668dd222503d08d9c22f8430694bdcefc7034d4a3ea4f89605074178689866552d3770620a9272a60da05c4dce943d8961b0610d26d0365b
|
data/lib/owo.rb
CHANGED
@@ -1,71 +1,71 @@
|
|
1
|
-
require 'owo/err'
|
2
|
-
require 'owo/api'
|
3
|
-
require 'rest-client'
|
4
|
-
|
5
|
-
# OwO base module
|
6
|
-
module OwO
|
7
|
-
# The main module for interaction
|
8
|
-
class WhatsThis
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
end
|
1
|
+
require 'owo/err'
|
2
|
+
require 'owo/api'
|
3
|
+
require 'rest-client'
|
4
|
+
|
5
|
+
# OwO base module
|
6
|
+
module OwO
|
7
|
+
# The main module for interaction
|
8
|
+
class WhatsThis
|
9
|
+
# The token to the module
|
10
|
+
attr_accessor :token
|
11
|
+
|
12
|
+
# The domain to use when returning upload URLs
|
13
|
+
attr_accessor :upload_url
|
14
|
+
|
15
|
+
# The domain to use when returning shortener URLs
|
16
|
+
attr_accessor :shorten_url
|
17
|
+
|
18
|
+
def initialize(token, upload_url: 'owo.whats-th.is', shorten_url: 'uwu.whats-th.is')
|
19
|
+
@token = token
|
20
|
+
@upload_url = upload_url
|
21
|
+
@shorten_url = shorten_url
|
22
|
+
end
|
23
|
+
|
24
|
+
def opts
|
25
|
+
{ 't' => @token, 'u' => @upload_url, 's' => @shorten_url }
|
26
|
+
end
|
27
|
+
|
28
|
+
# Upload a single file or multiple files to OwO.
|
29
|
+
# @param files [File, String, Array<File, String>] Files to upload, can be either String or File
|
30
|
+
# @return [String, Array<String>] Returns the URLs of the uploaded files
|
31
|
+
def upload(files)
|
32
|
+
ogfiles = files
|
33
|
+
raise OwO::Err::NoContent, 'Theres no files provided!' if files.empty?
|
34
|
+
files = [files] if files.is_a?(String)
|
35
|
+
files = files.map do |x|
|
36
|
+
return x unless x.is_a?(String)
|
37
|
+
begin
|
38
|
+
File.new(File.absolute_path(x), 'rb')
|
39
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENAMETOOLONG => e
|
40
|
+
errstring = 'Unknown'
|
41
|
+
case e.class.name
|
42
|
+
when 'Errno::ENOENT'
|
43
|
+
errstring = 'File Not Found'
|
44
|
+
when 'Errno::EACCES'
|
45
|
+
errstring = 'Permission Denied'
|
46
|
+
when 'Errno::ENAMETOOLONG'
|
47
|
+
errstring = 'Name Too Long'
|
48
|
+
end
|
49
|
+
raise OwO::Err::FileError, "#{errstring} | #{e.class.name}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
result = OwO::API.upload(opts, files)
|
53
|
+
result.shift
|
54
|
+
result = result['files'].map { |x| "https://#{@upload_url}/#{x['url']}" }
|
55
|
+
result = result[0] unless ogfiles.is_a?(Array)
|
56
|
+
result
|
57
|
+
end
|
58
|
+
|
59
|
+
# Shortens a link with OwO.
|
60
|
+
# @param urls [String, Array<String>] URLs to sharten
|
61
|
+
# @return [String, Array<String>] Returns the URLs of the shortened URLs
|
62
|
+
def shorten(urls)
|
63
|
+
raise OwO::Err::NoContent, 'Theres no URL provided!' if urls.empty?
|
64
|
+
if urls.is_a?(Array)
|
65
|
+
urls.map { |x| OwO::API.shorten(opts, x) }
|
66
|
+
else
|
67
|
+
OwO::API.shorten(opts, urls)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/owo/api.rb
CHANGED
@@ -1,71 +1,60 @@
|
|
1
|
-
require 'owo/err'
|
2
|
-
require 'owo/ver'
|
3
|
-
require 'uri'
|
4
|
-
require 'rest-client'
|
5
|
-
|
6
|
-
module OwO
|
7
|
-
# List of methods representing endpoints in OwO's API
|
8
|
-
module API
|
9
|
-
UPLOAD_URL =
|
10
|
-
SHORTEN_URL =
|
11
|
-
|
12
|
-
module_function
|
13
|
-
|
14
|
-
def request(type, *attributes)
|
15
|
-
raw = RestClient.send(type, *attributes, :'User-Agent' => "OwO.rb v#{OwO::VERSION} (https://github.com/whats-this/owo.rb)")
|
16
|
-
json = parse_json(raw)
|
17
|
-
return json
|
18
|
-
rescue RestClient::RequestEntityTooLarge
|
19
|
-
raise OwO::Err::TooLarge, 'Requested files are too large!'
|
20
|
-
rescue RestClient::Unauthorized
|
21
|
-
raise OwO::Err::BadToken, 'Token is invalid!'
|
22
|
-
rescue RestClient::BadRequest => e
|
23
|
-
raw = e.response
|
24
|
-
json = parse_json(raw)
|
25
|
-
if json.is_a?(Hash)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
rescue
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
def
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
def shorten(opts, url)
|
63
|
-
request(
|
64
|
-
:get,
|
65
|
-
"#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
|
66
|
-
).sub(/awau\.moe/, opts['s'])
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
# gem build owo.gemspec && gem install owo-1.0.0.gem && ruby test/upload.rb
|
1
|
+
require 'owo/err'
|
2
|
+
require 'owo/ver'
|
3
|
+
require 'uri'
|
4
|
+
require 'rest-client'
|
5
|
+
|
6
|
+
module OwO
|
7
|
+
# List of methods representing endpoints in OwO's API
|
8
|
+
module API
|
9
|
+
UPLOAD_URL = 'https://api.awau.moe/upload/pomf'.freeze
|
10
|
+
SHORTEN_URL = 'https://api.awau.moe/shorten/polr'.freeze
|
11
|
+
|
12
|
+
module_function
|
13
|
+
|
14
|
+
def request(type, *attributes)
|
15
|
+
raw = RestClient.send(type, *attributes, :'User-Agent' => "OwO.rb v#{OwO::VERSION} (https://github.com/whats-this/owo.rb)")
|
16
|
+
json = parse_json(raw)
|
17
|
+
return json
|
18
|
+
rescue RestClient::RequestEntityTooLarge
|
19
|
+
raise OwO::Err::TooLarge, 'Requested files are too large!'
|
20
|
+
rescue RestClient::Unauthorized
|
21
|
+
raise OwO::Err::BadToken, 'Token is invalid!'
|
22
|
+
rescue RestClient::BadRequest => e
|
23
|
+
raw = e.response
|
24
|
+
json = parse_json(raw)
|
25
|
+
raise OwO::Err::TooManyFiles, 'You requested too many files!' if json.is_a?(Hash) && json['description'] == 'too many files'
|
26
|
+
raise OwO::Err::BadURL, 'Your URL is invalid!' if !json.is_a?(Hash) && raw == 'invalid URL'
|
27
|
+
err = if json.is_a?(Hash)
|
28
|
+
json['description']
|
29
|
+
else
|
30
|
+
raw
|
31
|
+
end
|
32
|
+
raise err
|
33
|
+
rescue RestClient::InternalServerError
|
34
|
+
raise OwO::Err::ServerFail, 'Server Error!'
|
35
|
+
rescue RuntimeError => e
|
36
|
+
raise e
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_json(raw)
|
40
|
+
JSON.parse(raw)
|
41
|
+
rescue JSON::ParserError
|
42
|
+
raw
|
43
|
+
end
|
44
|
+
|
45
|
+
def upload(opts, files)
|
46
|
+
request(
|
47
|
+
:post,
|
48
|
+
"#{UPLOAD_URL}?key=#{opts['t']}",
|
49
|
+
'files'.to_sym => files
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def shorten(opts, url)
|
54
|
+
request(
|
55
|
+
:get,
|
56
|
+
"#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
|
57
|
+
).sub(/awau\.moe/, opts['s'])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/owo/err.rb
CHANGED
@@ -1,61 +1,68 @@
|
|
1
|
-
module OwO
|
2
|
-
# Custom errors raised in various places
|
3
|
-
module Err
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
1
|
+
module OwO
|
2
|
+
# Custom errors raised in various places
|
3
|
+
module Err
|
4
|
+
# Raised when a token is invalid or incorrect.
|
5
|
+
class BadToken < RuntimeError
|
6
|
+
# Default message for this exception
|
7
|
+
def message
|
8
|
+
'Token invalid'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Raised when too many files were uploaded.
|
13
|
+
class TooManyFiles < RuntimeError
|
14
|
+
# Default message for this exception
|
15
|
+
def message
|
16
|
+
'Too many files requested!'
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Raised when no files were sent.
|
21
|
+
class NoContent < RuntimeError
|
22
|
+
# Default message for this exception
|
23
|
+
def message
|
24
|
+
'No content found!'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Raised when requested URL is bad.
|
29
|
+
class BadURL < RuntimeError
|
30
|
+
# Default message for this exception
|
31
|
+
def message
|
32
|
+
'Your URL is invalid!'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Raised when requested file(s) have too much bytes.
|
37
|
+
class TooLarge < RuntimeError
|
38
|
+
# Default message for this exception
|
39
|
+
def message
|
40
|
+
'File(s) are too large!'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Raised when a server error occurs
|
45
|
+
class ServerFail < RuntimeError
|
46
|
+
# Default message for this exception
|
47
|
+
def message
|
48
|
+
'Server tried to do a thing and borked!'
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Raised when the error is unhandled
|
53
|
+
class Unhandled < RuntimeError
|
54
|
+
# Default message for this exception
|
55
|
+
def message
|
56
|
+
'Unhandled'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Raised when loading a file recieves an error
|
61
|
+
class FileError < RuntimeError
|
62
|
+
# Default message for this exception
|
63
|
+
def message
|
64
|
+
'Unknown'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/lib/owo/ver.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: owo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Snazzah
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/owo.rb
|
62
62
|
- lib/owo/api.rb
|
63
63
|
- lib/owo/err.rb
|
64
|
+
- lib/owo/ver.rb
|
64
65
|
homepage: https://github.com/whats-this/owo.rb
|
65
66
|
licenses:
|
66
67
|
- MIT
|