radarcordrb 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/radarcordrb.rb +120 -104
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 68d5ff0bcf45fc46911ba2d53d0f84161dd06a71cef2ebed6f64eb57e0aebb3e
|
|
4
|
+
data.tar.gz: 56a9846bf0ea2c639a27c3aa8bdb6615184a6381e094f8894bbd1313c72e9f63
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6535f2628061e8e7d39df6f8596802aaab2fdf967c4e1c6402ff7196c2e4d82fb7e6b8786b7744e6380b6f06a3a6167bbbe1925f27b34bbf5e229a7a578c8dbb
|
|
7
|
+
data.tar.gz: 396220c4de3d61e9ea80f15a596674a58e488a6be6024062fbf5f71448a21b5d3f6034c95cbb125e0e6bde820d48d60e8cddeb1d6206c9ffb22cd7abc118cc42
|
data/README.md
CHANGED
|
@@ -19,7 +19,7 @@ gem 'radarcordrb'
|
|
|
19
19
|
or add the following to your Gemfile file and run the "bundle install" command:.
|
|
20
20
|
|
|
21
21
|
```ruby
|
|
22
|
-
gem 'radarcordrb', git: 'https://
|
|
22
|
+
gem 'radarcordrb', git: 'https://codeberg.org/roxannewolf/radarcordrb'
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Usage
|
data/lib/radarcordrb.rb
CHANGED
|
@@ -1,105 +1,121 @@
|
|
|
1
|
-
require 'json'
|
|
2
|
-
require 'net/http'
|
|
3
|
-
require 'uri'
|
|
4
|
-
require 'time'
|
|
5
|
-
|
|
6
|
-
module Radarcord
|
|
7
|
-
VERSION = '1.0.
|
|
8
|
-
class APIError < StandardError; end
|
|
9
|
-
|
|
10
|
-
class Client
|
|
11
|
-
BASE_URL = 'https://
|
|
12
|
-
UPTIMEPING_RATELIMIT_SECONDS = 120
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
@
|
|
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
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'uri'
|
|
4
|
+
require 'time'
|
|
5
|
+
|
|
6
|
+
module Radarcord
|
|
7
|
+
VERSION = '1.0.1'.freeze
|
|
8
|
+
class APIError < StandardError; end
|
|
9
|
+
|
|
10
|
+
class Client
|
|
11
|
+
BASE_URL = 'https://radar.cpdv.net/api'.freeze
|
|
12
|
+
UPTIMEPING_RATELIMIT_SECONDS = 120
|
|
13
|
+
STATS_RATELIMIT_SECONDS = 3600
|
|
14
|
+
|
|
15
|
+
def initialize(api_key:)
|
|
16
|
+
@api_key = api_key.to_s
|
|
17
|
+
@last_uptime_post_time = nil
|
|
18
|
+
@last_stats_post_time = nil
|
|
19
|
+
raise ArgumentError, '[Radarcordrb]: API key is not provided' if @api_key.empty?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def poststats(bot_id, guilds:, shards: nil)
|
|
23
|
+
if @last_stats_post_time && (Time.now - @last_stats_post_time) < STATS_RATELIMIT_SECONDS
|
|
24
|
+
seconds_to_wait = (STATS_RATELIMIT_SECONDS - (Time.now - @last_stats_post_time)).ceil
|
|
25
|
+
puts "[Radarcordrb] WARNING: Stats post was recently called. (wait #{seconds_to_wait}s)."
|
|
26
|
+
return true
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
raise ArgumentError, '[Radarcordrb]: Guilds count is required.' unless guilds
|
|
30
|
+
path = "/bot/#{bot_id}/stats"
|
|
31
|
+
body = { guilds: guilds }
|
|
32
|
+
body[:shards] = shards if shards
|
|
33
|
+
_authorized_post(path, body)
|
|
34
|
+
@last_stats_post_time = Time.now
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def postuptime(bot_id)
|
|
38
|
+
if @last_uptime_post_time && (Time.now - @last_uptime_post_time) < UPTIMEPING_RATELIMIT_SECONDS
|
|
39
|
+
seconds_to_wait = (UPTIMEPING_RATELIMIT_SECONDS - (Time.now - @last_uptime_post_time)).ceil
|
|
40
|
+
puts "[Radarcordrb]: WARNING: Uptime post was recently called. Skipping request to respect the 2-minute rate limit (wait #{seconds_to_wait}s)."
|
|
41
|
+
return true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
path = "/bot/#{bot_id}/uptime"
|
|
45
|
+
response = _authorized_post(path, {})
|
|
46
|
+
@last_uptime_post_time = Time.now
|
|
47
|
+
response
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def post_commands(bot_id, commands)
|
|
51
|
+
if @last_stats_post_time && (Time.now - @last_stats_post_time) < STATS_RATELIMIT_SECONDS
|
|
52
|
+
seconds_to_wait = (STATS_RATELIMIT_SECONDS - (Time.now - @last_stats_post_time)).ceil
|
|
53
|
+
puts "[Radarcordrb]: WARNING: Stats post was recently called. (wait #{seconds_to_wait}s)."
|
|
54
|
+
return true
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
path = "/bot/#{bot_id}/commands"
|
|
58
|
+
body = { commands: commands }
|
|
59
|
+
_authorized_post(path, body)
|
|
60
|
+
@last_stats_post_time = Time.now
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def get_botinfo(id_or_vanity)
|
|
64
|
+
path = "/bot/#{id_or_vanity}/"
|
|
65
|
+
_get(path)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def get_userinfo(user_id)
|
|
69
|
+
path = "/user/#{user_id}"
|
|
70
|
+
_get(path)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def has_voted(user_id, bot_id)
|
|
74
|
+
path = "/hasvoted/#{user_id}/#{bot_id}"
|
|
75
|
+
_get(path)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def get_last_voted(user_id, bot_id)
|
|
79
|
+
path = "/lastvoted/#{user_id}/#{bot_id}"
|
|
80
|
+
_get(path)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
def _authorized_post(path, body)
|
|
86
|
+
uri = URI.parse("#{BASE_URL}#{path}")
|
|
87
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
88
|
+
http.use_ssl = true
|
|
89
|
+
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
|
|
90
|
+
auth_header = "Bearer #{@api_key}"
|
|
91
|
+
request['Authorization'] = auth_header
|
|
92
|
+
request.body = body.to_json
|
|
93
|
+
_send_request(http, request)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def _get(path)
|
|
97
|
+
uri = URI.parse("#{BASE_URL}#{path}")
|
|
98
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
99
|
+
http.use_ssl = true
|
|
100
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
|
101
|
+
_send_request(http, request)
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def _send_request(http, request)
|
|
105
|
+
response = http.request(request)
|
|
106
|
+
if response.is_a?(Net::HTTPSuccess)
|
|
107
|
+
return JSON.parse(response.body)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
error_message = "#{response.code} #{response.message}"
|
|
111
|
+
begin
|
|
112
|
+
error_details = JSON.parse(response.body)
|
|
113
|
+
error_message += ": #{error_details['message'] || response.body}"
|
|
114
|
+
rescue JSON::ParserError
|
|
115
|
+
error_message += ": #{response.body}" unless response.body.empty?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
raise Radarcord::Error, "[Radarcordrb]: radarcord API request failed for #{request.path}: #{error_message}"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
105
121
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: radarcordrb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Roxanne Studios
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 1980-01-
|
|
10
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: json
|
|
@@ -66,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
67
|
version: '0'
|
|
68
68
|
requirements: []
|
|
69
|
-
rubygems_version: 3.
|
|
69
|
+
rubygems_version: 3.7.2
|
|
70
70
|
specification_version: 4
|
|
71
71
|
summary: Ruby gem for interacting with the radarcord API.
|
|
72
72
|
test_files: []
|