class ChallengeServerClient

Public Class Methods

new(hostname, port, journey_id, use_colours) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 3
def initialize(hostname, port, journey_id, use_colours)
  @base_url = "http://#{hostname}:#{port}"
  @journey_id = journey_id
  use_colours ? @accept_header = 'text/coloured' : @accept_header = 'text/not-coloured'
end

Public Instance Methods

get_available_actions() click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 13
def get_available_actions
  get('availableActions')
end
get_journey_progress() click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 9
def get_journey_progress
  get('journeyProgress')
end
get_round_description() click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 17
def get_round_description
  get('roundDescription')
end
send_action(action) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 21
def send_action(action)
  encoded_path = @journey_id.encode('utf-8')
  url = "#{@base_url}/action/#{action}/#{encoded_path}"
  response = Unirest.post(url, headers: {'Accept'=> @accept_header, 'Accept-Charset'=> 'UTF-8'})
  ensure_status_ok(response)
  response.body
end

Private Instance Methods

client_error?(response_status) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 49
def client_error?(response_status)
  response_status >= 400 && response_status < 500
end
ensure_status_ok(response) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 39
def ensure_status_ok(response)
  if client_error?(response.code)
    raise ClientErrorException, response.body
  elsif server_error?(response.code)
    raise ServerErrorException, response.body
  elsif other_error_response?(response.code)
    raise OtherCommunicationException, response.body
  end
end
get(name) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 31
def get(name)
  journey_id_utf8 = @journey_id.encode('utf-8')
  url = "#{@base_url}/#{name}/#{journey_id_utf8}"
  response = Unirest.get(url, headers: {'Accept'=> @accept_header, 'Accept-Charset'=> 'UTF-8'})
  ensure_status_ok(response)
  response.body
end
other_error_response?(response_status) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 57
def other_error_response?(response_status)
  response_status < 200 || response_status > 300
end
server_error?(response_status) click to toggle source
# File lib/tdl/runner/challenge_server_client.rb, line 53
def server_error?(response_status)
  response_status >= 500 && response_status < 600
end