owo 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/lib/owo.rb +64 -0
  3. data/lib/owo/api.rb +69 -0
  4. data/lib/owo/err.rb +61 -0
  5. metadata +88 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dadc9074b3638029d6ddcb693d4ba6dccbafaed9
4
+ data.tar.gz: 472b5659f8d11ae77500aa0e18afb5f4b7bfc00f
5
+ SHA512:
6
+ metadata.gz: ab721cb20581d3889a65af077540d1e78db19a88cd571f108664abacd7d4355ec291462082dce896fc4e91b6d8dfab53044c08ec107acbf158a44135b67c7c43
7
+ data.tar.gz: 3429c847244cc54558d85843ba16f4fb9477b24bed44b3966e640a41e448aeab3c2b74d3ff29a44e40792ede4ddbba9fe49b4e31d34ac4c4c7dec509c1d47c43
@@ -0,0 +1,64 @@
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
30
+ # @return [String, Array<String>] Returns the URLs of the uploaded files
31
+ def upload(files)
32
+ if not files.length == 0
33
+ was_string = false
34
+ if files.is_a?(String)
35
+ files = [ files ]
36
+ was_string = true
37
+ end
38
+ files.map { |x| File.new(x, 'rb') }
39
+ result = OwO::API.upload(opts(), files)
40
+ if was_string
41
+ result = result[0]
42
+ end
43
+ return result
44
+ else
45
+ raise OwO::Err::NoContent, "Theres no files provided!"
46
+ end
47
+ end
48
+
49
+ # Shortens a link with OwO.
50
+ # @param urls [String, Array<String>] URLs to sharten
51
+ # @return [String, Array<String>] Returns the URLs of the shortened URLs
52
+ def shorten(urls)
53
+ if not urls.length == 0
54
+ if urls.is_a?(Array)
55
+ return urls.map { |x| OwO::API.shorten(opts(), x) }
56
+ else
57
+ return OwO::API.shorten(opts(), urls)
58
+ end
59
+ else
60
+ raise OwO::Err::NoContent, "Theres no URL provided!"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,69 @@
1
+ require 'owo/err'
2
+ require 'uri'
3
+ require 'rest-client'
4
+
5
+ module OwO
6
+ # List of methods representing endpoints in OwO's API
7
+ module API
8
+ UPLOAD_URL = "https://api.awau.moe/upload/pomf"
9
+ SHORTEN_URL = "https://api.awau.moe/shorten/polr"
10
+
11
+ module_function
12
+
13
+ def request(type, *attributes)
14
+ raw = RestClient.send(type, *attributes, :'User-Agent' => "OwO.rb (https://github.com/whats-this/owo.rb)", :'Content-Type' => "multipart/form-data; boundary=XXX")
15
+ json = parse_json(raw);
16
+ return json
17
+ rescue RestClient::RequestEntityTooLarge
18
+ raise OwO::Err::TooLarge, "Requested files are too large!"
19
+ rescue RestClient::Unauthorized
20
+ raise OwO::Err::BadToken, "Token is invalid!"
21
+ rescue RestClient::BadRequest => e
22
+ raw = e.response
23
+ json = parse_json(raw)
24
+ if json.is_a?(Hash)
25
+ if json['description'] == 'too many files'
26
+ raise OwO::Err::TooManyFiles, "You requested too many files!"
27
+ end
28
+ else
29
+ if raw == 'invalid URL'
30
+ raise OwO::Err::BadURL, "Your URL is invalid!"
31
+ end
32
+ end
33
+ err = if json.is_a?(Hash)
34
+ json['description']
35
+ else
36
+ raw
37
+ end
38
+ raise err
39
+ rescue RestClient::InternalServerError
40
+ raise OwO::Err::BadToken, "Server Error!"
41
+ rescue Exception => e
42
+ raise e
43
+ end
44
+
45
+ def parse_json(raw)
46
+ JSON.parse(raw)
47
+ rescue JSON::ParserError
48
+ raw
49
+ end
50
+
51
+ def upload(opts, files)
52
+ # "https://#{opts['u']}/" +
53
+ request(
54
+ :post,
55
+ "#{UPLOAD_URL}?key=#{opts['t']}",
56
+ {:'files[]' => files}
57
+ )
58
+ end
59
+
60
+ def shorten(opts, url)
61
+ request(
62
+ :get,
63
+ "#{SHORTEN_URL}?action=shorten&url=#{URI.escape(url)}&key=#{opts['t']}"
64
+ ).sub(/awau\.moe/, opts['s'])
65
+ end
66
+ end
67
+ end
68
+
69
+ # gem build owo.gemspec && gem install owo-1.0.0.gem && ruby test/upload.rb
@@ -0,0 +1,61 @@
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
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: owo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Snazzah
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem that utilizes the OwO API.
56
+ email: suggesttosnazzy@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/owo.rb
62
+ - lib/owo/api.rb
63
+ - lib/owo/err.rb
64
+ homepage: https://github.com/whats-this/owo.rb
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A gem that utilizes the OwO API.
88
+ test_files: []