Method: MyAsk::Prompt#initialize

Defined in:
lib/myask/prompt.rb

#initialize(c) ⇒ Prompt

Returns a new instance of Prompt.



4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/myask/prompt.rb', line 4

def initialize(c)
  c.desc "Skip Follow-Up Question"
  c.switch [:no_followup]

  c.desc "Project ID"
  c.flag [:project]

  c.desc "Prompt to send"
  c.flag [:p, :prompt]

  c.desc "Question IDs to include in prompt context"
  c.flag [:c, :context_ids], multiple: true

  c.desc "Path to source file(s)"
  c.flag [:F, :input_file], multiple: true

  c.action do |global_options, options, args|
    helper = MyAsk::PromptHelper.new(global_options, options, args)

    config = global_options
    auth_token = config[:api_key]
    project = options[:project] || config[:project]

    content = helper.get_initial_question
    context_ids = options[:"context_ids"]

    loop do
      begin
        question_id = helper.submit_question(config[:api_host], auth_token, project, content, context_ids)
        abort if question_id.nil?

        context_ids << question_id

        response = helper.poll_for_response(config[:api_host], auth_token, question_id)

        if global_options[:json]
          puts "\n\n#{JSON.dump({response: response})}\n\n"
        else
          puts "\n\n#{response}\n\n"
        end

        break if options[:no_followup]

        prompt = TTY::Prompt.new
        is_follow_up = prompt.yes?("Would you like to ask a follow-up question?", default: false)

        break unless is_follow_up

        follow_up_question = prompt.ask("Enter your follow-up question:").strip
        content = follow_up_question
      rescue => e
        abort "Error: #{e}"
      end
    end
  end
  c
end