ubcbooker 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4bc08809f822c73cd88eefdfb8d1b3e7dcdd67c1
4
- data.tar.gz: d3b83b3f5966ba048b77c45b9423bf7f900db0af
3
+ metadata.gz: 053f9ad2f8d23269ce39d0c7e6cf53b51774d810
4
+ data.tar.gz: 79624f9a62de98c504efc5a53b96ee94da8ab41d
5
5
  SHA512:
6
- metadata.gz: aa3dba7476926f4b89e1902203bccabb1f7dd50b93a4d7583b54db993b365b19334a07239176912b83749a8e29dba0719103a9ebd2381b4e48f9b1618a9c12af
7
- data.tar.gz: 6e233736d5d31d8781d76685ead16a69d94ca4e846ca429e58a4558917596611aee97072ed2bc92ee3b7e950bfd8d971c6c7ceaf04ea657e671dbce30083c5e3
6
+ metadata.gz: 8464319a1fbebeb17ae4c7e66ff9335cf2a3125bd5cfe81b8aec9eacafd93b40d8b14813dbec3258bce30601eb3f0d0984b8fe77dfdc202dacb206edd4bdcca6
7
+ data.tar.gz: 5990bc3fdb45c82be09c794b712e199701a8a13d427281b480c6ba94c2ba889c8cccd6d609dfdd1dba5e8787fdcbea232dbb823bcdfab5c3a1e2f9747c053ec5
data/.gitignore CHANGED
@@ -2,6 +2,7 @@
2
2
  *.gem
3
3
  *.rbc
4
4
  lib/ubcbooker/config.yml
5
+ spec/config.yml
5
6
  /.config
6
7
  /coverage/
7
8
  /InstalledFiles
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  CLI tool to book project rooms in UBC
4
4
 
5
+ ![Demo](https://i.imgur.com/UhT9zKJ.gif)
6
+
5
7
  ## Installation
6
8
 
7
9
  Install it as:
@@ -21,7 +23,17 @@ And then execute:
21
23
  ## Usage
22
24
 
23
25
  ```
24
- $ ubcbooker --helj
26
+ # Book a room in CS with the name 'Study Group' from 11am to 1pm on March 5th
27
+
28
+ $ ubcbooker -b cs -n 'Study Group' -d 03/05 -t 11:00-13:00
29
+ ```
30
+
31
+ When you first run the command it will ask for your CWL account auth info. This is saved locally within the gem as `config.yml`. You can look at [sample.yml](https://github.com/jumbosushi/ubcbooker/blob/master/lib/ubcbooker/sample.yml) to understand what it will look like.
32
+
33
+ Run `ubcbooker -u` to update the CWL auth info.
34
+
35
+ ```
36
+ $ ubcbooker --help
25
37
 
26
38
  Usage: ubcbooker [options]
27
39
  -b, --building BUILDING Specify which department to book rooms from
@@ -37,7 +49,8 @@ ex. Book a room in CS from 11am to 1pm on March 5th with the name 'Study Group'
37
49
  $> ubcbooker -b cs -n 'Study Group' -d 03/05 -t 11:00-13:00
38
50
  ```
39
51
 
40
- Currently this app supports project rooms in cs (Commputer Science).
52
+ Currently this app supports project rooms in cs (Commputer Science). Run `ubcbooker -l` to check the latest supported departments.
53
+
41
54
  Feel free to send a PR that supports your department's rooms.
42
55
 
43
56
  ## Development
data/Rakefile CHANGED
@@ -1,6 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rspec/core/rake_task"
3
3
 
4
+ ENV['RACK_ENV'] == 'test'
4
5
  RSpec::Core::RakeTask.new(:spec)
5
6
 
6
7
  task :default => :spec
@@ -7,16 +7,6 @@ module Ubcbooker
7
7
  @options = nil
8
8
  end
9
9
 
10
- def ask_config
11
- print "Your CWL username: "
12
- username = gets.chomp
13
- print "Your CWL password: "
14
- # Hide the password input
15
- password = STDIN.noecho(&:gets).chomp
16
- @config.write(username, password)
17
- puts
18
- end
19
-
20
10
  def parse_options
21
11
 
22
12
  # This will hold the options we parse
@@ -47,9 +37,8 @@ module Ubcbooker
47
37
 
48
38
  parser.on("-h", "--help", "Show this help message") do
49
39
  puts parser
50
- puts
51
- puts "ex. Book a room in CS from 11am to 1pm on March 5th with the name 'Study Group'"
52
- puts " $>ubcbooker -b cs -n 'Study Group' -d 03/05 -t 11:00-13:00"
40
+ puts "\nex. Book a room in CS from 11am to 1pm on March 5th with the name 'Study Group'\n" <<
41
+ " $ ubcbooker -b cs -n 'Study Group' -d 03/05 -t 11:00-13:00"
53
42
  exit(0)
54
43
  end
55
44
 
@@ -75,7 +64,7 @@ module Ubcbooker
75
64
  end
76
65
 
77
66
  parser.on("-u", "--update", "Update username and password") do |v|
78
- ask_config
67
+ @config.ask
79
68
  exit(0)
80
69
  end
81
70
 
@@ -89,7 +78,7 @@ module Ubcbooker
89
78
  spinner.success("Done!") # Stop animation
90
79
 
91
80
  if CLI::Validator.is_required_missing(options)
92
- raise OptionParser::MissingArgument
81
+ raise Ubcbooker::Error::MissingRequired
93
82
  end
94
83
 
95
84
  return options
@@ -101,15 +90,11 @@ module Ubcbooker
101
90
  Ubcbooker::Error::UnsupportedTime,
102
91
  Ubcbooker::Error::UnsupportedDate,
103
92
  Ubcbooker::Error::ProfaneName,
93
+ Ubcbooker::Error::MissingRequired,
104
94
  ]
105
95
 
106
96
  begin
107
97
  return parse_options
108
- rescue OptionParser::MissingArgument
109
- puts "Error: Missing Option\n".red <<
110
- "One or more of required option are missing values\n" <<
111
- "Please check if options -b, -d, -n, and -t all have values passed"
112
- exit(1)
113
98
  rescue *option_errors => e
114
99
  puts e.message
115
100
  exit(1)
@@ -139,8 +124,13 @@ module Ubcbooker
139
124
  end
140
125
 
141
126
  def start
127
+ book_errors = [
128
+ Ubcbooker::Error::NoAvailableRoom,
129
+ Ubcbooker::Error::BookingFailed,
130
+ ]
131
+
142
132
  @options = get_options
143
- ask_config if !@config.defined?
133
+ @config.ask if !@config.defined?
144
134
 
145
135
  @client = get_scraper(@options[:department],
146
136
  @config.account["username"],
@@ -148,8 +138,9 @@ module Ubcbooker
148
138
  begin
149
139
  room_id = @client.book(@options)
150
140
  puts "Success! #{room_id} is booked".green
151
- rescue Ubcbooker::Error::NoAvailableRoom => e
141
+ rescue *book_errors => e
152
142
  puts e.message
143
+ exit(1)
153
144
  end
154
145
  end
155
146
  end
@@ -3,7 +3,7 @@ module Ubcbooker
3
3
  attr_accessor :account
4
4
 
5
5
  def initialize
6
- @config_path = File.expand_path("../config.yml", __FILE__)
6
+ @config_path = get_config_path
7
7
  if !File.exist?(@config_path)
8
8
  create_config_file(@config_path)
9
9
  end
@@ -11,6 +11,16 @@ module Ubcbooker
11
11
  @account = YAML.load_file(@config_path)
12
12
  end
13
13
 
14
+ def ask
15
+ print "Your CWL username: "
16
+ username = gets.chomp
17
+ print "Your CWL password: "
18
+ # Hide the password input
19
+ password = STDIN.noecho(&:gets).chomp
20
+ write(username, password)
21
+ puts
22
+ end
23
+
14
24
  def create_config_file(config_path)
15
25
  File.open(config_path, "w") do |f|
16
26
  f.write("---\nusername: sample\npassword: sample")
@@ -32,8 +42,18 @@ module Ubcbooker
32
42
  end
33
43
  end
34
44
 
45
+ def get_config_path
46
+ if ENV['RACK_ENV'] == 'test'
47
+ File.expand_path("../config.yml", __FILE__)
48
+ else
49
+ File.expand_path("../../../spec/config.yml", __FILE__)
50
+ end
51
+ end
52
+
35
53
  def defined?
36
- return @account["username"] != "sample" && @account["password"] != "sample"
54
+ return File.exist?(@config_path) &&
55
+ @account["username"] != "sample" &&
56
+ @account["password"] != "sample"
37
57
  end
38
58
  end
39
59
  end
@@ -35,6 +35,17 @@ module Ubcbooker
35
35
  end
36
36
  end
37
37
 
38
+ class MissingRequired < StandardError
39
+ attr_reader :message, :time
40
+ def initialize()
41
+ @message = "Error: Missing Option\n".red <<
42
+ "One or more of required option are missing values\n" <<
43
+ "Please check if options -b, -d, -n, and -t all have values passed"
44
+ super
45
+ end
46
+ end
47
+
48
+
38
49
  class ProfaneName < StandardError
39
50
  attr_reader :message, :name
40
51
  def initialize(name = "unknown")
@@ -57,6 +68,15 @@ module Ubcbooker
57
68
  end
58
69
  end
59
70
 
71
+ class BookingFailed < StandardError
72
+ attr_reader :message
73
+ def initialize
74
+ @message = "\nBooking Failed :/\n".red <<
75
+ "Please raise an issue on GitHub"
76
+ super
77
+ end
78
+ end
79
+
60
80
  class LoginFailed < StandardError
61
81
  attr_reader :message
62
82
  def initialize
@@ -46,8 +46,13 @@ module Ubcbooker
46
46
  booking_form["field_date[und][0][value][time]"] = time_to_ampm(book_slot.min)
47
47
  booking_form["field_date[und][0][value2][date]"] = book_date_str
48
48
  booking_form["field_date[und][0][value2][time]"] = time_to_ampm(book_slot.max)
49
- booking_form.submit
50
- spinner.success("Done!")
49
+ confirmation_page = booking_form.submit
50
+ if confirmation_page.search("div.alert-error").empty?
51
+ spinner.success("Done!")
52
+ else
53
+ spinner.fail("Booking rejected")
54
+ raise Ubcbooker::Error::BookingFailed.new
55
+ end
51
56
  end
52
57
 
53
58
  # Select the form otpion with right room id
@@ -115,7 +120,9 @@ module Ubcbooker
115
120
  def is_slot_booked(slot_booked, book_slot)
116
121
  booked = false
117
122
  slot_booked.each do |s|
118
- if s.include?(book_slot.min) || s.include?(book_slot.max)
123
+ if (s.include?(book_slot.min) ||
124
+ s.include?(book_slot.max) ||
125
+ book_slot.include?(s))
119
126
  booked = true
120
127
  end
121
128
  end
@@ -1,3 +1,3 @@
1
1
  module Ubcbooker
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ubcbooker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Atsushi Yamamoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-11 00:00:00.000000000 Z
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -168,10 +168,8 @@ files:
168
168
  - README.md
169
169
  - Rakefile
170
170
  - bin/console
171
- - bin/setup
172
171
  - bin/ubcbooker
173
172
  - lib/ubcbooker.rb
174
- - lib/ubcbooker/account.rb
175
173
  - lib/ubcbooker/cli.rb
176
174
  - lib/ubcbooker/cli_validator.rb
177
175
  - lib/ubcbooker/color.rb
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,31 +0,0 @@
1
- module Ubcbooker
2
- class Account
3
- attr_accessor :username, :password
4
-
5
- def initialize
6
- @config_path = File.expand_path("../config.yml", __FILE__)
7
- @account_info = YAML.load_file(@config_path)
8
- @username = @account_info["username"]
9
- @password = @account_info["password"]
10
- end
11
-
12
- def write(username, password)
13
- @account["username"] = username
14
- @account["password"] = password
15
- new_yml = YAML.dump(@account)
16
- open(@config_path, "w") { |f| f.write(new_yml) }
17
- @account = YAML.load_file(@config_path)
18
- end
19
-
20
- def print_supported_departments
21
- puts "Supported department options in #{Ubcbooker::VERSION}:"
22
- BOOKING_URL.keys.each do |d|
23
- puts " - #{d}"
24
- end
25
- end
26
-
27
- def defined?
28
- return @account["username"] != "hoge" && @account["password"] != "hoge"
29
- end
30
- end
31
- end