Class: Shhh::Application

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Application

Returns a new instance of Application.



15
16
17
18
19
20
21
22
# File 'lib/shhh/application.rb', line 15

def initialize(opts)
  self.opts      = opts
  self.opts_hash = opts.respond_to?(:to_hash) ? opts.to_hash : opts
  self.args      = ::Shhh::App::Args.new(opts_hash)
  initialize_input_handler
  initialize_key_handler
  initialize_action
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



6
7
8
# File 'lib/shhh/application.rb', line 6

def action
  @action
end

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/shhh/application.rb', line 6

def args
  @args
end

#input_handlerObject

Returns the value of attribute input_handler.



6
7
8
# File 'lib/shhh/application.rb', line 6

def input_handler
  @input_handler
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/shhh/application.rb', line 6

def key
  @key
end

#key_handlerObject

Returns the value of attribute key_handler.



6
7
8
# File 'lib/shhh/application.rb', line 6

def key_handler
  @key_handler
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/shhh/application.rb', line 6

def opts
  @opts
end

#opts_hashObject

Returns the value of attribute opts_hash.



6
7
8
# File 'lib/shhh/application.rb', line 6

def opts_hash
  @opts_hash
end

#resultObject

Returns the value of attribute result.



6
7
8
# File 'lib/shhh/application.rb', line 6

def result
  @result
end

Instance Method Details

#commandObject



62
63
64
65
# File 'lib/shhh/application.rb', line 62

def command
  @command_class ||= Shhh::App::Commands.find_command_class(opts)
  @command       ||= @command_class.new(self) if @command_class
end

#editorObject



67
68
69
# File 'lib/shhh/application.rb', line 67

def editor
  ENV['EDITOR'] || '/bin/vi'
end

#error(hash) ⇒ Object



71
72
73
# File 'lib/shhh/application.rb', line 71

def error(hash)
  hash
end

#executeObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shhh/application.rb', line 45

def execute
  execute!

rescue ::OpenSSL::Cipher::CipherError => e
  error type:      'Cipher Error',
        details:   e.message,
        reason:    'Perhaps either the secret is invalid, or encrypted data is corrupt.',
        exception: e

rescue Shhh::Errors::Error => e
  error type:    e.class.name.split(/::/)[-1],
        details: e.message

rescue StandardError => e
  error exception: e
end

#execute!Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/shhh/application.rb', line 32

def execute!
  if args.do_options_require_key? || args.do_options_specify_key?
    self.key = Shhh::App::PrivateKey::Handler.new(opts, input_handler).key
    raise Shhh::Errors::NoPrivateKeyFound.new('Private key is required') unless self.key
  end

  unless command
    raise Shhh::Errors::InsufficientOptionsError.new(
      'Can not determine what to do from the options ' + opts_hash.keys.reject { |k| !opts[k] }.to_s)
  end
  self.result = command.execute
end

#initialize_actionObject



24
25
26
27
28
29
30
# File 'lib/shhh/application.rb', line 24

def initialize_action
  self.action = if opts[:encrypt] then
                  :encr
                elsif opts[:decrypt]
                  :decr
                end
end

#initialize_input_handler(handler = ::Shhh::App::Input::Handler.new) ⇒ Object



75
76
77
# File 'lib/shhh/application.rb', line 75

def initialize_input_handler(handler = ::Shhh::App::Input::Handler.new)
  self.input_handler = handler
end

#initialize_key_handlerObject



79
80
81
# File 'lib/shhh/application.rb', line 79

def initialize_key_handler
  self.key_handler = ::Shhh::App::PrivateKey::Handler.new(self.opts, input_handler)
end