class Wordwise::CLI

Implements interface for user.

Constants

WIDTH

Public Class Methods

question_array() click to toggle source

Array is return value to be used in Question.

# File lib/cli.rb, line 107
def self.question_array
  origin = Wordwise::Scraper.scrape_entry_pages
  [@@question_words, @@question_defs, origin]
end
question_words() click to toggle source
# File lib/cli.rb, line 102
def self.question_words
  @@question_words
end

Public Instance Methods

ask_c_or_e() click to toggle source

Tells user questions are exhausted in category and gives options.

# File lib/cli.rb, line 186
def ask_c_or_e
  puts 'Sorry, no more questions available in category.'
  puts "Change category: 'c'. Exit: 'e'.\n"
  case_letter
end
ask_letter() click to toggle source

Display menu after user has answered a question.

# File lib/cli.rb, line 156
def ask_letter
  puts "\nWord origin: 'o'. Next question: 'n'. Change category: 'c'. Exit: 'e'.\n"
  case_letter
end
ask_list() click to toggle source

Asks user for category selection.

# File lib/cli.rb, line 43
def ask_list
  #
  puts "Please enter the number of the category you want:"
  input = gets.strip.to_i
  size = Wordwise::Scraper.scrape_word_lists.size
  if input.between?(1, size)
    @words_defs_ary = Wordwise::Scraper.scrape_word_list(input - 1)
    check_remaining
  else
    ask_list
  end
end
ask_n_c_or_e() click to toggle source

Displays menu after origin has been displayed.

# File lib/cli.rb, line 180
def ask_n_c_or_e
  puts "Next question: 'n'. Change category: 'c'. Exit game: 'e'\n"
  case_letter
end
ask_no() click to toggle source

Get definition number from user.

# File lib/cli.rb, line 123
def ask_no
  puts 'Please enter a number 1-4:'
  input = gets.strip
  case input
  when '1'
    @question.defs[0] == @question.def ? correct : incorrect
  when '2'
    @question.defs[1] == @question.def ? correct : incorrect
  when '3'
    @question.defs[2] == @question.def ? correct : incorrect
  when '4'
    @question.defs[3] == @question.def ? correct : incorrect
  else
    ask_no
  end
end
case_letter() click to toggle source

Initiates action based on user input.

# File lib/cli.rb, line 162
def case_letter
  input = gets.strip.downcase
  case input
  when 'o'
    puts "\n#{@question.origin}\n\n"
    ask_n_c_or_e
  when 'n'
    check_remaining
  when 'c'
    display_list
  when 'e'
    goodbye
  else
    ask_letter
  end
end
case_y_n() click to toggle source

Bids user farewell and end play.

# File lib/cli.rb, line 199
def case_y_n
  puts "Yes: 'y'. No: 'n'.\n"
  input = gets.strip.downcase
  case input
  when 'y'
    display_review
  when 'n'
    exit
  else
    case_y_n
  end
end
check_remaining() click to toggle source

Checks if there are enough unused words and definitions to form question. Minumum size is set at 5 because sample method in sample_words_defs is not called on first item in array.

# File lib/cli.rb, line 59
def check_remaining
  if @words_defs_ary.size >= 5
    sample_words_defs
  else
    ask_c_or_e
  end
end
correct() click to toggle source

Tell user they answered correctly and ask how they want to proceed.

# File lib/cli.rb, line 141
def correct
  puts "\nCORRECT!"
  ask_letter
end
display_list() click to toggle source

Presents list of categories.

# File lib/cli.rb, line 24
def display_list 
  @list.each_with_index do |l, i|
    puts wrap_indent("#{i + 1}) #{l}")
    puts ''
  end
  ask_list
end
display_question() click to toggle source

Presents a question to the user.

# File lib/cli.rb, line 113
def display_question
  puts "\nWhat does '#{@question.word}' mean?\n\n"
  (0..3).each do |i|
    puts wrap_indent("#{i + 1}) #{@question.defs[i]}")
    puts ''
  end
  @words_defs_ary.delete_if { |wd| wd == @question_words_defs[0] }
end
display_review() click to toggle source

Presents main words, their definitions and origins from session.

# File lib/cli.rb, line 213
def display_review
  Wordwise::Question.all.each_index do |i|
    question = Wordwise::Question.all[i]
    puts "\n'#{question.word}':"
    puts wrap_indent("#{question.def.capitalize}.")
    puts wrap_indent("Origin: #{question.origin}")
  end
  puts ''
  exit
end
exit() click to toggle source
# File lib/cli.rb, line 224
def exit
  puts "\nPlease come again!\n\n"
end
goodbye() click to toggle source
# File lib/cli.rb, line 192
def goodbye
  puts wrap("\nThanks for playing WordWise! Would you like to review the words and definitions from your session?")
  puts ''
  case_y_n
end
incorrect() click to toggle source

Tells user they answered incorrectly, gives correct answer, and asks how to proceed.

# File lib/cli.rb, line 148
def incorrect
  puts "\nINCORRECT.\n\n"
  puts 'CORRECT ANSWER:'
  puts wrap_indent("'#{@question.def}'")
  ask_letter
end
introduction() click to toggle source

Display welcome message.

# File lib/cli.rb, line 6
def introduction
  puts ''
  puts "WELCOME TO WORDWISE!\n".center(80)
  puts "You can learn more about the words in this quiz at https://www.oxforddictionaries.com.\n\n".center(80)
  puts "Get ready to test your word wisdom....\n\n".center(80)
  puts "What category would you like to test your knowledge on?\n"
  list
end
list() click to toggle source

Checks if @lists is populated. If not, calls .scrape_word_lists and populates it. Otherwise, call display_list and pass in the list.

# File lib/cli.rb, line 17
def list
  @list ||= Wordwise::Scraper.scrape_word_lists
  puts ''
  display_list
end
play() click to toggle source

Displays question and asks for answer.

# File lib/cli.rb, line 95
def play
  puts "\nLoading question...\n\n"
  @question = Wordwise::Question.new
  display_question
  ask_no
end
sample_words_defs() click to toggle source

Samples starting at index 1 of array to avoid any column headings.

# File lib/cli.rb, line 68
def sample_words_defs
  @question_words_defs = @words_defs_ary[1..@words_defs_ary.size - 1].sample(4)
  # Prevents repetition of words in questions.
  @words_defs_ary.delete_if { |wd| wd == @question_words_defs[0] }
  set_question_words
end
set_question_defs() click to toggle source

Iterates over array of words and definitions to make separate array of definitions.

# File lib/cli.rb, line 86
def set_question_defs
  @@question_defs = []
  @question_words_defs.each_index do |i|
    @@question_defs << @question_words_defs[i][1]
  end
  play
end
set_question_words() click to toggle source

Iterates over array of words and definitions to make separate array of words.

# File lib/cli.rb, line 76
def set_question_words
  @@question_words = []
  @question_words_defs.each_index do |i|
    @@question_words << @question_words_defs[i][0]
  end
  set_question_defs
end
wrap(str) click to toggle source

Wraps strings to format them prettily within the default page width.

# File lib/cli.rb, line 33
def wrap(str)
  str.gsub(/(.{1,#{WIDTH}})(\s+|\Z)/, "\\1\n")
end
wrap_indent(str) click to toggle source

As wrap, with a five space left indent.

# File lib/cli.rb, line 38
def wrap_indent(str)
  str.gsub(/(.{1,#{WIDTH}})(\s+|\Z)/, "     \\1\n")
end