class Controller
Constants
- STATIONS
Attributes
advisories[RW]
name[RW]
station_instance[RW]
Public Instance Methods
check_if_done()
click to toggle source
# File lib/gobart/controller.rb, line 130 def check_if_done #checks if the user is done with this session and checks for valid responses input_validator = false info_request = false while input_validator == false && info_request == false puts "\nCheck another station? [\'y\'/\'n\' or \'i\' for above station information]" check = gets.strip.downcase if check == 'y' || check == 'n' input_validator = true elsif check == 'i' puts "\n#{station_instance.info}" done = false else puts "\nALERT! Invalid response --> type \'y\'/\'n\' or \'i\'" end end if check == 'y' done = false else done = true end done end
converter(status_data)
click to toggle source
# File lib/gobart/controller.rb, line 109 def converter(status_data) #converts the status data into a more user friendly, easier to read format status_data.each do |x| puts ">>Destination: #{x[:destination]} (#{x[:abbreviation]})" #displays destination info combined_array = x[:arrivals].zip(x[:cars]) #combines the arrivals and cars arrays so they can be displayed together below combined_array.each do |x| puts "#{x[0]} min (#{x[1]} cars)" #displays the arrivals and cars information end end end
display_results(station)
click to toggle source
# File lib/gobart/controller.rb, line 120 def display_results(station) #displays real time station data returned from the station instance puts "\n#{station.upcase} departures as of #{Time.now}\n" converter(station_instance.status) puts "\n*** Station Advisory ***\n" puts station_instance.advisories puts "\nThere are #{Scraper.scrape_train_count} trains running systemwide at this time.\n" puts "" puts "Data provided by BART.gov. Real time train departure data available at https://www.bart.gov/schedules/eta." end
display_stations()
click to toggle source
# File lib/gobart/controller.rb, line 57 def display_stations #displays stations and codes upon request by user STATIONS.each do |info| puts "#{info[:station]} (#{info[:code]})" end end
get_input()
click to toggle source
# File lib/gobart/controller.rb, line 69 def get_input #returns valid BART station code; for complete list, go to http://api.bart.gov/docs/overview/abbrev.aspx if !Station.all.empty? print "\n(Recent searches: #{Station.all.collect {|x| x.name}})\n" end valid_station = false #sets the flag for the loop below while valid_station == false #checks to make sure the user is entering a valid station puts "\nEnter departure station code (e.g. \'woak\' for West Oakland) for real time departure information (type 'list' for codes):" station = gets.strip.downcase if station == "list" display_stations elsif STATIONS.detect {|x| x[:code] == station} #validates response using chart above valid_station = true else puts "\n ALERT! Invalid station code -> try again" end end station #returns the station value end
goodbye()
click to toggle source
# File lib/gobart/controller.rb, line 157 def goodbye #prints search history for the session and leaves user with pleasant wishes if !Station.all.empty? print "\n(Search history for this session: #{Station.all.collect {|x| x.name}})\n" end puts "\nHave a safe and pleasant journey!\n" end
handle_request()
click to toggle source
# File lib/gobart/controller.rb, line 90 def handle_request #controls the fulfillment of the query; because the fulfillment process is complex, this method was written to simplify and better manage the calls to the various processes. done = false while done == false station = get_input process_request(station) display_results(station) done = check_if_done end end
process_request(station)
click to toggle source
# File lib/gobart/controller.rb, line 100 def process_request(station) #determines if the station request already has an instance; if it does, it does not create another one. regardless of the outcome, a call is made to the station class to get updates from the BART api if !Station.all.detect {|x| x.instance_variable_get("@name") == station} @station_instance = Station.new(station) #creates a new station instance if it does not already exist in the Station.all else @station_instance = Station.all.detect {|x| x.instance_variable_get("@name") == station} end station_instance.call(station) end
run()
click to toggle source
# File lib/gobart/controller.rb, line 168 def run #entry point into the controller from the bin file welcome handle_request goodbye end
welcome()
click to toggle source
# File lib/gobart/controller.rb, line 64 def welcome #provides a nice welcome message with the current time. puts "\nReal Time BART.gov Train Departures" puts "\nCurrent time is #{Time.now}" end