discord_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 87a86b36af7da2e67a7cf1b5ba61abbb3cb3d44132c8b3d4ecc63871090d40a0
4
+ data.tar.gz: aee120202417f73ddbcfba36568d15fe3146bd54776c2ee1e95ca4ca880544e8
5
+ SHA512:
6
+ metadata.gz: 3b9df969d355386214e50684381f991aecdf1e71d02ffe90a0496b78a2d731b5e433ebfdf8cc136602e4e3214f182cd49744a087973736fb3ca2778c43f91e5a
7
+ data.tar.gz: 0b04d785a62f2ce440077cb448898816028bd0cc52ecb0e0287306c0f4520b43f9053f6ed07175c29adaa6f347e95aa14f0d99c0acad9de42ef233501f270b4f
data/.env.example ADDED
@@ -0,0 +1,2 @@
1
+ DISCORD_USER_ACCESS_TOKEN=
2
+ DISCORD_BOT_ACCESS_TOKEN=
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-30
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Dean Perry
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Discord API
2
+
3
+ This is a Ruby wrapper for the Discord API. It is currently in development and is not ready for use.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "discord_api"
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Authentication
16
+
17
+ Firstly you'll need to set either a user token or bot token. Some endpoints are only accssible with a bot token.
18
+
19
+ ```ruby
20
+ # Set a user token
21
+ @client = Discord::Client.new(user_access_token: "")
22
+
23
+ # Or set a bot token
24
+ @client = Discord::Client.new(bot_access_token: "")
25
+ ```
26
+
27
+ ### Users
28
+
29
+ ```ruby
30
+ # Get the current user
31
+ @client.users.me
32
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,40 @@
1
+ module Discord
2
+ class Client
3
+ BASE_URL = "https://discord.com/api/v10"
4
+
5
+ attr_reader :user_access_token, :bot_access_token, :adapter
6
+
7
+ def initialize(user_access_token: nil, bot_access_token: nil, adapter: Faraday.default_adapter)
8
+ @user_access_token = user_access_token
9
+ @bot_access_token = bot_access_token
10
+ @adapter = adapter
11
+ end
12
+
13
+ def users
14
+ UsersResource.new(self)
15
+ end
16
+
17
+ def connection
18
+ @connection ||= Faraday.new(BASE_URL) do |conn|
19
+ if user_access_token
20
+ conn.request :authorization, :Bearer, user_access_token
21
+ elsif bot_access_token
22
+ conn.request :authorization, :Bot, bot_access_token
23
+ else
24
+ raise Error, "You must provide a user or bot access token."
25
+ end
26
+
27
+ conn.headers = {
28
+ "User-Agent" => "discord_api/v#{VERSION} (github.com/deanpcmad/discord_api)"
29
+ }
30
+
31
+ conn.request :json
32
+
33
+ conn.response :json, content_type: "application/json"
34
+
35
+ conn.adapter adapter, @stubs
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,21 @@
1
+ module Discord
2
+ class Collection
3
+ attr_reader :data, :total, :cursor
4
+
5
+ def self.from_response(response, type:)
6
+ body = response.body
7
+
8
+ new(
9
+ data: body["data"].map { |attrs| type.new(attrs) },
10
+ total: body["data"].count,
11
+ cursor: body.dig("pagination", "cursor")
12
+ )
13
+ end
14
+
15
+ def initialize(data:, total:, cursor:)
16
+ @data = data
17
+ @total = total
18
+ @cursor = cursor.nil? ? nil : cursor
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module Discord
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module Discord
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [key, to_ostruct(val)] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,4 @@
1
+ module Discord
2
+ class User < Object
3
+ end
4
+ end
@@ -0,0 +1,58 @@
1
+ module Discord
2
+ class Resource
3
+ attr_reader :client
4
+
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ private
10
+
11
+ def get_request(url, params: {}, headers: {})
12
+ handle_response client.connection.get(url, params, headers)
13
+ end
14
+
15
+ def post_request(url, body:, headers: {})
16
+ handle_response client.connection.post(url, body, headers)
17
+ end
18
+
19
+ def patch_request(url, body:, headers: {})
20
+ handle_response client.connection.patch(url, body, headers)
21
+ end
22
+
23
+ def put_request(url, body:, headers: {})
24
+ handle_response client.connection.put(url, body, headers)
25
+ end
26
+
27
+ def delete_request(url, params: {}, headers: {})
28
+ handle_response client.connection.delete(url, params, headers)
29
+ end
30
+
31
+ def handle_response(response)
32
+ case response.status
33
+ when 400
34
+ raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
+ when 401
36
+ raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'"
37
+ when 403
38
+ raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]}'"
39
+ when 404
40
+ raise Error, "Error 404: No results were found for your request. '#{response.body["error"]}'"
41
+ when 409
42
+ raise Error, "Error 409: Your request was a conflict. '#{response.body["message"]}'"
43
+ when 422
44
+ raise Error, "Error 422: Unprocessable Entity. '#{response.body["message"]}"
45
+ when 429
46
+ raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
47
+ when 500
48
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
49
+ when 503
50
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
51
+ when 204
52
+ return true
53
+ end
54
+
55
+ response
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,10 @@
1
+ module Discord
2
+ class UsersResource < Resource
3
+
4
+ # Get the currently authenticated user
5
+ def me
6
+ User.new get_request("users/@me").body
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Discord
4
+ VERSION = "0.0.1"
5
+ end
data/lib/discord.rb ADDED
@@ -0,0 +1,17 @@
1
+ require "faraday"
2
+ require "json"
3
+ require "discord/version"
4
+
5
+ module Discord
6
+
7
+ autoload :Client, "discord/client"
8
+ autoload :Collection, "discord/collection"
9
+ autoload :Error, "discord/error"
10
+ autoload :Resource, "discord/resource"
11
+ autoload :Object, "discord/object"
12
+
13
+ autoload :UsersResource, "discord/resources/users"
14
+
15
+ autoload :User, "discord/objects/user"
16
+
17
+ end
@@ -0,0 +1 @@
1
+ require "discord"
@@ -0,0 +1,6 @@
1
+ module Discord
2
+ module Api
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: discord_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description:
28
+ email:
29
+ - deanperry@fastmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".env.example"
35
+ - CHANGELOG.md
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/discord.rb
40
+ - lib/discord/client.rb
41
+ - lib/discord/collection.rb
42
+ - lib/discord/error.rb
43
+ - lib/discord/object.rb
44
+ - lib/discord/objects/user.rb
45
+ - lib/discord/resource.rb
46
+ - lib/discord/resources/users.rb
47
+ - lib/discord/version.rb
48
+ - lib/discord_api.rb
49
+ - sig/discord/api.rbs
50
+ homepage:
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.6.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.10
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A Ruby wrapper for the Discord API
73
+ test_files: []