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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/lib/owo.rb +80 -71
  3. data/lib/owo/api.rb +65 -60
  4. data/lib/owo/err.rb +68 -68
  5. data/lib/owo/ver.rb +3 -3
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1fe203eb93b393250b771e3943189ec1c2591ac2
4
- data.tar.gz: 12f07ca21399f391a2096b2f00efbadfcfdf796d
3
+ metadata.gz: c622fa50671d197b6eb0c96859f19024ebe4ca6d
4
+ data.tar.gz: b6681af8a15fa995018d287884baf99c64a403af
5
5
  SHA512:
6
- metadata.gz: 8bef383c38dec27b3ca9c404846aef436fc2a7524a080a3fbc8c62a81ccf99e392558ea7f4b8b9d398662463fcfd468a1039105c1102532aa294a6a30a91b55c
7
- data.tar.gz: dc42359d52c0e5b54a5d0918759b3edf8171c9b4520c4340268aca6fd49a57ad03a29b0baf0936c11e2be6b1f69abfb550a69cf3944a867199de3cb502a275ed
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
- # 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 shorten
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
+ 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
@@ -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 = '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
+ 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
@@ -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
@@ -1,3 +1,3 @@
1
- module OwO
2
- VERSION = '1.2.2'.freeze
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.2.2
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.4.5.1
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.