rvpc 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rvpc.rb +163 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed71842d1d6784991e0fef8ef5f2f476f4862e08
4
+ data.tar.gz: 232b7703d5f0de1811b48e881902d423e9d495d3
5
+ SHA512:
6
+ metadata.gz: 4097b9dfca1af73bc42f6cb5070cec47806ccf5b6b05be2c6709f22cbc62c9ccedba9a975224851cb440cbae3e5346bf29ecde69f87e243d7a0726517ccc3f79
7
+ data.tar.gz: 4956c099078ed16bc8c74e220cdaa2704a40767bd07a6cd52402164dc3e80a461a0adc07cf466f8be573adf0a8778381d4a9fbc2e1bc10c6b6c1bd92160456ba
data/lib/rvpc.rb ADDED
@@ -0,0 +1,163 @@
1
+ require 'digest'
2
+ require 'json'
3
+
4
+ # A simple Virtual Private Computer written in ruby.
5
+ # This is the main (and, currently, only) Class.
6
+ # This class supports chaining 👍
7
+
8
+ class Computer
9
+ # Create a new Computer object
10
+ #
11
+ # @param username [String] The username to create the new Computer with
12
+ # @param password [String] The password to assign to the username, the password's SHA and MD5 are what's stored
13
+ # @return [Computer] A brand-spankin'-new Computer object, with username and password
14
+ def initialize(username, password)
15
+ @username = username
16
+ @sha = Digest::SHA256.hexdigest password
17
+ @md5 = Digest::MD5.hexdigest password
18
+ @files = Hash.new
19
+ @@users[username.to_sym] = Hash.new
20
+ @@users[username.to_sym][:sha] = @sha
21
+ @@users[username.to_sym][:md5] = @md5
22
+ end
23
+
24
+ private
25
+ @@users = Hash.new(Hash.new(0))
26
+ @@gfiles = Hash.new(Hash.new(0))
27
+
28
+ public
29
+ # Create a new file for the current user
30
+ #
31
+ # @param filename [String] The filename for the new file
32
+ # @param contents [String] The contents to dump into the new file
33
+ # @return [Computer] Returns the current object (for chaining)
34
+ def create(filename, contents)
35
+ filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
36
+ @files[filename] = Hash.new
37
+ @files[filename]["contents"] = contents
38
+ @files[filename]["time"] = Time.now
39
+ self
40
+ end
41
+
42
+ # Create a new global file, accessible by all users
43
+ #
44
+ # @param password [String] Used sort of like +sudo+ for writing to +/+
45
+ # @param (see #create)
46
+ # @return (see #create)
47
+ def create_global(password, filename, contents)
48
+ filename = filename.gsub(/[\x00\/\\:\*\?\"<>\|]/, '_')
49
+ if (Digest::SHA256.hexdigest password) == @sha && (Digest::MD5.hexdigest password) == @md5
50
+ @@gfiles[filename] = Hash.new
51
+ @@gfiles[filename]["creator"] = @username
52
+ @@gfiles[filename]["contents"] = contents
53
+ @@gfiles[filename]["time"] = Time.now
54
+ else
55
+ puts "Invalid password"
56
+ end
57
+ self
58
+ end
59
+
60
+ # Return the username for the current user
61
+ #
62
+ # @return [String] The current user's username
63
+ def user
64
+ @username unless @username.nil?
65
+ end
66
+
67
+ # Deletes a file from the user's fs
68
+ #
69
+ # @return (see #create)
70
+ def delete(filename)
71
+ @files.delete(filename) if @files.has_key?(filename)
72
+ self
73
+ end
74
+
75
+ # Returns the contents of the specified file from the user's fs
76
+ #
77
+ # @param filename [String] The filename of the file to return
78
+ # @return [String] The contents of the selected file
79
+ def get(filename)
80
+ @files[filename]["contents"] if @files.has_key?(filename).has_key?("contents")
81
+ end
82
+
83
+ # Gets the Hash containing all the users
84
+ #
85
+ # @return [Hash] Hash containing all the users
86
+ def self.get_users
87
+ @@users unless @@users.nil?
88
+ end
89
+
90
+ # Gets the Hash containing all the global files
91
+ #
92
+ # @return [Hash] Hash containing all the global files
93
+ def self.get_gfiles
94
+ @@gfiles unless @@gfiles.nil?
95
+ end
96
+
97
+ # Gets the specified global file, can also be used with a
98
+ # yield block
99
+ #
100
+ # @param filename [String] The filename of the global file to return
101
+ # @return [Hash, Computer] If a block is given, it returns +self+, otherwise it returns the file Hash
102
+ # @yield [creator, contents, time] Optional yield block for the file
103
+ def self.gfile(filename)
104
+ yield(@@gfiles[filename]["creator"], @@gfiles[filename]["contents"], @@gfiles[filename]["time"]) if block_given?
105
+ @@gfiles[filename] unless block_given?
106
+ self unless block_given?
107
+ end
108
+
109
+ # Saves all the global files to a JSON file
110
+ #
111
+ # @param file [String] The relative path of the file, including the filename
112
+ # @return (see #create)
113
+ def self.gsave(file)
114
+ File.open(file, "w") do |f|
115
+ f.write(JSON.pretty_generate(@@gfiles))
116
+ end
117
+ rescue
118
+ puts "Error writing."
119
+ ensure
120
+ self
121
+ end
122
+
123
+ # Loads the specified JSON file into the global files
124
+ #
125
+ # @note This replaces the current global files, so be careful!
126
+ # @param username [String] +sudo+-like verification
127
+ # @param password [String] +sudo+-like verification
128
+ # @return (see #create)
129
+ def self.gopen(username, password, file)
130
+ if (Digest::SHA256.hexdigest password) == @@users[username.to_sym][:sha] && (Digest::MD5.hexdigest password) == @@users[username.to_sym][:md5]
131
+ @@gfiles = JSON.parse(File.read(file)) if File.exists?(file)
132
+ else
133
+ puts "Invalid password."
134
+ end
135
+ rescue
136
+ puts "Error reading."
137
+ ensure
138
+ self
139
+ end
140
+
141
+ # Do the given block for each file in the global files
142
+ #
143
+ # @yield [creator, filename, contents, time] Yield block to do for each file
144
+ # @return (see #create)
145
+ def self.geach
146
+ @@gfiles.each do |f, h|
147
+ yield(h["creator"], f, h["contents"], h["time"]) if block_given?
148
+ puts "#{f}: #{h}" unless block_given?
149
+ end
150
+ self
151
+ end
152
+
153
+ # Do the given block for each file in the user's fs
154
+ #
155
+ # @yield [filename, contents, time] Yield block to do for each file
156
+ # @return (see #create)
157
+ def each
158
+ @files.each do |f, h|
159
+ yield(f, h["contents"], h["time"]) if block_given?
160
+ puts "#{f}: #{h}" unless block_given?
161
+ end
162
+ end
163
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rvpc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ariel Abreu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple Virtual Private Computer written in ruby with virtual memory
14
+ files. Currently in development.
15
+ email: facekapow2002@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/rvpc.rb
21
+ homepage: http://rubygems.org/gems/rvpc
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.0.14
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A Ruby VPC
45
+ test_files: []
46
+ has_rdoc: