Class: B64File
- Inherits:
-
Object
- Object
- B64File
- Defined in:
- lib/B64.rb
Overview
Base64 encoder/decoder to/from file
Instance Method Summary collapse
- #add(file = @opts[:add]) ⇒ Object
- #decode(file = @opts[:read]) ⇒ Object
- #delete(file = @opts[:delete]) ⇒ Object
- #encode(file = @opts[:write]) ⇒ Object
-
#initialize ⇒ B64File
constructor
A new instance of B64File.
- #opts ⇒ Object
- #start ⇒ Object
- #time ⇒ Object
Constructor Details
#initialize ⇒ B64File
Returns a new instance of B64File.
8 9 10 11 |
# File 'lib/B64.rb', line 8 def initialize start opts end |
Instance Method Details
#add(file = @opts[:add]) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/B64.rb', line 38 def add(file = @opts[:add]) print '>' addition = gets.chomp @encode = Base64.urlsafe_encode64(addition) open(file, 'a+') { |a| a.puts "\n" + @encode.to_s } end |
#decode(file = @opts[:read]) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/B64.rb', line 57 def decode(file = @opts[:read]) open(file).readlines.each do |line| @decode = Base64.decode64(line) puts @decode end end |
#delete(file = @opts[:delete]) ⇒ Object
53 54 55 |
# File 'lib/B64.rb', line 53 def delete(file = @opts[:delete]) File.delete(file) end |
#encode(file = @opts[:write]) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/B64.rb', line 45 def encode(file = @opts[:write]) unless File.zero?(time) @encode = Base64.urlsafe_encode64(file.to_s) open(time.to_s, 'w+') { |w| w.write(@encode.to_s) } return File.delete(time) if File.zero?(time) end end |
#opts ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/B64.rb', line 26 def opts if @opts[:write] encode elsif @opts[:read] decode elsif @opts[:delete] delete elsif @opts[:add] add end end |
#start ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/B64.rb', line 17 def start @opts = Trollop. do opt :write, 'write to file', type: :string opt :read, 'read file', type: :string opt :delete, 'delete file', type: :string opt :add, 'add to file', type: :string end end |
#time ⇒ Object
13 14 15 |
# File 'lib/B64.rb', line 13 def time @time = Time.now.strftime('%Y-%m-%d_%H-%M-%S').to_s end |