class NycToday::CLI

Public Instance Methods

call() click to toggle source
# File lib/nyc_today/cli.rb, line 6
def call
  puts welcome
  NycToday::Scraper.scrape_events
  list_event_types
end
category() click to toggle source
# File lib/nyc_today/cli.rb, line 81
def category
  NycToday::Event.event_types[@@type_choice]
end
choose_type() click to toggle source
# 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() click to toggle source
# 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
    reset_menu
  else
    error
    end_of_list
  end
end
error() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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 menu_bottom
  choose_type
  list_events
end
list_events() click to toggle source
# 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() click to toggle source
more_events() click to toggle source
# 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 'menu' to return to the main menu
      * Enter 'back' to go back or 'exit' to leave the program
    HEREDOC
  end
more_info(event) click to toggle source
# 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
  return_to_menu
end
page_count() click to toggle source
# File lib/nyc_today/cli.rb, line 77
def page_count
  NycToday::Event.event_sets(@@type_choice).count
end
reset_menu() click to toggle source
# File lib/nyc_today/cli.rb, line 195
def reset_menu
  system "clear"
  NycToday::Event.reset_sets
  @@set_no = 0
  @@type_choice = 0
  list_event_types
end
return_to_menu() click to toggle source
# File lib/nyc_today/cli.rb, line 159
def return_to_menu
  puts event_info_bottom
  input = gets.strip.downcase
  if input == "exit"
    goodbye
  else
    system "clear"
    list_events
  end
end
selection() click to toggle source
# 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"
    reset_menu
  elsif input == "back"
    if @@set_no >= 1
      @@set_no -= 1
      list_events
    else
      reset_menu
    end
    puts
  elsif input == "exit"
    goodbye
  else
    system "clear"
    error
    sleep 2
    list_events
  end
end
type_entry() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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