Class: Radarcord::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/radarcordrb.rb

Constant Summary collapse

BASE_URL =
'https://radar.cpdv.net/api'.freeze
UPTIMEPING_RATELIMIT_SECONDS =
120
STATS_RATELIMIT_SECONDS =
3600

Instance Method Summary collapse

Constructor Details

#initialize(api_key:) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
# File 'lib/radarcordrb.rb', line 15

def initialize(api_key:)
	@api_key = api_key.to_s
	@last_uptime_post_time = nil
	@last_stats_post_time = nil
	raise ArgumentError, '[Radarcordrb]: API key is not provided' if @api_key.empty?
end

Instance Method Details

#get_botinfo(id_or_vanity) ⇒ Object



63
64
65
66
# File 'lib/radarcordrb.rb', line 63

def get_botinfo(id_or_vanity)
	path = "/bot/#{id_or_vanity}/"
	_get(path)
end

#get_last_voted(user_id, bot_id) ⇒ Object



78
79
80
81
# File 'lib/radarcordrb.rb', line 78

def get_last_voted(user_id, bot_id)
	path = "/lastvoted/#{user_id}/#{bot_id}"
	_get(path)
end

#get_userinfo(user_id) ⇒ Object



68
69
70
71
# File 'lib/radarcordrb.rb', line 68

def get_userinfo(user_id)
	path = "/user/#{user_id}"
	_get(path)
end

#has_voted(user_id, bot_id) ⇒ Object



73
74
75
76
# File 'lib/radarcordrb.rb', line 73

def has_voted(user_id, bot_id)
	path = "/hasvoted/#{user_id}/#{bot_id}"
	_get(path)
end

#post_commands(bot_id, commands) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/radarcordrb.rb', line 50

def post_commands(bot_id, commands)
	if @last_stats_post_time && (Time.now - @last_stats_post_time) < STATS_RATELIMIT_SECONDS
		seconds_to_wait = (STATS_RATELIMIT_SECONDS - (Time.now - @last_stats_post_time)).ceil
		puts "[Radarcordrb]: WARNING: Stats post was recently called. (wait #{seconds_to_wait}s)."
		return true
	end

	path = "/bot/#{bot_id}/commands"
	body = { commands: commands }
	_authorized_post(path, body)
	@last_stats_post_time = Time.now
end

#poststats(bot_id, guilds:, shards: nil) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/radarcordrb.rb', line 22

def poststats(bot_id, guilds:, shards: nil)
	if @last_stats_post_time && (Time.now - @last_stats_post_time) < STATS_RATELIMIT_SECONDS
		seconds_to_wait = (STATS_RATELIMIT_SECONDS - (Time.now - @last_stats_post_time)).ceil
		puts "[Radarcordrb] WARNING: Stats post was recently called. (wait #{seconds_to_wait}s)."
		return true
	end

	raise ArgumentError, '[Radarcordrb]: Guilds count is required.' unless guilds
	path = "/bot/#{bot_id}/stats"
	body = { guilds: guilds }
	body[:shards] = shards if shards
	_authorized_post(path, body)
	@last_stats_post_time = Time.now
end

#postuptime(bot_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/radarcordrb.rb', line 37

def postuptime(bot_id)
	if @last_uptime_post_time && (Time.now - @last_uptime_post_time) < UPTIMEPING_RATELIMIT_SECONDS
		seconds_to_wait = (UPTIMEPING_RATELIMIT_SECONDS - (Time.now - @last_uptime_post_time)).ceil
		puts "[Radarcordrb]: WARNING: Uptime post was recently called. Skipping request to respect the 2-minute rate limit (wait #{seconds_to_wait}s)."
		return true
	end

	path = "/bot/#{bot_id}/uptime"
	response = _authorized_post(path, {})
	@last_uptime_post_time = Time.now
	response
end