Class: RubynCode::IDE::Handlers::PlanInterviewAnswerHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyn_code/ide/handlers/plan_interview_answer_handler.rb

Overview

Handles “plan/interview/answer” — feeds the user’s answer into the active InterviewSession, emits the next question OR the final plan via notifications, and returns an empty ack to the extension.

Constant Summary collapse

SESSION_NOT_FOUND_CODE =
-32_011
INVALID_INTERVIEW_CODE =
-32_010

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ PlanInterviewAnswerHandler

Returns a new instance of PlanInterviewAnswerHandler.



13
14
15
# File 'lib/rubyn_code/ide/handlers/plan_interview_answer_handler.rb', line 13

def initialize(server)
  @server = server
end

Instance Method Details

#call(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubyn_code/ide/handlers/plan_interview_answer_handler.rb', line 17

def call(params)
  session_id  = params['sessionId'].to_s
  question_id = params['questionId'].to_s
  answer      = params['answer'].to_s

  session = @server.lookup_interview_session(session_id)
  unless session
    raise Protocol::JsonRpcError.new(SESSION_NOT_FOUND_CODE,
                                      "Unknown interview session: #{session_id}")
  end

  outcome = session.answer(question_id, answer)
  emit_outcome(session, outcome)
  {}
rescue Megaplan::InterviewSession::InvalidAnswerError => e
  raise Protocol::JsonRpcError.new(Protocol::INVALID_PARAMS, e.message)
rescue Megaplan::InterviewSession::MalformedResponseError,
       Megaplan::PlanProposer::InvalidProposalError => e
  warn "[PlanInterviewAnswerHandler] interview failed: #{e.message}"
  @server.notify('plan/interview/error', {
    'sessionId' => params['sessionId'],
    'message' => e.message
  })
  @server.drop_interview_session(params['sessionId'].to_s)
  raise Protocol::JsonRpcError.new(INVALID_INTERVIEW_CODE, e.message)
end