Class: Ubcbooker::CLI
- Inherits:
-
Object
- Object
- Ubcbooker::CLI
- Defined in:
- lib/ubcbooker/cli.rb,
lib/ubcbooker/cli_validator.rb
Defined Under Namespace
Modules: Validator
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #get_department_scraper(department) ⇒ Object
- #get_options ⇒ Object
- #get_scraper(department, username, password) ⇒ Object
- #get_spinner(text) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #parse_options ⇒ Object
- #start ⇒ Object
Constructor Details
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/ubcbooker/cli.rb', line 3 def @options end |
Instance Method Details
#get_department_scraper(department) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ubcbooker/cli.rb', line 104 def get_department_scraper(department) case department when "cs" return Ubcbooker::Scraper::Cs when "sauder_ugrad" return Ubcbooker::Scraper::SauderUgrad else raise Ubcbooker::Error::UnsupportedDepartment.new(department) end end |
#get_options ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/ubcbooker/cli.rb', line 87 def option_errors = [ Ubcbooker::Error::UnsupportedDepartment, Ubcbooker::Error::UnsupportedTime, Ubcbooker::Error::UnsupportedDate, Ubcbooker::Error::ProfaneName, Ubcbooker::Error::MissingRequired, ] begin return rescue *option_errors => e puts e. exit(1) end end |
#get_scraper(department, username, password) ⇒ Object
115 116 117 118 |
# File 'lib/ubcbooker/cli.rb', line 115 def get_scraper(department, username, password) scraper_client = get_department_scraper(department) return scraper_client.new(username, password) end |
#get_spinner(text) ⇒ Object
120 121 122 123 124 |
# File 'lib/ubcbooker/cli.rb', line 120 def get_spinner(text) spinner = ::TTY::Spinner.new("[:spinner] #{text} ... ", format: :dots) spinner.auto_spin # Automatic animation with default interval return spinner end |
#parse_options ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ubcbooker/cli.rb', line 10 def # This will hold the options we parse = { name: nil, date: nil, time: nil, department: nil, } # TODO: Change department to building OptionParser.new do |parser| parser.on("-b", "--building BUILDING", String, "Specify which department to book rooms from") do |v| if CLI::Validator.is_valid_department(v) [:department] = v else raise Ubcbooker::Error::UnsupportedDepartment.new(v) end end parser.on("-d", "--date DATE", String, "Specify date to book rooms for (MM/DD)") do |v| if CLI::Validator.is_valid_date(v) [:date] = v else raise Ubcbooker::Error::UnsupportedDate.new(v) end end parser.on("-h", "--help", "Show this help message") do puts parser puts "\nex. Book a room in CS from 11am to 1pm on March 5th with the name 'Study Group'\n" << " $ ubcbooker -b cs -n 'Study Group' -d 03/05 -t 11:00-13:00" exit(0) end parser.on("-l", "--list", "List supported departments") do |v| @config.print_supported_departments exit(0) end parser.on("-n", "--name NAME", String, "Name of the booking") do |v| if CLI::Validator.is_valid_name(v) [:name] = v else raise Ubcbooker::Error::ProfaneName.new(v) end end parser.on("-t", "--time TIME", String, "Specify time to book rooms for (HH:MM-HH:MM)") do |v| if CLI::Validator.is_valid_time(v) [:time] = v else raise Ubcbooker::Error::UnsupportedTime.new(v) end end parser.on("-u", "--update", "Update username and password") do |v| @config.ask exit(0) end parser.on("-v", "--version", "Show version") do |v| puts Ubcbooker::VERSION exit(0) end end.parse! spinner = get_spinner("Verifying inputs") spinner.success("Done!") # Stop animation if CLI::Validator.is_required_missing() raise Ubcbooker::Error::MissingRequired end return end |
#start ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/ubcbooker/cli.rb', line 126 def start book_errors = [ Ubcbooker::Error::NoAvailableRoom, Ubcbooker::Error::BookingFailed, ] @options = if !@config.defined? || @config.is_windows? @config.ask end @client = get_scraper(@options[:department], @config.get_username, @config.get_password) begin room_id = @client.book(@options) puts "Success! #{room_id} is booked".green rescue *book_errors => e puts e. exit(1) end end |