Class: Ubcbooker::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ubcbooker/cli.rb,
lib/ubcbooker/cli_validator.rb

Defined Under Namespace

Modules: Validator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



5
6
7
8
# File 'lib/ubcbooker/cli.rb', line 5

def initialize
  @config = Ubcbooker::Config.new
  @options = nil
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/ubcbooker/cli.rb', line 3

def options
  @options
end

Instance Method Details

#ask_configObject



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_optionsObject



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 get_options
  option_errors = [
    Ubcbooker::Error::UnsupportedDepartment,
    Ubcbooker::Error::UnsupportedTime,
    Ubcbooker::Error::UnsupportedDate,
    Ubcbooker::Error::ProfaneName,
  ]

  begin
    return parse_options
  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.message
    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_optionsObject



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 parse_options

  # This will hold the options we parse
  options = {
    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)
        options[: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)
        options[: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)
        options[: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)
        options[: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(options)
    raise OptionParser::MissingArgument
  end

  return options
end

#startObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ubcbooker/cli.rb', line 141

def start
  @options = get_options
  ask_config if !@config.defined?

  @client = get_scraper(@options[:department],
                        @config.["username"],
                        @config.["password"])
  begin
    room_id = @client.book(@options)
    puts "Success! #{room_id} is booked".green
  rescue Ubcbooker::Error::NoAvailableRoom => e
    puts e.message
  end
end