Class: Sym::App::Input::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/sym/app/input/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin = $stdin, stdout = $stdout, stderr = $stderr, kernel = nil) ⇒ Handler

Returns a new instance of Handler.



9
10
11
12
13
14
# File 'lib/sym/app/input/handler.rb', line 9

def initialize(stdin = $stdin, stdout = $stdout, stderr = $stderr, kernel = nil)
  self.stdin  = stdin
  self.stdout = stdout
  self.stderr = stderr
  self.kernel = kernel
end

Instance Attribute Details

#kernelObject

Returns the value of attribute kernel.



7
8
9
# File 'lib/sym/app/input/handler.rb', line 7

def kernel
  @kernel
end

#stderrObject

Returns the value of attribute stderr.



7
8
9
# File 'lib/sym/app/input/handler.rb', line 7

def stderr
  @stderr
end

#stdinObject

Returns the value of attribute stdin.



7
8
9
# File 'lib/sym/app/input/handler.rb', line 7

def stdin
  @stdin
end

#stdoutObject

Returns the value of attribute stdout.



7
8
9
# File 'lib/sym/app/input/handler.rb', line 7

def stdout
  @stdout
end

Instance Method Details

#askObject



16
17
18
19
20
21
22
23
# File 'lib/sym/app/input/handler.rb', line 16

def ask
  retries ||= 0
  prompt('Password: ', :green)
rescue ::OpenSSL::Cipher::CipherError
  stderr.puts 'Invalid password. Please try again.'
  retry if (retries += 1) < 3
  nil
end

#highline(message, color) ⇒ Object



36
37
38
# File 'lib/sym/app/input/handler.rb', line 36

def highline(message, color)
  HighLine.new(stdin, stderr).ask(message.bold) { |q| q.echo = ''.send(color) }
end

#new_passwordObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sym/app/input/handler.rb', line 40

def new_password
  password = prompt('New Password     :  ', :blue)

  raise Sym::Errors::PasswordTooShort.new(
    'Minimum length is 7 characters.') if password.length < 7

  password_confirm = prompt('Confirm Password :  ', :blue)

  raise Sym::Errors::PasswordsDontMatch.new(
    'The passwords you entered do not match.') if password != password_confirm

  password
end

#prompt(message, color) ⇒ Object



29
30
31
32
33
34
# File 'lib/sym/app/input/handler.rb', line 29

def prompt(message, color)
  unless $stdin.isatty && $stdin.tty?
    raise Sym::Errors::CantReadPasswordNoTTY.new('key requires a password, however STDIN is not a TTY')
  end
  highline(message, color)
end

#puts(*args) ⇒ Object



25
26
27
# File 'lib/sym/app/input/handler.rb', line 25

def puts(*args)
  stderr.puts args
end