Module: Tokenifier::Cli

Extended by:
Cli
Included in:
Cli
Defined in:
lib/tokenifier/cli.rb

Instance Method Summary collapse

Instance Method Details

#optionsObject



7
8
9
# File 'lib/tokenifier/cli.rb', line 7

def options
  @options ||= {}
end

#optparseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tokenifier/cli.rb', line 11

def optparse
  OptionParser.new do |opts|
    opts.banner =<<-USAGE

Usage:

tokenifier [options] COMMAND 'custom string'

Commands:

s|secret - Generates secret string
e|encrypt - Does data encryption of any string data
d|decrypt - Does data decryption from hashed data.

NOTE: You have to use a permanent secret to decrypt a data.
      Tokinifier generates random secret string each execution time instead.

Examples:

 tokenifier encrypt "CUSTOM DATA"
 tokenifier decrypt "CUSTOM DATA"

 tokenifier --secret MYSECRET e "CUSTOM DATA"
 tokenifier --secret MYSECRET d "ENCRYPTED DATA"


USAGE

    opts.on('-s', '--secret SECRET', 'Using custom secret phrase') do |secret|
      options[:secret] = secret
    end

    opts.on('-h', '--help', 'Display this screen') do
      puts opts
      exit
    end
  end
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tokenifier/cli.rb', line 50

def run
  optparse.parse!

  begin
    case ARGV.first
      when /^e/
        if options[:secret].nil?
          options[:secret] = Tokenifier::Random.secret
          puts "SECRET: #{options[:secret]}"
        end

        puts Tokenifier.encrypt(ARGV.last, options)
      when /^d/
        puts Tokenifier.decrypt(ARGV.last, options)
      when /^s/
        puts Tokenifier::Random.secret
      else
        puts optparse.help
        exit
    end
  rescue Tokenifier::Error => e
    puts "ERROR: Token processing error."
    puts optparse.help
    exit
  end

end