Class: Dydra::Command::Login

Inherits:
Dydra::Command show all
Defined in:
lib/dydra/command/login.rb

Overview

Logins and caches credentials locally.

Instance Method Summary (collapse)

Methods inherited from Dydra::Command

#abort, #basename, #catch_errors, #debug?, #initialize, #parse_repository_specs, #parse_resource_specs, #require_gem!, #stderr, #stdout, #validate_repository_specs, #validate_resource_specs, #verbose?, #wrap_errors

Methods included from Dydra::Client

authenticate!, credentials_file, delete, get, get_json, post, reset!, resource, rpc, setup?

Constructor Details

This class inherits a constructor from Dydra::Command

Instance Method Details

- (Object) ask_for_pass



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dydra/command/login.rb', line 41

def ask_for_pass
  print "Password: "
  begin
    system "stty -echo"
    password = $stdin.gets.strip
    rescue Exception => e
      raise e
    ensure
      system "stty echo"
    end
  puts
  password
end

- (Object) ask_for_user



36
37
38
39
# File 'lib/dydra/command/login.rb', line 36

def ask_for_user
  print "Email or username: "
  $stdin.gets.strip
end

- execute(given_user = nil, given_pass = nil)

This method returns an undefined value.

Parameters:

  • account_name (String)
  • password (String)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dydra/command/login.rb', line 10

def execute(given_user = nil, given_pass = nil)
  while !Dydra::Client.setup?
    Dydra::Client.reset!
    user = given_user
    pass = given_pass
    begin
      user = ask_for_user if user.nil?
      raise AuthenticationError, "Username cannot be blank" if user.nil? || user.empty?
      pass = ask_for_pass if pass.nil?
      Dydra::Client.setup!(:user => user, :pass => pass)
      token = Account.new(user).info['authentication_token']
      raise AuthenticationError, "Incorrect password" if token.nil?
    rescue RestClient::ResourceNotFound
      puts "User #{user} not found"
    rescue SignalException, SystemExit => e
      # Special case ctrl-c at the command line
      Kernel.abort
    rescue Exception => e
      puts "Invalid credentials: #{e.message}"
    end
    Dydra::Client.reset! unless token
  end
  save_credentials(user, token)
  puts "Credentials saved to #{Dydra::Client.credentials_file}" if verbose?
end

- (Object) save_credentials(user, token)



55
56
57
58
59
# File 'lib/dydra/command/login.rb', line 55

def save_credentials(user, token)
  require 'yaml'
  Dir.mkdir(File.dirname(Dydra::Client.credentials_file)) unless File.exists?(File.dirname(Dydra::Client.credentials_file))
  File.open(Dydra::Client.credentials_file, 'w+') { |f| f.write({ :user => user, :token => token }.to_yaml) }
end