tsolak_lab_reservation 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #$LOAD_PATH.unshift File.dirname(__FILE__)
4
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
5
+
6
+ require_relative '../lib/lab_reservation'
7
+ require_relative '../lib/lab_reservation/version'
8
+ require_relative '../lib/machines'
9
+ require_relative '../lib/requests'
10
+ require_relative '../lib/users'
11
+ require 'date'
12
+
13
+ LabReservation::sample_files
14
+
15
+ puts " Request Options "
16
+ puts " a - add request"
17
+ puts " s - show existing requests"
18
+ puts " q - quit Request Options (return to main menu)"
19
+ opt = gets.chomp
20
+
21
+ while opt != "q" do
22
+
23
+ case
24
+ when opt == "a"
25
+ puts " date required (mm-dd-yyyy)?"
26
+ req_date = gets.chomp
27
+ puts " number of days needed?"
28
+ num_days = gets.chomp
29
+ puts " number of machines needed?"
30
+ num_mach = gets.chomp
31
+ puts " image (XP|Win7|Win8)?"
32
+ img = gets.chomp
33
+ puts " comments (optional)?"
34
+ comment = gets.chomp
35
+ Requests.new.add('1', req_date, num_days, num_mach, img, comment)
36
+ when opt == "f"
37
+ puts " request id?"
38
+ req_id = gets.chomp
39
+ req = Requests.new.get req_id
40
+ req_date = Date.strptime(req[:date_reqd], '%m-%d-%Y')
41
+ puts "date required: #{req_date}"
42
+ puts "days required: #{req[:days_reqd]}"
43
+ puts Machines.new.find_free(req[:date_reqd], req[:days_reqd].to_i)
44
+ when opt == "s"
45
+ Requests.new.show
46
+ else
47
+ puts "Invalid Option, please try again"
48
+ end
49
+
50
+ puts " Request Options"
51
+ puts " a - add request"
52
+ puts " s - show existing requests"
53
+ puts " f - find available machine for request"
54
+ puts " q - quit Request Options (return to main menu)"
55
+ opt = gets.chomp
56
+
57
+ end
@@ -0,0 +1,83 @@
1
+ #$LOAD_PATH.unshift File.dirname(__FILE__)
2
+ $LOAD_PATH.unshift File.expand_path("../lab_reservation", __FILE__)
3
+
4
+ DATADIR = "#{Dir.home}/lr_data"
5
+
6
+ MFILE = "#{DATADIR}/machines.txt"
7
+ RFILE = "#{DATADIR}/requests.txt"
8
+ UFILE = "#{DATADIR}/users.txt"
9
+
10
+ require 'version'
11
+ require 'yaml'
12
+ require 'users'
13
+ require 'requests'
14
+ require 'machines'
15
+
16
+ module LabReservation
17
+
18
+ puts "module LabReservation defined"
19
+
20
+ def self.sample_files
21
+
22
+ if !Dir.exists?(DATADIR)
23
+ Dir.mkdir(DATADIR)
24
+ end
25
+
26
+ if !File.exists?(MFILE)
27
+ File.open(MFILE, "w") do |line|
28
+ line.puts("q123456|23")
29
+ line.puts("q234567|207")
30
+ line.puts("q345678|23, 25")
31
+ line.puts("q987899|25")
32
+ line.puts("q456789|")
33
+ end
34
+ end
35
+
36
+ if !File.exists?(RFILE)
37
+ File.open(RFILE, "w") do |line|
38
+ #req_id|user_id|date_req'd|#days|#machines|image|comments
39
+ line.puts("24|267|04-23-2013|10|4|WIN7|for internal testing")
40
+ line.puts("25|4|05-01-2013|3|1|WIN8|have a good weekend")
41
+ line.puts("207|3|04-15-2013|5|2|WIN7|")
42
+ line.puts("23|3|04-15-2013|5|2|XP|sorry I need so many at once")
43
+ end
44
+ end
45
+
46
+ if !File.exists?(UFILE)
47
+ File.open(UFILE, "w") do |line|
48
+ line.puts("Solak|Theresa|true|1")
49
+ line.puts("Jones|Bob|false|267")
50
+ line.puts("Smith|Jean|false|4")
51
+ line.puts("Doe|John|false|3")
52
+ end
53
+ end
54
+
55
+ end
56
+
57
+ class MyError < StandardError
58
+ end
59
+
60
+ def self.init_config
61
+ puts "in self.init_config"
62
+ config_values = { :default_file_name => 'default_file.txt', :supported_types => ['txt', 'pdf'] }
63
+ File.open('config.yaml', 'w') do |config|
64
+ config.write config_values.to_yaml
65
+ end
66
+ end
67
+
68
+ def self.configure
69
+ begin
70
+ if File.exists?('config.yaml')
71
+ puts "exists"
72
+ else
73
+ raise LabReservation::MyError, 'missing config file'
74
+ end
75
+ rescue LabReservation::MyError
76
+ puts "doesn't exist, but rescued"
77
+ self.init_config
78
+ end
79
+ puts "this prints in any event"
80
+ puts File.read("config.yaml")
81
+ end
82
+
83
+ end
@@ -0,0 +1,9 @@
1
+ module LabReservation
2
+
3
+ # puts in there to show you what gets loaded first
4
+
5
+ puts "file version.rb required"
6
+
7
+ VERSION = "0.0.4"
8
+
9
+ end
data/lib/machines.rb ADDED
@@ -0,0 +1,118 @@
1
+ #$LOAD_PATH.unshift File.dirname(__FILE__)
2
+ #$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'date'
5
+ require 'requests'
6
+ require 'users'
7
+
8
+ class Machines
9
+
10
+ puts "In Machines"
11
+
12
+ def initialize
13
+ #puts "in initialize"
14
+ #puts "#{MFILE}"
15
+ lines = File.open(MFILE, 'r').readlines
16
+ @entries = lines[0..-1]
17
+ @machines = @entries.collect do |line|
18
+ entry_to_hash line.chomp
19
+ end
20
+ count
21
+ end
22
+
23
+ def fields
24
+ [:mach_id, :req_id]
25
+ end
26
+
27
+ def entry_to_hash line
28
+ values = line.split("|")
29
+ Hash[fields.zip values]
30
+ end
31
+
32
+ def count
33
+ @machines.count
34
+ end
35
+
36
+ def add id
37
+ f = File.open(MFILE, "a")
38
+ @machines.collect do |line|
39
+ if (id == line[:mach_id])
40
+ puts "machine already exists, not added"
41
+ break
42
+ end
43
+ end
44
+ f.write "#{id}|"
45
+ f.close
46
+ initialize
47
+ # @machines.push "#{id}|"
48
+ end
49
+
50
+ def save
51
+ f = File.open(MFILE, "w")
52
+ @machines.collect do |line|
53
+ f.puts "#{line[:mach_id]}|#{line[:req_id]}"
54
+ end
55
+ f.close
56
+ end
57
+
58
+ def show
59
+ puts @machines
60
+ count
61
+ end
62
+
63
+ def find_free date, days
64
+ desired_range = Date.strptime(date, '%m-%d-%Y')..(Date.strptime(date, '%m-%d-%Y') + days.to_i)
65
+ # puts "desired_range: #{desired_range}"
66
+ free_machines = Array.new
67
+ @machines.collect do |line|
68
+ # puts line
69
+ if line[:req_id].nil?
70
+ free_machines.push line[:mach_id]
71
+ else
72
+ avail = true
73
+ r = line[:req_id].split(", ")
74
+ # puts "r: #{r}"
75
+ # account for multiple requests being fulfilled by a given machine
76
+ r.collect do |rq|
77
+ # puts "rq: #{rq}"
78
+ rq_range = Requests.new.req_date_range rq
79
+ # puts "rq_range: #{rq_range}"
80
+ rq_range.map do |date|
81
+ if desired_range === date
82
+ avail = false
83
+ end
84
+ end
85
+ # puts "avail: #{avail}"
86
+ end
87
+ if avail == true
88
+ free_machines.push line[:mach_id]
89
+ end
90
+ end
91
+ end
92
+ free_machines
93
+ end
94
+
95
+ def add_req machine_name, request_id
96
+ f = File.open(MFILE, "w")
97
+ @machines.collect do |line|
98
+ if line[:mach_id] != machine_name
99
+ f.puts "#{line[:mach_id]}|#{line[:req_id]}"
100
+ else
101
+ # update the request id field
102
+ if line[:req_id] == nil
103
+ new_r_id = request_id
104
+ else
105
+ new_r_id = "#{line[:req_id]}, #{request_id}"
106
+ end
107
+ f.puts "#{line[:mach_id]}|#{new_r_id}"
108
+ end
109
+ end
110
+ f.close
111
+ Machines.new.get machine_name
112
+ end
113
+
114
+ def get m
115
+ @machines.find { |i| i[:mach_id] == m }
116
+ end
117
+
118
+ end
data/lib/requests.rb ADDED
@@ -0,0 +1,91 @@
1
+ #$LOAD_PATH.unshift File.dirname(__FILE__)
2
+ #$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ require 'machines'
5
+ require 'users'
6
+
7
+ class Requests < Array
8
+
9
+ puts "In Requests"
10
+
11
+ def initialize
12
+ # puts "in initialize"
13
+ # puts "filename: #{RFILE}"
14
+ lines = File.open(RFILE, 'r').readlines
15
+ @entries = lines[0..-1]
16
+ @requests = @entries.collect do |line|
17
+ entry_to_hash line.chomp
18
+ end
19
+ end
20
+
21
+ def fields
22
+ [:req_id, :user_id, :date_reqd, :days_reqd, :num_machines, :image, :comments]
23
+ end
24
+
25
+ def count
26
+ @requests.count
27
+ end
28
+
29
+ def entry_to_hash line
30
+ values = line.split("|")
31
+ Hash[fields.zip values]
32
+ end
33
+
34
+ def add user, date, days, num, image, comment
35
+ new_req = {:req_id=>"#{id_next}", :user_id=>"#{user}", :date_reqd=>"#{date}", :days_reqd=>"#{days}", :num_machines=>"#{num}", :image=>"#{image}", :comments=>"#{comment}"}
36
+ @requests.push new_req
37
+ save
38
+ count
39
+ end
40
+
41
+ def save
42
+ f = File.open(RFILE, "w")
43
+ @requests.collect do |line|
44
+ f.puts "#{line[:req_id]}|#{line[:user_id]}|#{line[:date_reqd]}|#{line[:days_reqd]}|#{line[:num_machines]}|#{line[:image]}|#{line[:comments]}"
45
+ end
46
+ f.close
47
+ count
48
+ end
49
+
50
+ def id_next
51
+ last_id = 0
52
+ @requests.collect do |line|
53
+ curr_id = line[:req_id]
54
+ if curr_id.to_i > last_id
55
+ last_id = curr_id.to_i
56
+ end
57
+ end
58
+ last_id+1
59
+ end
60
+
61
+ def last
62
+ @requests[-1]
63
+ end
64
+
65
+ def show
66
+ puts "req id | user id | date | # days | # machines | image | comments"
67
+ @requests.collect do |line|
68
+ puts "#{line[:req_id]} | #{line[:user_id]} | #{line[:date_reqd]} | #{line[:days_reqd]} | #{line[:num_machines]} | #{line[:image]} | #{line[:comments]}"
69
+ #if !line[:comments].nil?
70
+ # puts " comments: #{line[:comments]}"
71
+ #end
72
+ end
73
+ count
74
+ end
75
+
76
+ def get_all
77
+ @requests
78
+ end
79
+
80
+ def get r
81
+ @requests.find { |q| q[:req_id] == r }
82
+ end
83
+
84
+ def req_date_range r
85
+ item = get(r)
86
+ days = item[:days_reqd].to_i
87
+ start = Date.strptime(item[:date_reqd], '%m-%d-%Y')
88
+ range = start..(start+days)
89
+ end
90
+
91
+ end
data/lib/users.rb ADDED
@@ -0,0 +1,130 @@
1
+ #$LOAD_PATH.unshift File.dirname(__FILE__)
2
+ #$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+
4
+ class Users
5
+
6
+ # FILENAME="data/sample.txt"
7
+
8
+ def initialize
9
+ lines = File.open(UFILE, 'r').readlines
10
+ @entries = lines[0..-1]
11
+ @usrs = @entries.collect do |line|
12
+ entry_to_hash line
13
+ end
14
+ end
15
+
16
+ def fields
17
+ [:last_name, :first_name, :admin, :id]
18
+ end
19
+
20
+ def entry_to_hash line
21
+ values = line.split("|")
22
+ Hash[fields.zip values]
23
+ end
24
+
25
+ def add l_name, f_name
26
+ f = File.open(UFILE, "a")
27
+ @usrs.collect do |line|
28
+ if (l_name == line[:last_name]) && (f_name == line[:first_name])
29
+ puts "User already exists, not added"
30
+ break
31
+ end
32
+ end
33
+ id_next = id_last + 1
34
+ f.puts "#{l_name}|#{f_name}|false|#{id_next}"
35
+ f.close
36
+ initialize
37
+ end
38
+
39
+ def del l_name, f_name
40
+ f = File.open(UFILE, "w")
41
+ @usrs.collect do |line|
42
+ if (l_name == line[:last_name]) && (f_name == line[:first_name])
43
+ loop
44
+ else
45
+ f.puts "#{line[:last_name]}|#{line[:first_name]}|#{line[:admin]}|#{line[:id]}"
46
+ end
47
+ end
48
+ f.close
49
+ initialize
50
+ end
51
+
52
+ def id_last
53
+ last = 0
54
+ @entries.collect do |line|
55
+ last_name, first_name, admin, id = line.split("|")
56
+ if id.to_i > last
57
+ last = id.to_i
58
+ end
59
+ end
60
+ last
61
+ end
62
+
63
+ def write_new filename
64
+ f = File.open(filename, "w")
65
+ @entries.collect do |line|
66
+ f.puts line
67
+ end
68
+ f.close
69
+ end
70
+
71
+ def is_admin l_name, f_name
72
+ ans = false
73
+ @entries.collect do |line|
74
+ last_name, first_name, admin, id = line.split("|")
75
+ if (l_name == last_name) && (f_name == first_name)
76
+ if admin == "true"
77
+ ans = true
78
+ end
79
+ end
80
+ end
81
+ ans
82
+ end
83
+
84
+ def user_exists l_name, f_name
85
+ ans = false
86
+ @usrs.collect do |line|
87
+ if (l_name == line[:last_name]) && (f_name == line[:first_name])
88
+ ans = true
89
+ end
90
+ end
91
+ ans
92
+ end
93
+
94
+ # def user_exists l_name, f_name
95
+ # ans = false
96
+ # @entries.collect do |line|
97
+ # last_name, first_name, admin, id = line.split("|")
98
+ # if (l_name == last_name) && (f_name == first_name)
99
+ # ans = true
100
+ # end
101
+ # end
102
+ # ans
103
+ # end
104
+
105
+ def name_update old_l_name, f_name, new_l_name
106
+ @usrs.collect do |line|
107
+ if (old_l_name == line[:last_name]) && (f_name == line[:first_name])
108
+ line[:last_name] = new_l_name
109
+ end
110
+ end
111
+ # @entries.collect do |line|
112
+ # last_name, first_name, admin, id = line.split("|")
113
+ # if (old_l_name == last_name) && (f_name == first_name)
114
+ # line = "#{new_l_name}|{first_name}"
115
+ # end
116
+ # end
117
+ # puts @usrs
118
+ save
119
+ end
120
+
121
+ def save
122
+ f = File.open(UFILE, "w")
123
+ @usrs.collect do |line|
124
+ f.puts "#{line[:last_name]}|#{line[:first_name]}|#{line[:admin]}|#{line[:id]}"
125
+ end
126
+ f.close
127
+ end
128
+
129
+
130
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsolak_lab_reservation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Theresa Solak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem to reserve one or more lab machines for one or more days
15
+ email: yarfreese@gmail.com
16
+ executables:
17
+ - lab_reservation
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/lab_reservation.rb
22
+ - lib/requests.rb
23
+ - lib/lab_reservation/version.rb
24
+ - lib/machines.rb
25
+ - lib/users.rb
26
+ - bin/lab_reservation
27
+ homepage: http://rubygems.org/gems/lab_reservation
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 1.8.24
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: Reserve a Lab Machine
51
+ test_files: []
52
+ has_rdoc: