Class: Jets::Thor::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/thor/auth.rb

Constant Summary collapse

@@success =

Only check once. Thor subcommands result in 2 calls to Thor dispatch.

false
@@no_auth_command =

Tricky: Thor load the command and then the subcommand. IE: jets generate:event

@args = ["dotenv", "list"] # first pass
@args = ["list"]           # second pass

We only check first pass to see if it is a no_auth_command. And cache it so the second pass never occurs.

nil

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Auth

Returns a new instance of Auth.



3
4
5
# File 'lib/jets/thor/auth.rb', line 3

def initialize(args)
  @args = args
end

Instance Method Details

#api_key?Boolean

interface method

Returns:

  • (Boolean)


25
26
27
# File 'lib/jets/thor/auth.rb', line 25

def api_key?
  Jets::Api::Config.instance.api_key?
end

#check!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/jets/thor/auth.rb', line 9

def check!
  return @@success if @@success
  # Commands that do not require authentication
  return if no_auth_command?

  if api_key?
    resp = ping
    puts resp["message"] unless resp["message"] == "pong"
    @@success = true
  else
    
    exit 1
  end
end

#handle_unauthorized(e) ⇒ Object



91
92
93
94
# File 'lib/jets/thor/auth.rb', line 91

def handle_unauthorized(e)
  puts "Unauthorized: #{e.message}".color(:red)
  exit 1
end

#login_help_messageObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jets/thor/auth.rb', line 79

def 
  puts <<~EOL
    An account is required to use Jets.
    You can sign up at https://www.rubyonjets.com

    Please login. You can login with

        jets login

  EOL
end

#no_auth_command?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
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
77
# File 'lib/jets/thor/auth.rb', line 41

def no_auth_command?
  return @@no_auth_command unless @@no_auth_command.nil?
  base = %w[
    generate
    init
  ]
  more = %w[
    ci:info
    ci:init
    ci:logs
    ci:start
    ci:status
    ci:stop
    clean
    concurrency
    curl
    dotenv
    env
    exec
    functions
    funs
    logs
    maintenance
    schedule
    waf:info
    waf:init
  ]
  commands = base + more
  commands += ProjectCheck.new(@args).no_project_commands
  commands.uniq!
  # @args contains the command and subcommand
  # IE: jets ci:init => ["ci", "init"]

  @@no_auth_command = (commands & @args).any? ||  # IE: ["ci"] & ["ci", "init"]
    (commands & [@args.join(":")]).any? ||        # IE: ["ci:init"] & ["ci:init"]
    @args.empty?
end

#pingObject

interface method



30
31
32
# File 'lib/jets/thor/auth.rb', line 30

def ping
  Jets::Api::Ping.create
end