Class: Ubcbooker::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ubcbooker/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

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

#accountObject

Returns the value of attribute account.



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

def 
  @account
end

Instance Method Details

#askObject



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

Returns:

  • (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_pathObject



45
46
47
48
49
50
51
# File 'lib/ubcbooker/config.rb', line 45

def get_config_path
  if ENV['RACK_ENV'] == 'test'
    File.expand_path("../config.yml", __FILE__)
  else
    File.expand_path("../../../spec/config.yml", __FILE__)
  end
end


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