owo 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/lib/owo.rb +71 -71
  3. data/lib/owo/api.rb +60 -71
  4. data/lib/owo/err.rb +68 -61
  5. data/lib/owo/ver.rb +3 -0
  6. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a7ea7249271b04538e746273c02f31900772f4db
4
- data.tar.gz: 13d193c9b9da46c37039da7b19cf83bb5dbf1696
3
+ metadata.gz: ca1e08a5e54f9dd371a76883c245b11e827011cf
4
+ data.tar.gz: 225324af52d912ea4735cc8d840b3f377f3c2680
5
5
  SHA512:
6
- metadata.gz: 3ed821d013f84b4b9dbfa96fbf69177087d024521d5dc6263ccee4b6e2c3b61df0183aad88d0c5d24421deaae31a43fdc21be88b89a79289aaeb1c249d714f59
7
- data.tar.gz: c9201ecd0b74a21b14717a8cc3386ebdbc7bb278033a4d3c7a1675ceba6c2ece70309f8044e8b0bfc6d83c3db4b50a18af36afde3102f227c6c5a3be9a7494a3
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
- # 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
- if not files.empty?
34
- if files.is_a?(String)
35
- files = [files]
36
- end
37
- files = files.map do |x|
38
- if x.is_a?(String)
39
- File.new(File.absolute_path(x), 'rb')
40
- else
41
- x
42
- end
43
- end
44
- result = OwO::API.upload(opts, files)
45
- result.shift
46
- result = result['files'].map do |x|
47
- "https://#{@upload_url}/#{x['url']}"
48
- end
49
- result = result[0] unless ogfiles.is_a?(Array)
50
- result
51
- else
52
- raise OwO::Err::NoContent, 'Theres no files provided!'
53
- end
54
- end
55
-
56
- # Shortens a link with OwO.
57
- # @param urls [String, Array<String>] URLs to sharten
58
- # @return [String, Array<String>] Returns the URLs of the shortened URLs
59
- def shorten(urls)
60
- if not urls.length == 0
61
- if urls.is_a?(Array)
62
- urls.map { |x| OwO::API.shorten(opts(), x) }
63
- else
64
- OwO::API.shorten(opts, urls)
65
- end
66
- else
67
- raise OwO::Err::NoContent, 'Theres no URL provided!'
68
- end
69
- end
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
@@ -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 = "https://api.awau.moe/upload/pomf"
10
- SHORTEN_URL = "https://api.awau.moe/shorten/polr"
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
- if json['description'] == 'too many files'
27
- raise OwO::Err::TooManyFiles, 'You requested too many files!'
28
- end
29
- else
30
- if raw == 'invalid URL'
31
- raise OwO::Err::BadURL, 'Your URL is invalid!'
32
- end
33
- end
34
- err = if json.is_a?(Hash)
35
- json['description']
36
- else
37
- raw
38
- end
39
-
40
- raise err
41
- rescue RestClient::InternalServerError
42
- raise OwO::Err::ServerFail, 'Server Error!'
43
- rescue RuntimeError => e
44
- raise e
45
- end
46
-
47
- def parse_json(raw)
48
- JSON.parse(raw)
49
- rescue JSON::ParserError
50
- raw
51
- end
52
-
53
- def upload(opts, files)
54
- # "https://#{opts['u']}/" +
55
- request(
56
- :post,
57
- "#{UPLOAD_URL}?key=#{opts['t']}",
58
- "files".to_sym() => files
59
- )
60
- end
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
@@ -1,61 +1,68 @@
1
- module OwO
2
- # Custom errors raised in various places
3
- module Err
4
-
5
- # Raised when a token is invalid or incorrect.
6
- class BadToken < RuntimeError
7
- # Default message for this exception
8
- def message
9
- 'Token invalid'
10
- end
11
- end
12
-
13
- # Raised when too many files were uploaded.
14
- class TooManyFiles < RuntimeError
15
- # Default message for this exception
16
- def message
17
- 'Too many files requested!'
18
- end
19
- end
20
-
21
- # Raised when no files were sent.
22
- class NoContent < RuntimeError
23
- # Default message for this exception
24
- def message
25
- 'No content found!'
26
- end
27
- end
28
-
29
- # Raised when requested URL is bad.
30
- class BadURL < RuntimeError
31
- # Default message for this exception
32
- def message
33
- 'Your URL is invalid!'
34
- end
35
- end
36
-
37
- # Raised when requested file(s) have too much bytes.
38
- class TooLarge < RuntimeError
39
- # Default message for this exception
40
- def message
41
- 'File(s) are too large!'
42
- end
43
- end
44
-
45
- # Raised when a server error occurs
46
- class ServerFail < RuntimeError
47
- # Default message for this exception
48
- def message
49
- 'Server tried to do a thing and borked!'
50
- end
51
- end
52
-
53
- # Raised when the error is unhandled
54
- class Unhandled < RuntimeError
55
- # Default message for this exception
56
- def message
57
- 'Unhandled'
58
- end
59
- end
60
- end
61
- 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
@@ -0,0 +1,3 @@
1
+ module OwO
2
+ VERSION = '1.2.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.2.0
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