Class: Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/cli_trivia/cli.rb

Overview

Cli class is a controller for the app and handles the display of all information and interactions with the user.

Instance Method Summary collapse

Constructor Details

#initializeCli

Generates the categories, but not the questions on initialization and resets the @right and @wrong counters. Finally invokes call to begin the app.



6
7
8
9
10
11
# File 'lib/cli_trivia/cli.rb', line 6

def initialize
  ApiManager.generate_categories
  @right = 0
  @wrong = 0
  @prompt = TTY::Prompt.new
end

Instance Method Details

#callObject

Begins the trivia game by printing the welcome, title, and asks user to make the first choice between random and category. Uses case to determine how to proceed from call.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cli_trivia/cli.rb', line 15

def call
  system('clear')
  puts('')
  puts('CLI Trivia')
  puts('')
  user_input = @prompt.select('Choose either random, or select a category?', %w[Random Category Exit])
  case user_input
  when 'Random'
    generate_random
  when 'Category'
    display_categories
  when 'Exit'
    system('clear')
    system(exit)
  end
end

#correct?(user_input, correct_answer) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/cli_trivia/cli.rb', line 102

def correct?(user_input, correct_answer)
  user_input == correct_answer
end

#display_answer_choices(question, answer_choices, random_answers) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/cli_trivia/cli.rb', line 85

def display_answer_choices(question, answer_choices, random_answers)
  if answer_choices[:choices].length > 2
    @prompt.select("Q#{@right+@wrong+1}/10> #{question.question}", random_answers)
  else
    @prompt.select("Q#{@right+@wrong+1}/10> #{question.question}", answer_choices[:choices])
  end
end

#display_categoriesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/cli_trivia/cli.rb', line 42

def display_categories
  system('clear')
  categories = Category.all_by_name
  puts('')
  puts('CLI Trivia')
  puts('')
  puts('')
  user_input = @prompt.select('Please Select a Category (scroll for more)', categories, per_page: 15)
  display_questions_by_category(user_input)
end

#display_question(question) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/cli_trivia/cli.rb', line 106

def display_question(question)
  system('clear')
  answer_choices = generate_answers(question.correct_answer, question.incorrect_answers, question.type)
  random_answers = answer_choices[:choices]
  puts('')
  puts("CLI trivia :: #{@right} correct")
  puts('')
  user_input = display_answer_choices(question, answer_choices, random_answers)
  puts('')
  if correct?(user_input, answer_choices[:correct])
    @right += 1
    puts "That's correct.  Press Any Enter to Continue."
  else
    @wrong += 1
    puts "Wrong. #{answer_choices[:correct]} was right.  Enter to continue."
  end
  gets
end

#display_questions_by_category(user_input) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/cli_trivia/cli.rb', line 53

def display_questions_by_category(user_input)
  category = Category.find_by_name(user_input)
  question_id = category.id
  ApiManager.generate_questions_by_id(question_id)
  category.questions.each do |question|
    display_question(question)
  end
  display_result
end

#display_resultObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cli_trivia/cli.rb', line 125

def display_result
  system('clear')
  puts('')
  puts("CLI trivia :: Final score: #{@right}/10")
  puts('')
  user_input = @prompt.select('Exit or restart?', %w[Restart Exit])
  case user_input
  when 'Restart'
    Question.clear_all
    @right = 0
    @wrong = 0
    call
  when 'Exit'
    system('clear')
    system(exit)
  end
end

#generate_answers(correct, incorrect, type) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cli_trivia/cli.rb', line 63

def generate_answers(correct, incorrect, type)
  if type == 'boolean'
    answers = {
      correct: Question.format_string(correct),
      incorrect: incorrect,
      choices: %w[True False]
    }
    answers
  elsif type == 'multiple'
    random_answers = []
    incorrect.each do |wrong|
      formatted_answer = Question.format_string(wrong)
      random_answers << formatted_answer
    end
    answers = {
      correct: Question.format_string(correct),
      choices: [correct] + random_answers
    }
    answers
  end
end

#generate_randomObject

Run when user selects ‘Random’ from the main menu, calls on ApiManager class to query the api and create Question Objects. Finally, new question objects are displayed using display_question.



34
35
36
37
38
39
40
# File 'lib/cli_trivia/cli.rb', line 34

def generate_random
  ApiManager.generate_random_questions
  Question.all.each do |question|
    display_question(question)
  end
  display_result
end

#question_type(answer_choices) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/cli_trivia/cli.rb', line 93

def question_type(answer_choices)
  if answer_choices[:choices].length > 2
    random_answers = answer_choices[:choices].shuffle
  else
    random_answers = answer_choices[:choices]
  end
  random_answers
end