Class: Ubcbooker::Config
- Inherits:
-
Object
- Object
- Ubcbooker::Config
- Defined in:
- lib/ubcbooker/config.rb
Instance Attribute Summary collapse
-
#account ⇒ Object
Returns the value of attribute account.
Instance Method Summary collapse
- #ask ⇒ Object
- #create_config_file(config_path) ⇒ Object
- #defined? ⇒ Boolean
- #get_config_path ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #print_supported_departments ⇒ Object
- #write(username, password) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
5 6 7 8 9 10 11 12 |
# File 'lib/ubcbooker/config.rb', line 5 def initialize @config_path = get_config_path if !File.exist?(@config_path) create_config_file(@config_path) end @account = YAML.load_file(@config_path) end |
Instance Attribute Details
#account ⇒ Object
Returns the value of attribute account.
3 4 5 |
# File 'lib/ubcbooker/config.rb', line 3 def account @account end |
Instance Method Details
#ask ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/ubcbooker/config.rb', line 14 def ask print "Your CWL username: " username = gets.chomp print "Your CWL password: " # Hide the password input password = STDIN.noecho(&:gets).chomp write(username, password) puts end |
#create_config_file(config_path) ⇒ Object
24 25 26 27 28 |
# File 'lib/ubcbooker/config.rb', line 24 def create_config_file(config_path) File.open(config_path, "w") do |f| f.write("---\nusername: sample\npassword: sample") end end |
#defined? ⇒ Boolean
53 54 55 56 57 |
# File 'lib/ubcbooker/config.rb', line 53 def defined? return File.exist?(@config_path) && @account["username"] != "sample" && @account["password"] != "sample" end |
#get_config_path ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/ubcbooker/config.rb', line 45 def get_config_path if ENV['RACK_ENV'] == 'test' File.("../config.yml", __FILE__) else File.("../../../spec/config.yml", __FILE__) end end |
#print_supported_departments ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ubcbooker/config.rb', line 38 def print_supported_departments puts "Supported department options in #{Ubcbooker::VERSION}:" BOOKING_URL.keys.each do |d| puts " - #{d}" end end |
#write(username, password) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/ubcbooker/config.rb', line 30 def write(username, password) @account["username"] = username @account["password"] = password new_yml = YAML.dump(@account) open(@config_path, "w") { |f| f.write(new_yml) } @account = YAML.load_file(@config_path) end |