Class: NycToday::CLI
- Inherits:
-
Object
- Object
- NycToday::CLI
- Defined in:
- lib/nyc_today/cli.rb
Constant Summary collapse
- @@set_no =
0
- @@type_choice =
0
Instance Method Summary collapse
- #call ⇒ Object
- #category ⇒ Object
- #choose_type ⇒ Object
- #end_of_list ⇒ Object
- #error ⇒ Object
- #event_entry ⇒ Object
- #event_info_bottom ⇒ Object
- #goodbye ⇒ Object
- #list_event_types ⇒ Object
- #list_events ⇒ Object
- #menu_bottom ⇒ Object
- #more_events ⇒ Object
- #more_info(event) ⇒ Object
- #page_count ⇒ Object
- #reset_menu ⇒ Object
- #return_to_menu ⇒ Object
- #selection ⇒ Object
- #type_entry ⇒ Object
- #welcome ⇒ Object
- #wrap(text, width = 80) ⇒ Object
Instance Method Details
#call ⇒ Object
6 7 8 9 10 |
# File 'lib/nyc_today/cli.rb', line 6 def call puts welcome NycToday::Scraper.scrape_events list_event_types end |
#category ⇒ Object
81 82 83 |
# File 'lib/nyc_today/cli.rb', line 81 def category NycToday::Event.event_types[@@type_choice] end |
#choose_type ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/nyc_today/cli.rb', line 49 def choose_type input = gets.strip if input.to_i > 0 && input.to_i <= NycToday::Event.event_types.count @@type_choice = input.to_i-1 elsif input.downcase == "exit" goodbye else system "clear" puts "I'm sorry, that is not an option. Please choose a number from the menu." sleep 1 system "clear" list_event_types end end |
#end_of_list ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/nyc_today/cli.rb', line 179 def end_of_list puts "\nYou've reached the end of the #{category} events list. Would you like to see it again? (Y/n)" input = gets.strip.downcase if input == "y" || input == "yes" @@set_no = 0 list_events elsif input == "n" || input == "no" puts "\nReturning to main menu..." sleep 1 else error end_of_list end end |
#error ⇒ Object
92 93 94 95 96 |
# File 'lib/nyc_today/cli.rb', line 92 def error puts "I'm sorry, I didn't understand what you typed. Please try again." sleep 1 system "clear" end |
#event_entry ⇒ Object
85 86 87 88 89 90 |
# File 'lib/nyc_today/cli.rb', line 85 def event_entry NycToday::Event.event_sets(@@type_choice)[@@set_no].each.with_index(1) do |event, i| puts "#{i.to_s.rjust(3," ")} | #{event.name}" puts " | #{event.time} at #{event.venue}\n\n" end end |
#event_info_bottom ⇒ Object
170 171 172 173 174 175 176 177 |
# File 'lib/nyc_today/cli.rb', line 170 def event_info_bottom <<~HEREDOC -------------------------------------------------------------------------------- * Press Enter to return to the list of events * Type 'exit' to leave the program HEREDOC end |
#goodbye ⇒ Object
203 204 205 206 207 208 209 |
# File 'lib/nyc_today/cli.rb', line 203 def goodbye system "clear" puts "\nGood-bye! Come back tomorrow for a new list of events." sleep 1.1 system "clear" exit end |
#list_event_types ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/nyc_today/cli.rb', line 25 def list_event_types system "clear" puts "\nHere are today's event categories:\n\n" type_entry puts choose_type list_events end |
#list_events ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/nyc_today/cli.rb', line 64 def list_events system "clear" page_count category if @@set_no+1 <= page_count puts "\nHere's page #{@@set_no+1}/#{page_count} of today's #{category} events ordered by time:\n\n" event_entry else end_of_list end selection end |
#menu_bottom ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/nyc_today/cli.rb', line 34 def <<~HEREDOC ------------------------------------------------------------- * Enter a number for the type of event you would like to see. * Type 'exit' to leave the program. HEREDOC end |
#more_events ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/nyc_today/cli.rb', line 128 def more_events <<~HEREDOC ------------------------------------------------------------------- * Enter the number of any event you'd like to know more about * Press Enter for more events * Enter '' to return to the main menu * Enter 'back' to go back or 'exit' to leave the program HEREDOC end |
#more_info(event) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/nyc_today/cli.rb', line 138 def more_info(event) if event.event_info != nil || event.price != nil system "clear" puts "\n--------------------------------------------------------------------------------\n\n" puts "Ticket info: #{event.price}\n\n" unless event.price == nil puts wrap(event.event_info, 80) unless event.event_info == nil else system "clear" puts "\nI'm sorry, there is no additional information about this event." end end |
#page_count ⇒ Object
77 78 79 |
# File 'lib/nyc_today/cli.rb', line 77 def page_count NycToday::Event.event_sets(@@type_choice).count end |
#reset_menu ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/nyc_today/cli.rb', line 195 def system "clear" NycToday::Event.reset_sets @@set_no = 0 @@type_choice = 0 list_event_types end |
#return_to_menu ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/nyc_today/cli.rb', line 159 def puts event_info_bottom input = gets.strip.downcase if input == "exit" goodbye else system "clear" list_events end end |
#selection ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/nyc_today/cli.rb', line 98 def selection puts more_events input = gets.strip.downcase if input == "" or input == " " @@set_no += 1 list_events elsif input.to_i > 0 event = NycToday::Event.sets[@@set_no][input.to_i-1] NycToday::Scraper.scrape_event_page(event) unless event.event_info != nil more_info(event) elsif input == "menu" elsif input == "back" if @@set_no >= 1 @@set_no -= 1 list_events else end puts elsif input == "exit" goodbye else system "clear" error sleep 2 list_events end end |
#type_entry ⇒ Object
43 44 45 46 47 |
# File 'lib/nyc_today/cli.rb', line 43 def type_entry NycToday::Event.event_types.each.with_index(1) do |event_type, i| puts "#{i.to_s.rjust(2," ")} | #{event_type}" end end |
#welcome ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/nyc_today/cli.rb', line 12 def welcome system "clear" <<~HEREDOC Welcome to NYC Today-- your guide to today's events in and around New York City! Please wait a few seconds while I gather today's events. For the best experience, maximize the terminal. HEREDOC end |
#wrap(text, width = 80) ⇒ Object
151 152 153 154 155 156 157 |
# File 'lib/nyc_today/cli.rb', line 151 def wrap(text, width=80) paragraphs = text.split("\n") wrapped = paragraphs.collect do |para| para.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n") end wrapped end |