Class: CommandLineInterface

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

Class Method Summary collapse

Class Method Details

.display_all_topicsObject

beautifies and lists the command line options


52
53
54
55
56
57
# File 'lib/command_line_interface.rb', line 52

def self.display_all_topics
  colors = [:red, :green, :yellow, :blue, :magenta, :cyan, :red, :green, :yellow, :blue, :magenta]
  @list.each_with_index{|item,indx|
    puts "#{indx + 1}. #{item}".colorize(colors[indx])
  }
end

.explore_moreObject


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/command_line_interface.rb', line 113

def self.explore_more
  puts "Either type 'reroll' to choose another page within the " + @choice + " topic you selected. Or select a new topic with 'new'."
  get_current_input
  if !quit?(@current_input)
    if @current_input == "REROLL"
      get_rand_url
      visit_portal
    elsif @current_input == "NEW"
      get_inputs
    else
      puts "Invalid input please enter (reroll/new)."
      explore_more
    end
  end
end

.generate_topic_listObject


30
31
32
# File 'lib/command_line_interface.rb', line 30

def self.generate_topic_list
  @list = Scraper.all_topics
end

.get_current_inputObject


17
18
19
# File 'lib/command_line_interface.rb', line 17

def self.get_current_input
  @current_input = gets.strip.upcase
end

.get_inputsObject


44
45
46
47
48
49
# File 'lib/command_line_interface.rb', line 44

def self.get_inputs
  display_all_topics         #Displays all available main topics
  get_topic_choice #gets users main topic choice
  get_rand_url #gets random sub-topic and creates Portal objects
  visit_portal #asks user if they want to visit the randomnly select sub-topic
end

.get_rand_urlObject


34
35
36
37
38
39
40
41
42
# File 'lib/command_line_interface.rb', line 34

def self.get_rand_url
  # binding.pry
  @randurl = Scraper.scrape_portals_page(@topic.name)
  @portal = Portal.find_or_create_by_url(@randurl)
  @selected = Scraper.get_portal_name(@randurl)
  @portal.name = @selected
  @portal.topic = @topic
  @topic.portals << @portal
end

.get_topic_choiceObject


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/command_line_interface.rb', line 60

def self.get_topic_choice
  puts "Select a number (1-12) to explore that topic"
  get_current_input
  # binding.pry
  if !quit?(@current_input)
    while !@current_input.to_i.between?(1,12)#ask_input != "1"
      # self.send(__callee__)
      puts "Invalid input. Please choose a number between 1 and 12."
      get_topic_choice
    end
    @choice = @list[@current_input.to_i - 1]
    @topic = Topic.find_or_create_by_name(@choice)
    # binding.pry
  else
    quit?(@current_input)
  end
end

.keep_exploring?Boolean

Returns:

  • (Boolean)

101
102
103
104
105
106
107
108
109
110
111
# File 'lib/command_line_interface.rb', line 101

def self.keep_exploring?
  get_current_input
  if @current_input == "Y"
    start
  elsif @current_input == "N"
    quit?("EXIT")
  else
    puts "Please enter a valid command (Y/N) to keep exploring."
    keep_exploring?
  end
end

.quit?(option) ⇒ Boolean

Returns:

  • (Boolean)

21
22
23
24
25
26
27
28
# File 'lib/command_line_interface.rb', line 21

def self.quit?(option)
  if option == "EXIT"
    @status = "offline"
    puts "Goodbye explorer."
  else
    false
  end
end

.runObject


2
3
4
5
6
7
8
# File 'lib/command_line_interface.rb', line 2

def self.run
  puts "Welcome to Did-You-Know Wikipedia Edition!"
  puts "To quit the Wikipedia explorer at anytime type 'exit'."
  @status = "online"
  generate_topic_list
  start
end

.startObject


10
11
12
13
14
15
# File 'lib/command_line_interface.rb', line 10

def self.start
  puts "Please select a topic to be given a random Wikipedia Portal to read:"
  while @status == "online"
    get_inputs #starts cli flow
  end
end

.visit_page?Boolean

Returns:

  • (Boolean)

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/command_line_interface.rb', line 85

def self.visit_page?
  #checks if user wants to visit the selected portal page
  get_current_input
  if !quit?(@current_input)
    case @current_input
    when "Y"
      puts @randurl
      Launchy.open(@randurl)
      puts "Do you want to continue exploring? (Y/N)"
      keep_exploring?
    when "N"
      explore_more
    end
  end
end

.visit_portalObject


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

def self.visit_portal
  puts "We've selected " + @selected
 + " for you within the " + @choice +" topic you selected."
  puts "Would you like to visit this page? (Y/N)"
  visit_page?
end