class Bnet::Commands::TokenCommand

Public Instance Methods

description() click to toggle source
# File lib/bnet/commands/token.rb, line 9
def description
  "Print current token for giving secret."
end
extra_params() click to toggle source
# File lib/bnet/commands/token.rb, line 13
def extra_params
  "[-r] [--repeat] <secret>"
end
run() click to toggle source
# File lib/bnet/commands/token.rb, line 24
def run
  secret = @args.shift

  token, next_timestamp = Authenticator.get_token(secret)

  if @options.repeat
    interrupted = false
    trap("INT") { interrupted = true } # traps Ctrl-C

    until interrupted do
      sleep 1

      if Time.now.getutc.to_i < next_timestamp
        seconds = next_timestamp - Time.now.getutc.to_i
        h, c = color_of(seconds)
        puts "\e[s\e[%d;%dm\e[5m%02d\e[25m\t->\t%s\t<-\e[1A\e[0m\e[u" % [h, c, seconds, token]
        next
      end

      token, next_timestamp = Authenticator.get_token(secret)
    end
  else
    puts token
  end
end
setup_opts(opts) click to toggle source
# File lib/bnet/commands/token.rb, line 17
def setup_opts(opts)
  @options.repeat = false
  opts.on("-r", "--repeat", "Keep printing updated token") do
    @options.repeat = true
  end
end

Private Instance Methods

color_of(seconds) click to toggle source
# File lib/bnet/commands/token.rb, line 52
def color_of(seconds)
  case
    when seconds > 25 then h, c = 1, 32
    when seconds > 20 then h, c = 0, 32
    when seconds > 15 then h, c = 1, 33
    when seconds > 10 then h, c = 0, 33
    when seconds > 5  then h, c = 0, 31
    else
      h, c = 1, 31
  end

  [h, c]
end