Skip to content

Commit

Permalink
Handle jservice being down
Browse files Browse the repository at this point in the history
  • Loading branch information
gesteves committed Dec 12, 2023
1 parent b2d8246 commit 6fa1463
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/lib/jservice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Jservice
def self.get_question
uri = "http://jservice.io/api/random?count=1"
request = HTTParty.get(uri)
return if response.code != 200
response = JSON.parse(request.body, symbolize_names: true).first
question = response[:question]

Expand Down
35 changes: 20 additions & 15 deletions app/workers/start_game_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ def perform(team_id, channel_id, user_id)
return if team.blank?

q = Jservice.get_question
question = q[:question]
answer = q[:answer]
value = q[:value]
category = q.dig(:category, :title)
air_date = q[:airdate]
if q.present?
question = q[:question]
answer = q[:answer]
value = q[:value]
category = q.dig(:category, :title)
air_date = q[:airdate]

game = Game.new(question: question,
answer: answer,
value: value,
category: category,
air_date: air_date,
channel: channel_id,
team: team)
game.save!
PostGameMessageWorker.perform_async(game.id)
EndGameWorker.perform_in(ENV['CONFIG_GAME_TIME_LIMIT'].to_i.seconds, game.id)
game = Game.new(question: question,
answer: answer,
value: value,
category: category,
air_date: air_date,
channel: channel_id,
team: team)
game.save!
PostGameMessageWorker.perform_async(game.id)
EndGameWorker.perform_in(ENV['CONFIG_GAME_TIME_LIMIT'].to_i.seconds, game.id)
else
reply = "Apologies, but I can't reach jService.io at this time."
PostMessageWorker.perform_async(reply, team_id, channel_id)
end
end
end

0 comments on commit 6fa1463

Please sign in to comment.