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
- #ask_config ⇒ Object
- #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
#ask_config ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/ubcbooker/cli.rb', line 10 def ask_config print "Your CWL username: " username = gets.chomp print "Your CWL password: " # Hide the password input password = STDIN.noecho(&:gets).chomp @config.write(username, password) puts end |
#get_department_scraper(department) ⇒ Object
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ubcbooker/cli.rb', line 119 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/ubcbooker/cli.rb', line 98 def option_errors = [ Ubcbooker::Error::UnsupportedDepartment, Ubcbooker::Error::UnsupportedTime, Ubcbooker::Error::UnsupportedDate, Ubcbooker::Error::ProfaneName, ] begin return rescue OptionParser::MissingArgument puts "Error: Missing Option\n".red << "One or more of required option are missing values\n" << "Please check if options -b, -d, -n, and -t all have values passed" exit(1) rescue *option_errors => e puts e. exit(1) end end |
#get_scraper(department, username, password) ⇒ Object
130 131 132 133 |
# File 'lib/ubcbooker/cli.rb', line 130 def get_scraper(department, username, password) scraper_client = get_department_scraper(department) return scraper_client.new(username, password) end |
#get_spinner(text) ⇒ Object
135 136 137 138 139 |
# File 'lib/ubcbooker/cli.rb', line 135 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
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 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ubcbooker/cli.rb', line 20 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 puts "ex. Book a room in CS from 11am to 1pm on March 5th with the name 'Study Group'" puts " $>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| ask_config 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 OptionParser::MissingArgument end return end |
#start ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ubcbooker/cli.rb', line 141 def start @options = ask_config if !@config.defined? @client = get_scraper(@options[:department], @config.account["username"], @config.account["password"]) begin room_id = @client.book(@options) puts "Success! #{room_id} is booked".green rescue Ubcbooker::Error::NoAvailableRoom => e puts e. end end |