Class: Computer
- Inherits:
-
Object
- Object
- Computer
- Defined in:
- lib/rvpc.rb
Overview
A simple Virtual Private Computer written in ruby. This is the main (and, currently, only) Class. This class supports chaining 👍
Class Method Summary collapse
-
.geach {|creator, filename, contents, time| ... } ⇒ Computer
Do the given block for each file in the global files.
-
.get_gfiles ⇒ Hash
Gets the Hash containing all the global files.
-
.get_users ⇒ Hash
Gets the Hash containing all the users.
-
.gfile(filename) {|creator, contents, time| ... } ⇒ Hash, Computer
Gets the specified global file, can also be used with a yield block.
-
.gopen(username, password, file) ⇒ Computer
Loads the specified JSON file into the global files.
-
.gsave(file) ⇒ Computer
Saves all the global files to a JSON file.
Instance Method Summary collapse
-
#create(filename, contents) ⇒ Computer
Create a new file for the current user.
-
#create_global(password, filename, contents) ⇒ Computer
Create a new global file, accessible by all users.
-
#delete(filename) ⇒ Computer
Deletes a file from the user’s fs.
-
#each {|filename, contents, time| ... } ⇒ Computer
Do the given block for each file in the user’s fs.
-
#get(filename) ⇒ String
Returns the contents of the specified file from the user’s fs.
-
#initialize(username, password) ⇒ Computer
constructor
Create a new Computer object.
-
#user ⇒ String
Return the username for the current user.
Constructor Details
#initialize(username, password) ⇒ Computer
Create a new Computer object
14 15 16 17 18 19 20 21 22 |
# File 'lib/rvpc.rb', line 14 def initialize(username, password) @username = username @sha = Digest::SHA256.hexdigest password @md5 = Digest::MD5.hexdigest password @files = Hash.new @@users[username.to_sym] = Hash.new @@users[username.to_sym][:sha] = @sha @@users[username.to_sym][:md5] = @md5 end |
Class Method Details
.geach {|creator, filename, contents, time| ... } ⇒ Computer
Do the given block for each file in the global files
150 151 152 153 154 155 156 |
# File 'lib/rvpc.rb', line 150 def self.geach @@gfiles.each do |f, h| yield(h["creator"], f, h["contents"], h["time"]) if block_given? puts "#{f}: #{h}" unless block_given? end self end |
.get_gfiles ⇒ Hash
Gets the Hash containing all the global files
95 96 97 |
# File 'lib/rvpc.rb', line 95 def self.get_gfiles @@gfiles unless @@gfiles.nil? end |
.get_users ⇒ Hash
Gets the Hash containing all the users
88 89 90 |
# File 'lib/rvpc.rb', line 88 def self.get_users @@users unless @@users.nil? end |
.gfile(filename) {|creator, contents, time| ... } ⇒ Hash, Computer
Gets the specified global file, can also be used with a yield block
105 106 107 108 109 110 |
# File 'lib/rvpc.rb', line 105 def self.gfile(filename) filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') yield(@@gfiles[filename]["creator"], @@gfiles[filename]["contents"], @@gfiles[filename]["time"]) if block_given? @@gfiles[filename] unless block_given? self unless block_given? end |
.gopen(username, password, file) ⇒ Computer
This replaces the current global files, so be careful!
Loads the specified JSON file into the global files
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rvpc.rb', line 133 def self.gopen(username, password, file) file = file.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') if (Digest::SHA256.hexdigest password) == @@users[username.to_sym][:sha] && (Digest::MD5.hexdigest password) == @@users[username.to_sym][:md5] @@gfiles = JSON.parse(File.read(file)) if File.exists?(file) else puts "Invalid password." end rescue puts "Error reading." ensure self end |
.gsave(file) ⇒ Computer
Saves all the global files to a JSON file
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rvpc.rb', line 116 def self.gsave(file) file = file.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') File.open(file, "w") do |f| f.write(JSON.pretty_generate(@@gfiles)) end rescue puts "Error writing." ensure self end |
Instance Method Details
#create(filename, contents) ⇒ Computer
Create a new file for the current user
34 35 36 37 38 39 40 |
# File 'lib/rvpc.rb', line 34 def create(filename, contents) filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') @files[filename] = Hash.new @files[filename]["contents"] = contents @files[filename]["time"] = Time.now self end |
#create_global(password, filename, contents) ⇒ Computer
Create a new global file, accessible by all users
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rvpc.rb', line 47 def create_global(password, filename, contents) filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') if (Digest::SHA256.hexdigest password) == @sha && (Digest::MD5.hexdigest password) == @md5 @@gfiles[filename] = Hash.new @@gfiles[filename]["creator"] = @username @@gfiles[filename]["contents"] = contents @@gfiles[filename]["time"] = Time.now else puts "Invalid password" end self end |
#delete(filename) ⇒ Computer
Deletes a file from the user’s fs
70 71 72 73 74 |
# File 'lib/rvpc.rb', line 70 def delete(filename) filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') @files.delete(filename) if @files.has_key?(filename) self end |
#each {|filename, contents, time| ... } ⇒ Computer
Do the given block for each file in the user’s fs
162 163 164 165 166 167 |
# File 'lib/rvpc.rb', line 162 def each @files.each do |f, h| yield(f, h["contents"], h["time"]) if block_given? puts "#{f}: #{h}" unless block_given? end end |
#get(filename) ⇒ String
Returns the contents of the specified file from the user’s fs
80 81 82 83 |
# File 'lib/rvpc.rb', line 80 def get(filename) filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_') @files[filename]["contents"] if @files.has_key?(filename).has_key?("contents") end |
#user ⇒ String
Return the username for the current user
63 64 65 |
# File 'lib/rvpc.rb', line 63 def user @username unless @username.nil? end |