class B64File

Base64 encoder/decoder to/from file

Public Class Methods

new() click to toggle source
# File lib/B64.rb, line 8
def initialize
  start
  opts
end

Public Instance Methods

add(file = @opts[:add]) click to toggle source
# 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]) click to toggle source
# 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]) click to toggle source
# File lib/B64.rb, line 53
def delete(file = @opts[:delete])
  File.delete(file)
end
encode(file = @opts[:write]) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/B64.rb, line 17
def start
  @opts = Trollop.options 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() click to toggle source
# File lib/B64.rb, line 13
def time
  @time = Time.now.strftime('%Y-%m-%d_%H-%M-%S').to_s
end