Skip to content

Commit

Permalink
better error handling; bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesZh committed Dec 31, 2024
1 parent 4a55d58 commit f17a3e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1
FROM ruby:3.3.0
FROM ruby:3.4.1
WORKDIR /app
COPY . .

Expand Down
22 changes: 14 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ GEM
remote: https://rubygems.org/
specs:
any_ascii (0.3.2)
concurrent-ruby (1.3.3)
base64 (0.2.0)
concurrent-ruby (1.3.4)
discordrb (3.5.0)
discordrb-webhooks (~> 3.5.0)
ffi (>= 1.9.24)
Expand All @@ -13,13 +14,16 @@ GEM
rest-client (>= 2.0.0)
domain_name (0.6.20240107)
event_emitter (0.2.6)
ffi (1.17.0-x86_64-linux-gnu)
ffi (1.17.1-x86_64-linux-gnu)
http-accept (1.7.0)
http-cookie (1.0.6)
http-cookie (1.0.8)
domain_name (~> 0.5)
mime-types (3.5.2)
logger (1.6.4)
mime-types (3.6.0)
logger
mime-types-data (~> 3.2015)
mime-types-data (3.2024.0702)
mime-types-data (3.2024.1203)
mutex_m (0.3.0)
netrc (0.11.0)
opus-ruby (1.0.1)
ffi
Expand All @@ -31,10 +35,12 @@ GEM
netrc (~> 0.8)
ropencc (0.0.6)
ffi (~> 1.0)
sqlite3 (2.0.2-x86_64-linux-gnu)
sqlite3 (2.5.0-x86_64-linux-gnu)
websocket (1.2.11)
websocket-client-simple (0.8.0)
websocket-client-simple (0.9.0)
base64
event_emitter
mutex_m
websocket

PLATFORMS
Expand All @@ -49,4 +55,4 @@ DEPENDENCIES
sqlite3

BUNDLED WITH
2.5.16
2.6.2
6 changes: 4 additions & 2 deletions lib/lyricat/bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ def b15 lang, session_token
next '*No such chart.*' unless song&.diff[diff_id]
begin
leaderboard = HeavyLifting.get_leaderboard SESSION_TOKEN, song_id, diff_id
rescue HeavyLifting::BadUpstreamResponse
rescue HeavyLifting::BadUpstreamResponse => e
STDERR.puts e.full_message
next "*There is a problem in retrieving the leaderboard. Please contact <@#{MAINTAINER_ID}>.*"
end
text = leaderboard.map do |hash|
Expand All @@ -521,7 +522,8 @@ def b15 lang, session_token
next '*No such chart.*' unless song&.diff[diff_id]
begin
leaderboard = HeavyLifting.get_month_leaderboard SESSION_TOKEN, song_id, diff_id
rescue HeavyLifting::BadUpstreamResponse
rescue HeavyLifting::BadUpstreamResponse => e
STDERR.puts e.full_message
next "*There is a problem in retrieving the leaderboard. Please contact <@#{MAINTAINER_ID}>.*"
end
text = leaderboard.map do |hash|
Expand Down
30 changes: 15 additions & 15 deletions lib/lyricat/heavylifting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def request net, request
def get_user session_token
net = make_net
response = request net, make_get_request('/parse/sessions/me', session_token)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "Response code is #{response.code} instead of 200" unless response.code == '200'
me = JSON.parse response.body, symbolize_names: true

raise BadUpstreamResponse unless me[:user].is_a? Hash
raise BadUpstreamResponse, "The `user` field of the JSON is not a Hash but a #{me[:user].class}" unless me[:user].is_a? Hash
class_name, object_id = me[:user].values_at :className, :objectId
response = request net, make_get_request("/parse/classes/#{class_name}/#{object_id}", session_token)
hash = JSON.parse response.body, symbolize_names: true
Expand All @@ -61,9 +61,9 @@ def get_user session_token
def process_leaderboard result
columns = result.split ?$
columns.shift
raise BadUpstreamResponse unless columns.size == 5
raise BadUpstreamResponse, "The leaderboard has #{columns.size} instead of 5 columns" unless columns.size == 5
scores, nicknames, heads = columns[0..2].map { _1.split ?' }
raise BadUpstreamResponse unless scores.size == nicknames.size && nicknames.size == heads.size
raise BadUpstreamResponse, "The leaderboard format is wrong because there are #{scores.size} scores, #{nicknames.size} nicknames, and #{heads.size} heads" unless scores.size == nicknames.size && nicknames.size == heads.size
scores.map! &:to_i
heads.map! &:to_i
scores.zip(nicknames, heads).each_with_index.map { |(score, nickname, head), i| {score:, nickname:, head:, rank: i+1} }
Expand All @@ -72,56 +72,56 @@ def process_leaderboard result
def get_leaderboard session_token, song_id, diff_id
net = make_net
response = request net, make_post_request('/parse/functions/AskLeaderBoardNew', session_token, diff: diff_id, score: 0, songId: song_id)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "The response code is #{response.code} instead of 200" unless response.code == '200'

body = JSON.parse response.body, symbolize_names: true
raise BadUpstreamResponse unless body[:result].is_a? String
raise BadUpstreamResponse, "The `result` field of the JSON is not a String but a #{body[:result].class}" unless body[:result].is_a? String
process_leaderboard body[:result]
end

def get_my_leaderboard session_token, song_id, diff_id
net = make_net
response = request net, make_post_request('/parse/functions/AskMyLeaderBoard', session_token, diff: diff_id, score: 0, songId: song_id)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "The response code is #{response.code} instead of 200" unless response.code == '200'

body = JSON.parse response.body, symbolize_names: true
raise BadUpstreamResponse unless body[:result].is_a? String
raise BadUpstreamResponse, "The `result` field of the JSON is not a String but a #{body[:result].class}" unless body[:result].is_a? String
zero, score, rank, diff_id, song_id, extra = body[:result].split ','
{score: score.to_i, rank: rank.to_i, diff_id: diff_id.to_i, song_id: song_id.to_i}
end

def get_month_leaderboard session_token, song_id, diff_id
net = make_net
response = request net, make_post_request('/parse/functions/AskMonthLeaderBoardNew', session_token, diff: diff_id, score: 0, songId: song_id)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "The response code is #{response.code} instead of 200" unless response.code == '200'

body = JSON.parse response.body, symbolize_names: true
raise BadUpstreamResponse unless body[:result].is_a? String
raise BadUpstreamResponse, "The `result` field of the JSON is not a String but a #{body[:result].class}" unless body[:result].is_a? String
process_leaderboard body[:result]
end

def get_my_month_leaderboard session_token, song_id, diff_id
net = make_net
response = request net, make_post_request('/parse/functions/AskMyMonthLeaderBoard', session_token, diff: diff_id, score: 0, songId: song_id)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "The response code is #{response.code} instead of 200" unless response.code == '200'

body = JSON.parse response.body, symbolize_names: true
raise BadUpstreamResponse unless body[:result].is_a? String
raise BadUpstreamResponse, "The `result` field of the JSON is not a String but a #{body[:result].class}" unless body[:result].is_a? String
zero, score, rank, diff_id, song_id, extra = body[:result].split ','
{score: score.to_i, rank: rank.to_i, diff_id: diff_id.to_i, song_id: song_id.to_i}
end

def get_expiration_date session_token
net = make_net
response = request net, make_get_request('/parse/sessions/me', session_token)
raise BadUpstreamResponse unless response.code == '200'
raise BadUpstreamResponse, "The response code is #{response.code} instead of 200" unless response.code == '200'
me = JSON.parse response.body, symbolize_names: true
iso = me[:expiresAt]&.[] :iso
raise BadUpstreamResponse unless iso
raise BadUpstreamResponse, "The JSON does not have `expiresAt.iso` field" unless iso
begin
return DateTime.parse iso
rescue Date::Error
raise BadUpstreamResponse
raise BadUpstreamResponse, "Bad date format: #{iso}"
end
end
end
Expand Down

0 comments on commit f17a3e3

Please sign in to comment.