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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/radarcordrb.rb +120 -104
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cee9ff34c9b47e7e1746ad9b0e9d0923af629eab2e1520e88ca80ec6edaa379
4
- data.tar.gz: b606a508af4cfb19c9b5d147408b7035853a7fe2ca11e2f4d6c73c52e6de64cf
3
+ metadata.gz: 68d5ff0bcf45fc46911ba2d53d0f84161dd06a71cef2ebed6f64eb57e0aebb3e
4
+ data.tar.gz: 56a9846bf0ea2c639a27c3aa8bdb6615184a6381e094f8894bbd1313c72e9f63
5
5
  SHA512:
6
- metadata.gz: 6a479e5c501ee2c3943e630b7ebb1530e08371f7c25b0d76a5ce4139741c8f97d317eb5ff4f9e0b7e4ea2cddefa2eef31ffa1922bf1c57801ccb2a8143612c3b
7
- data.tar.gz: 1fbb25c672505837a9e74386781ed266f1dacb98b1321d7686d0f2444769025814c642715087881e71ae8f73ff3595a2365859552244c178681046c94564261a
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://gitlab.com/roxannewolf/radarcordrb'
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.0'.freeze
8
- class APIError < StandardError; end
9
-
10
- class Client
11
- BASE_URL = 'https://api.radarcord.net'.freeze
12
- UPTIMEPING_RATELIMIT_SECONDS = 120
13
-
14
- def initialize(api_key:)
15
- @api_key = api_key.to_s
16
- @last_uptime_post_time = nil
17
- raise ArgumentError, '[Radarcordrb] API key is not provided' if @api_key.empty?
18
- end
19
-
20
- def poststats(bot_id, guilds:, shards: nil)
21
- raise ArgumentError, '[Radarcordrb] Guilds count is required.' unless guilds
22
- path = "/bot/#{bot_id}/stats"
23
- body = { guilds: guilds }
24
- body[:shards] = shards if shards
25
- _authorized_post(path, body)
26
- end
27
-
28
- def postuptime(bot_id)
29
- if @last_uptime_post_time && (Time.now - @last_uptime_post_time) < UPTIMEPING_RATELIMIT_SECONDS
30
- seconds_to_wait = (UPTIMEPING_RATELIMIT_SECONDS - (Time.now - @last_uptime_post_time)).ceil
31
- puts "[Radarcordrb] WARNING: Uptime post was recently called. Skipping request to respect the 2-minute rate limit (wait #{seconds_to_wait}s)."
32
- return true
33
- end
34
-
35
- path = "/bot/#{bot_id}/uptime"
36
- response = _authorized_post(path, {})
37
- @last_uptime_post_time = Time.now
38
- response
39
- end
40
-
41
- def post_commands(bot_id, commands)
42
- path = "/bot/#{bot_id}/commands"
43
- body = { commands: commands }
44
- _authorized_post(path, body)
45
- end
46
-
47
- def get_botinfo(id_or_vanity)
48
- path = "/bot/#{id_or_vanity}/"
49
- _get(path)
50
- end
51
-
52
- def get_userinfo(user_id)
53
- path = "/user/#{user_id}"
54
- _get(path)
55
- end
56
-
57
- def has_voted(user_id, bot_id)
58
- path = "/hasvoted/#{user_id}/#{bot_id}"
59
- _get(path)
60
- end
61
-
62
- def get_last_voted(user_id, bot_id)
63
- path = "/lastvoted/#{user_id}/#{bot_id}"
64
- _get(path)
65
- end
66
-
67
- private
68
-
69
- def _authorized_post(path, body)
70
- uri = URI.parse("#{BASE_URL}#{path}")
71
- http = Net::HTTP.new(uri.host, uri.port)
72
- http.use_ssl = true
73
- request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
74
- auth_header = "Bearer #{@api_key}"
75
- request['Authorization'] = auth_header
76
- request.body = body.to_json
77
- _send_request(http, request)
78
- end
79
-
80
- def _get(path)
81
- uri = URI.parse("#{BASE_URL}#{path}")
82
- http = Net::HTTP.new(uri.host, uri.port)
83
- http.use_ssl = true
84
- request = Net::HTTP::Get.new(uri.request_uri)
85
- _send_request(http, request)
86
- end
87
-
88
- def _send_request(http, request)
89
- response = http.request(request)
90
- if response.is_a?(Net::HTTPSuccess)
91
- return JSON.parse(response.body)
92
- end
93
-
94
- error_message = "#{response.code} #{response.message}"
95
- begin
96
- error_details = JSON.parse(response.body)
97
- error_message += ": #{error_details['message'] || response.body}"
98
- rescue JSON::ParserError
99
- error_message += ": #{response.body}" unless response.body.empty?
100
- end
101
-
102
- raise Radarcord::Error, "[Radarcordrb] radarcord API request failed for #{request.path}: #{error_message}"
103
- end
104
- end
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.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-02 00:00:00.000000000 Z
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.6.9
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: []