Class: StormForge::Thor::Main

Inherits:
Base
  • Object
show all
Defined in:
lib/thor/main.rb

Instance Method Summary collapse

Instance Method Details

#authObject



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
49
50
51
52
53
54
55
# File 'lib/thor/main.rb', line 19

def auth
  error "WARNING! Disabling SSL certificate verification is not recommended. Proceed with caution!" if options[:skip_ssl_verify]
  email, password = options[:email], options[:password]

  email = ask("Please enter your email:") unless email
  raise Thor::Error, "You must enter a value for that field." if email.empty?

  # TODO: replace with ask("...", :echo => false) if thor gets a new release
  print "Please enter your password: "
  password = STDIN.noecho(&:gets).chomp unless password
  print "\n"

  raise Thor::Error, "You must enter a value for that field." if password.empty?

  say "Attempting to aquire API token..."

  token = StormForge::Client.new(dev_mode: options[:development], skip_ssl_verify: options[:skip_ssl_verify]).aquire_api_token(email, password)
  raise Thor::Error, "Authentication error!" unless token

  say "Your API authentication is: #{token}", :yellow
  unless options[:authfile] == "-"
    say "Writing/Updating credentials to #{authfile}"
    File.open(authfile, "w+") do |file|
      file.puts({ email: email, authentication_token: token, created_at: Time.now }.to_json)
    end
    File.chmod(0600, authfile)
  end
rescue Faraday::Error::ConnectionFailed => e
  p e.message
  raise Thor::Error, "StormForger API not available :-/ (connection failed)"
rescue Faraday::Error::ClientError => e
  if e.response[:status] == 401
    handle_authentication_error(e)
  else
    raise e
  end
end

#curlObject



64
65
66
# File 'lib/thor/main.rb', line 64

def curl
  say "curl --user '#{authentication["email"]}:#{authentication["authentication_token"]}' -X GET #{client.url_base}"
end

#httpieObject



69
70
71
# File 'lib/thor/main.rb', line 69

def httpie
  say "http --auth '#{authentication["email"]}:#{authentication["authentication_token"]}' GET #{client.url_base}"
end

#pingObject



58
59
60
61
# File 'lib/thor/main.rb', line 58

def ping
  result = client.ping
  say "pong" if result.status < 399
end

#versionObject



12
13
14
# File 'lib/thor/main.rb', line 12

def version
  say "stormforge-ruby version #{StormForge::VERSION}"
end