Class: Computer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Computer

Create a new Computer object

Parameters:

  • username (String)

    The username to create the new Computer with

  • password (String)

    The password to assign to the username, the password's SHA and MD5 are what's stored


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

Yields:

  • (creator, filename, contents, time)

    Yield block to do for each file

Returns:

  • (Computer)

    Returns the current object (for chaining)


145
146
147
148
149
150
151
# File 'lib/rvpc.rb', line 145

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_gfilesHash

Gets the Hash containing all the global files

Returns:

  • (Hash)

    Hash containing all the global files


93
94
95
# File 'lib/rvpc.rb', line 93

def self.get_gfiles
  @@gfiles unless @@gfiles.nil?
end

.get_usersHash

Gets the Hash containing all the users

Returns:

  • (Hash)

    Hash containing all the users


86
87
88
# File 'lib/rvpc.rb', line 86

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

Parameters:

  • filename (String)

    The filename of the global file to return

Yields:

  • (creator, contents, time)

    Optional yield block for the file

Returns:

  • (Hash, Computer)

    If a block is given, it returns self, otherwise it returns the file Hash


103
104
105
106
107
# File 'lib/rvpc.rb', line 103

def self.gfile(filename)
  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

Note:

This replaces the current global files, so be careful!

Loads the specified JSON file into the global files

Parameters:

  • username (String)

    sudo-like verification

  • password (String)

    sudo-like verification

Returns:

  • (Computer)

    Returns the current object (for chaining)


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rvpc.rb', line 129

def self.gopen(username, password, file)
  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

Parameters:

  • file (String)

    The relative path of the file, including the filename

Returns:

  • (Computer)

    Returns the current object (for chaining)


113
114
115
116
117
118
119
120
121
# File 'lib/rvpc.rb', line 113

def self.gsave(file)
  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

Parameters:

  • filename (String)

    The filename for the new file

  • contents (String)

    The contents to dump into the new file

Returns:

  • (Computer)

    Returns the current object (for chaining)


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

Parameters:

  • password (String)

    Used sort of like sudo for writing to /

  • filename (String)

    The filename for the new file

  • contents (String)

    The contents to dump into the new file

Returns:

  • (Computer)

    Returns the current object (for chaining)


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

Returns:

  • (Computer)

    Returns the current object (for chaining)


70
71
72
73
# File 'lib/rvpc.rb', line 70

def delete(filename)
  @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

Yields:

  • (filename, contents, time)

    Yield block to do for each file

Returns:

  • (Computer)

    Returns the current object (for chaining)


157
158
159
160
161
162
# File 'lib/rvpc.rb', line 157

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

Parameters:

  • filename (String)

    The filename of the file to return

Returns:

  • (String)

    The contents of the selected file


79
80
81
# File 'lib/rvpc.rb', line 79

def get(filename)
  @files[filename]["contents"] if @files.has_key?(filename).has_key?("contents")
end

#userString

Return the username for the current user

Returns:

  • (String)

    The current user's username


63
64
65
# File 'lib/rvpc.rb', line 63

def user
  @username unless @username.nil?
end