From 12c76b2fe4e04606b4c6573cc61ad126a68298ff Mon Sep 17 00:00:00 2001 From: Karol Date: Wed, 18 Dec 2019 17:06:31 +0100 Subject: [PATCH] reduced perceived complexity --- app/services/exercise_service/check_external.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/services/exercise_service/check_external.rb b/app/services/exercise_service/check_external.rb index 7bfb12e3..cf4a818e 100644 --- a/app/services/exercise_service/check_external.rb +++ b/app/services/exercise_service/check_external.rb @@ -13,20 +13,23 @@ module ExerciseService req.headers['Authorization'] = 'Bearer ' + @codeharbor_link.api_key req.body = {uuid: @uuid}.to_json end - response_hash = JSON.parse(response.body, symbolize_names: true) - message = if response_hash[:exercise_found] - response_hash[:update_right] ? I18n.t('exercises.export_codeharbor.check.exercise_found') : I18n.t('exercises.export_codeharbor.check.exercise_found_no_right') - else - I18n.t('exercises.export_codeharbor.check.no_exercise') - end + response_hash = JSON.parse(response.body, symbolize_names: true).slice(:exercise_found, :update_right) - {error: false, message: message}.merge(response_hash.slice(:exercise_found, :update_right)) + {error: false, message: message(response_hash[:exercise_found], response_hash[:update_right])}.merge(response_hash) rescue Faraday::Error, JSON::ParserError {error: true, message: I18n.t('exercises.export_codeharbor.error')} end private + def message(exercise_found, update_right) + if exercise_found + update_right ? I18n.t('exercises.export_codeharbor.check.exercise_found') : I18n.t('exercises.export_codeharbor.check.exercise_found_no_right') + else + I18n.t('exercises.export_codeharbor.check.no_exercise') + end + end + def connection Faraday.new(url: @codeharbor_link.check_uuid_url) do |faraday| faraday.options[:open_timeout] = 5