owo 1.2.2 → 1.3.0.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 +80 -71
- data/lib/owo/api.rb +65 -60
- data/lib/owo/err.rb +68 -68
- data/lib/owo/ver.rb +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c622fa50671d197b6eb0c96859f19024ebe4ca6d
|
4
|
+
data.tar.gz: b6681af8a15fa995018d287884baf99c64a403af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ffadc940761314f1bfa11339b352db28b72d1ebc9bababd2f1b7430768e1abd83e94c7161771b430a17bbfe9656a8f4aa45af5ca3ac5eb1b769baff94c08d09
|
7
|
+
data.tar.gz: a71eab6cc22124094f9dd570c7bff3e2e80409d70e790542f51c719d80192833b4a7bc13bcc4032e03dd41c0dfddd4c9d4327b48ea86f683aa5197a37dee87c6
|
data/lib/owo.rb
CHANGED
@@ -1,71 +1,80 @@
|
|
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
|
-
attr_accessor :token
|
11
|
-
|
12
|
-
#
|
13
|
-
attr_accessor :
|
14
|
-
|
15
|
-
#
|
16
|
-
attr_accessor :
|
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
|
-
when 'Errno::
|
47
|
-
errstring = '
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
result
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
#
|
61
|
-
# @
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
70
|
-
|
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
|
+
# @return [String] the token used for OwO.
|
10
|
+
attr_accessor :token
|
11
|
+
|
12
|
+
# @return [String] the domain to use when returning URLs.
|
13
|
+
attr_accessor :url
|
14
|
+
|
15
|
+
# @return [String] the url being used to use the OwO API.
|
16
|
+
attr_accessor :api_url
|
17
|
+
|
18
|
+
# Create a class to upload files and shorten URLs with OWO.
|
19
|
+
# @param token [String] the token required to use OwO.
|
20
|
+
# @param url [String] the domain to use when returning URLs.
|
21
|
+
# @param api_url [String] the url to use when using the OwO API. (This requires https:// or http://)
|
22
|
+
def initialize(token, url: 'owo.whats-th.is', api_url: 'https://api.awau.moe')
|
23
|
+
@token = token
|
24
|
+
@url = url
|
25
|
+
@api_url = api_url
|
26
|
+
|
27
|
+
raise OwO::Err::BadToken, 'Token is empty!' if token.empty?
|
28
|
+
raise OwO::Err::BadToken, 'Input your OwO token to test the client.' if token == 'TOKEN'
|
29
|
+
end
|
30
|
+
|
31
|
+
# Upload a single file or multiple files to OwO.
|
32
|
+
# @param files [File, String, Array<File, String>] Files to upload, can be either String or File
|
33
|
+
# @return [String, Array<String>] Returns the URLs of the uploaded files
|
34
|
+
def upload(*files)
|
35
|
+
ogfiles = files
|
36
|
+
files = files.flatten
|
37
|
+
raise OwO::Err::NoContent, 'There is a empty string in your arguments!' if files.include? ''
|
38
|
+
raise OwO::Err::NoContent, 'Theres no files provided!' if files.empty?
|
39
|
+
files = files.map do |x|
|
40
|
+
return x if x.respond_to? :read
|
41
|
+
begin
|
42
|
+
File.new(File.absolute_path(x), 'rb')
|
43
|
+
rescue Errno::ENOENT, Errno::EACCES, Errno::ENAMETOOLONG => e
|
44
|
+
errstring = 'Unknown'
|
45
|
+
case e.class.name
|
46
|
+
when 'Errno::ENOENT'
|
47
|
+
errstring = 'File Not Found'
|
48
|
+
when 'Errno::EACCES'
|
49
|
+
errstring = 'Permission Denied'
|
50
|
+
when 'Errno::ENAMETOOLONG'
|
51
|
+
errstring = 'Name Too Long'
|
52
|
+
end
|
53
|
+
raise OwO::Err::FileError, "Error initializing file '${x}' | #{errstring} | #{e.class.name}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
result = OwO::API.upload(opts, files)['files'].map { |x| "https://#{@url}/#{x['url']}" }
|
57
|
+
result[0] if ogfiles.length == 1 || !ogfiles.first.is_a?(Array)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Shortens a link with OwO.
|
61
|
+
# @param urls [String, Array<String>] URLs to shorten
|
62
|
+
# @return [String, Array<String>] Returns the URLs of the shortened URLs
|
63
|
+
def shorten(*urls)
|
64
|
+
urls = urls.flatten
|
65
|
+
raise OwO::Err::NoContent, 'There is a empty string in your arguments!' if urls.include? ''
|
66
|
+
raise OwO::Err::NoContent, 'Theres no URLs provided!' if urls.empty?
|
67
|
+
result = urls.flatten.map { |x| OwO::API.shorten(opts, x) }
|
68
|
+
result[0] if urls.length == 1 || !urls.first.is_a?(Array)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def opts
|
74
|
+
{ 't' => @token, 'u' => @url, 'a' => @api_url }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Alias for the class {WhatsThis}
|
79
|
+
Client = WhatsThis
|
80
|
+
end
|
data/lib/owo/api.rb
CHANGED
@@ -1,60 +1,65 @@
|
|
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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
raise OwO::Err::
|
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
|
-
|
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 = '/upload/pomf'.freeze
|
10
|
+
SHORTEN_URL = '/shorten/polr'.freeze
|
11
|
+
|
12
|
+
module_function
|
13
|
+
|
14
|
+
# Handles requests given by the client
|
15
|
+
def request(type, *attributes)
|
16
|
+
raw = RestClient.send(type, *attributes, :'User-Agent' => "WhatsThisClient (https://github.com/whats-this/owo.rb, v#{OwO::VERSION})")
|
17
|
+
json = parse_json(raw)
|
18
|
+
return json
|
19
|
+
rescue RestClient::RequestEntityTooLarge
|
20
|
+
raise OwO::Err::TooLarge, 'Requested files are too large!'
|
21
|
+
rescue RestClient::Unauthorized
|
22
|
+
raise OwO::Err::BadToken, 'Token is invalid!'
|
23
|
+
rescue RestClient::BadRequest => e
|
24
|
+
raw = e.response
|
25
|
+
json = parse_json(raw)
|
26
|
+
raise OwO::Err::TooManyFiles, 'You requested too many files!' if json.is_a?(Hash) && json['description'] == 'too many files'
|
27
|
+
raise OwO::Err::BadURL, 'Your URL is invalid!' if !json.is_a?(Hash) && raw == 'invalid URL'
|
28
|
+
err = if json.is_a?(Hash)
|
29
|
+
json['description']
|
30
|
+
else
|
31
|
+
raw
|
32
|
+
end
|
33
|
+
raise err
|
34
|
+
rescue RestClient::InternalServerError
|
35
|
+
raise OwO::Err::ServerFail, 'Server Error!'
|
36
|
+
rescue RuntimeError => e
|
37
|
+
raise e
|
38
|
+
end
|
39
|
+
|
40
|
+
# Requests a file(s) to be uploaded
|
41
|
+
def upload(opts, files)
|
42
|
+
request(
|
43
|
+
:post,
|
44
|
+
"#{opts['a']}#{UPLOAD_URL}?key=#{opts['t']}",
|
45
|
+
'files'.to_sym => files
|
46
|
+
)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Requests a url to be shortened
|
50
|
+
def shorten(opts, url)
|
51
|
+
request(
|
52
|
+
:get,
|
53
|
+
"#{opts['a']}#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
|
54
|
+
).sub(/awau\.moe/, opts['u'])
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def parse_json(raw)
|
60
|
+
JSON.parse(raw)
|
61
|
+
rescue JSON::ParserError
|
62
|
+
raw
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/owo/err.rb
CHANGED
@@ -1,68 +1,68 @@
|
|
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
|
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
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module OwO
|
2
|
-
VERSION = '1.
|
3
|
-
end
|
1
|
+
module OwO
|
2
|
+
VERSION = '1.3.0.1'.freeze
|
3
|
+
end
|
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.
|
4
|
+
version: 1.3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Snazzah
|
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
82
|
version: '0'
|
83
83
|
requirements: []
|
84
84
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.
|
85
|
+
rubygems_version: 2.5.1
|
86
86
|
signing_key:
|
87
87
|
specification_version: 4
|
88
88
|
summary: A gem that utilizes the OwO API.
|