Class: Whatsa::CLI

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

Instance Method Summary collapse

Instance Method Details

#askObject



18
19
20
21
# File 'lib/whatsa/cli.rb', line 18

def ask
  puts "What would you like to know about?"
  gets_command
end

#categories(article) ⇒ Object



117
118
119
120
121
122
# File 'lib/whatsa/cli.rb', line 117

def categories(article)
  display_sections(article)
  choice = gets_command
  section = article.choose_section(choice)
  summarize(section)
end

#display_dmb(dmb) ⇒ Object

Raises:

  • (TypeError)


59
60
61
62
63
64
65
66
67
68
# File 'lib/whatsa/cli.rb', line 59

def display_dmb(dmb)
  raise TypeError unless dmb.is_a?(Whatsa::Disambig)
  system("clear")
  stripped_title = dmb.title.gsub("(disambiguation)", "").strip
  puts "Hmmm... #{stripped_title} could mean a few different things:\n"
  dmb.descriptions.each_with_index do |kvp, i|
    puts "#{i + 1}. #{kvp[0].to_s} (#{kvp[1]})"
  end
  puts "\nPlease select a choice, either by name or number."
end

#display_sections(text) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/whatsa/cli.rb', line 70

def display_sections(text)
  text = text.article if text.is_a?(Whatsa::Section)
  system("clear")
  puts "Here are some specific subjects about '#{text.title}':\n"
  text.section_titles.each_with_index {|title, i| puts "#{i + 1}. #{title}"}
  puts "\nPlease select a choice, either by name or number."
end

#full(text) ⇒ Object



110
111
112
113
114
115
# File 'lib/whatsa/cli.rb', line 110

def full(text)
  system("clear")
  puts word_wrap(text.full_text)
  full_text_helpline
  gets_command
end

#full_text_helplineObject



85
86
87
88
89
# File 'lib/whatsa/cli.rb', line 85

def full_text_helpline
  puts "   _______________________________________________________________"
  puts "    (type 'other' if you'd like to select a specific category of"
  puts " information on the topic, or 'new' to find out about something else)"
end

#get_dmb_choice(disambig) ⇒ Object



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

def get_dmb_choice(disambig)
  display_dmb(disambig)
  choice = nil
  loop do
    choice = gets_command
    in_choices = disambig.choices.detect { |c| c.downcase == choice.downcase }
    break if in_choices || choice.to_i > 0
  end
  disambig.choose_article(choice)
end

#gets_commandObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/whatsa/cli.rb', line 23

def gets_command
  input = nil
  loop do
    print "> "
    input = gets.strip
    case input
    when ""
      puts "Please enter a valid input."
    when "exit"
      exit
    when "help"
      instructions
    when "new"
      run
    else
      break
    end
  end
  input
end

#instructionsObject



9
10
11
12
13
14
15
16
# File 'lib/whatsa/cli.rb', line 9

def instructions
  puts "Enter a word (or phrase) to get a brief summary of that subject."
  puts "If you aren't satisfied, type 'more' to get a slightly longer"
  puts "description, or type 'other' to get a list of specific categories"
  puts "regarding that subject, which you can choose by number or name."
  puts "You can type 'exit' to close the program (or 'help' to receive"
  puts "these instructions again) at any time!"
end

#runObject



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/whatsa/cli.rb', line 124

def run
  welcome
  instructions

  loop do
    # get a search term
    input = ask
    scraper = Whatsa::Scraper.new(input)

    # get an article from the search, or restart the loop if it can't be found
    if scraper.not_found?
      puts "Hmmm... I don't know what '#{input}' means! Try something else."
      next
    elsif scraper.disambig?
      article = get_dmb_choice(scraper.make_disambig)
    else
      article = scraper.make_article
    end

    # summarize that article
    input = summarize(article)

    # the only valid input here that would go uncaught is "other", so
    # keep asking until you get a caught input (logic determined by
    # #gets_command, e.g. "help", "exit", "new") or "other"
    loop { input = input.downcase == "other" ? categories(article) : gets_command }
  end
end

#summarize(text) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/whatsa/cli.rb', line 102

def summarize(text)
  system("clear")
  puts word_wrap(text.summary)
  summary_helpline
  input = gets_command
  input.downcase == "more" ? full(text) : input
end

#summary_helplineObject



78
79
80
81
82
83
# File 'lib/whatsa/cli.rb', line 78

def summary_helpline
  puts "   _______________________________________________________________"
  puts "   (type 'more' for a potentially longer summary, 'other' if you'd"
  puts "   like to select a specific category of information on the topic,"
  puts "             or 'new' to find out about something else)"
end

#welcomeObject



3
4
5
6
7
# File 'lib/whatsa/cli.rb', line 3

def welcome
  system('clear')
  puts "Whatsa is a quick-and-dirty lookup utility, powered by Wikipedia!"
  puts "-----------------------------------------------------------------"
end

#word_wrap(text) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/whatsa/cli.rb', line 44

def word_wrap(text)
  count = 0
  words = text.split(/ /)
  words.each_with_index do |word, index|
    count += word.length + 1
    if count > 80
      words.insert(index, "\n")
      count = 0
    elsif word.index("\n")
      count = word.length
    end
  end
  words.join(" ").gsub(/^ /, "")
end