Class: B64File

Inherits:
Object
  • Object
show all
Defined in:
lib/B64.rb

Overview

Base64 encoder/decoder to/from file

Instance Method Summary collapse

Constructor Details

#initializeB64File

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

#optsObject



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

#startObject



17
18
19
20
21
22
23
24
# 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

#timeObject



13
14
15
# File 'lib/B64.rb', line 13

def time
  @time = Time.now.strftime('%Y-%m-%d_%H-%M-%S').to_s
end