class Codewars::Attempt

Public Class Methods

new(client) click to toggle source
# File lib/codewars/attempt.rb, line 3
def initialize(client)
  api_key = Configuration.option('api_key')
  raise Thor::Error, 'You should set an api-key to use this command' unless api_key

  desc = Description.new
  project_id = desc.take_value_from_file(/Project ID: (.+)/, 'Project ID')
  solution_id = desc.take_value_from_file(/Solution ID: (.+)/, 'Solution ID')
  solution = read_solution_file

  attempt = client.attempt_solution(
    project_id: project_id,
    solution_id: solution_id,
    code: solution
  )

  say 'Your solution has been uploaded. Waiting for a result of tests on the server.'

  deferred_response = deferred_response(client, attempt)
  handle_deferred_response(deferred_response)
end

Private Instance Methods

deferred_response(client, attempt) click to toggle source
# File lib/codewars/attempt.rb, line 35
def deferred_response(client, attempt)
  seconds = ENV['CHECK_TIMEOUT'] || 10
  seconds.to_i.times do
    deferred_response = client.deferred_response(dmid: attempt.dmid)
    return deferred_response if deferred_response.success
    sleep 1
  end
  nil
end
handle_deferred_response(deferred_response) click to toggle source
# File lib/codewars/attempt.rb, line 45
def handle_deferred_response(deferred_response)
  if deferred_response.nil? || !deferred_response.success
    raise Thor::Error, "Can't get a result of tests on the server. Try it again."
  end

  if deferred_response.valid
    say 'The solution has passed all tests on the server.'
    say 'Type to finalize the solution: ' + set_color('codewars finalize', :blue, true)
  else
    error 'The solution has not passed tests on the server. Response:'
    raise Thor::Error, set_color(deferred_response.reason, :red)
  end
end
read_solution_file() click to toggle source
# File lib/codewars/attempt.rb, line 26
def read_solution_file
  file_name = 'solution.*'
  solution_filename = Dir.glob(file_name).first
  unless solution_filename
    raise Thor::Error, "The file '#{file_name}' has not been found in the current directory."
  end
  File.read File.expand_path(solution_filename)
end