class BrainyQuote::CLI

Public Instance Methods

call() click to toggle source
# File lib/brainyquote/cli.rb, line 2
def call
  starter
end

Private Instance Methods

continue_or_exit() click to toggle source
# File lib/brainyquote/cli.rb, line 35
def continue_or_exit
  until @input == 'exit'
    main_controller
  end
  skip_line
  puts "Enough quotes for now. Goodbye!"
  exit
end
decide_to_retry() click to toggle source
# File lib/brainyquote/cli.rb, line 126
def decide_to_retry
  get_input

  if @input == 'y'
    retrieve_quote
    format_quote
    display_instructions_for('retry', 'exit')
    decide_to_retry
  elsif @input == 'n'
    topic_controller
  end
end
display_instructions_for(*options) click to toggle source
# File lib/brainyquote/cli.rb, line 19
def display_instructions_for(*options)
  puts "Welcome to BrainyQuote." if options.include?('intro')
  puts "That is not a valid option." if options.include?('invalid')
  puts "Hit 'Enter' to see available topics." if options.include?('topics')
  if options.include?('retry')
    puts "Would you like another quote from this topic? (y/n)"
    puts "Hitting 'n' will display the topic list."
  end
  puts "Enter a number from the list to select a topic." if options.include?('choose')
  puts "Type 'exit' to leave." if options.include?('exit')
end
format_quote() click to toggle source
# File lib/brainyquote/cli.rb, line 90
def format_quote
  skip_line
  puts "~#{@quote.topic_name.split.map(&:capitalize).join(' ')}~"
  skip_line(2)
  puts word_wrap(@quote.text, line_width: 75)
  skip_line
  puts "  - #{@quote.author}"
  skip_line(4)
end
format_topics() click to toggle source
# File lib/brainyquote/cli.rb, line 76
def format_topics
  topics_string = ""
  @topics.each_with_index do |topic, i|
    cell =  "#{i + 1}) #{topic}"
    x = (25 - cell.length)
    x.times{ cell << " " }
    topics_string << cell
  end

  rows = topics_string.scan(/.{1,75}/)
  rows.each {|row| puts row}
  skip_line
end
get_input() click to toggle source
# File lib/brainyquote/cli.rb, line 31
def get_input
  @input = gets.strip.downcase
end
get_topics() click to toggle source
# File lib/brainyquote/cli.rb, line 113
def get_topics
  @topics = BrainyQuote::Quote.topics
end
main_controller() click to toggle source
# File lib/brainyquote/cli.rb, line 44
def main_controller
  if @filter == 'main'
    if @input == ""
      topic_controller
    else
      display_instructions_for('invalid', 'topics', 'exit')
      get_input
    end
  elsif @filter == 'topic'
    if (1..122) === @input.to_i
      translate_input_to_topic_name
      retrieve_quote
      format_quote
      display_instructions_for('retry', 'exit')
      decide_to_retry
    else
      display_instructions_for('invalid', 'choose', 'exit')
      get_input
      continue_or_exit
    end
  end
end
retrieve_quote() click to toggle source
# File lib/brainyquote/cli.rb, line 122
def retrieve_quote
  @quote = BrainyQuote::Quote.quote_about(@topic_name)
end
skip_line(n = 1) click to toggle source
# File lib/brainyquote/cli.rb, line 109
def skip_line(n = 1)
  n.times{ puts "" }
end
starter() click to toggle source
# File lib/brainyquote/cli.rb, line 10
def starter
  @filter = 'main'
  puts ""
  display_instructions_for('intro', 'topics', 'exit')
  skip_line
  get_input
  continue_or_exit
end
topic_controller() click to toggle source
# File lib/brainyquote/cli.rb, line 67
def topic_controller
  @filter = 'topic'
  get_topics
  format_topics
  display_instructions_for('choose', 'exit')
  get_input
  continue_or_exit
end
translate_input_to_topic_name() click to toggle source
# File lib/brainyquote/cli.rb, line 118
def translate_input_to_topic_name
  @topic_name = @topics[@input.to_i - 1].downcase
end
word_wrap(text, options = {}) click to toggle source

ActionView's WordWrap

# File lib/brainyquote/cli.rb, line 101
 def word_wrap(text, options = {})
  line_width = options.fetch(:line_width, 80)

  text.split("\n").collect! do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end