Class: Mu::Command::API

Inherits:
Mu::Command show all
Defined in:
lib/mu/command/api.rb

Constant Summary

Constants inherited from Mu::Command

Api

Constants included from Helper

Helper::ESCAPES

Instance Attribute Summary collapse

Attributes inherited from Mu::Command

#options, #opts

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

#ask, #bin2hex, #error, #escape, #format_float, #get_file_as_string_array, #make_xml, #msg, #shift, #to_boolean

Instance Attribute Details

#credentialsObject

Returns the value of attribute credentials.



4
5
6
# File 'lib/mu/command/api.rb', line 4

def credentials
  @credentials
end

Class Method Details

.clientObject



85
86
87
# File 'lib/mu/command/api.rb', line 85

def self.client
    self.instance.client
end

.instanceObject



81
82
83
# File 'lib/mu/command/api.rb', line 81

def self.instance
    @instance ||= API.new
end

Instance Method Details

#ask_for_credentialsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mu/command/api.rb', line 49

def ask_for_credentials
    msg "Enter your credentials."
    print "User-ID: "
    user = ask
    print "API-Key: "
    apik = ask
    apik2 = Mu::Client.new(user, apik, host).['api_key']
    if not apik2
        error "Authentication failed"
        exit 1
    end
    
    [ user, apik2 ]
end

#clientObject



14
15
16
17
# File 'lib/mu/command/api.rb', line 14

def client
    get_credentials
    Mu::Client.new(user, password, host)
end

#cmd_init(argv) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/mu/command/api.rb', line 6

def cmd_init argv
    FileUtils.rm credentials_file rescue nil
    API.client
    
    msg "You are now ready to use mu!"
    msg "Try mu help to learn more about the commands."
end

#credentials_fileObject



33
34
# File 'lib/mu/command/api.rb', line 33

def credentials_file
end

#get_credentialsObject



36
37
38
39
40
41
42
43
# File 'lib/mu/command/api.rb', line 36

def get_credentials
    return if @credentials
    unless @credentials = read_credentials
        @credentials = ask_for_credentials
        save_credentials
    end
    @credentials
end

#hostObject



29
30
31
# File 'lib/mu/command/api.rb', line 29

def host
    ENV['MU_IP'] || ''
end

#passwordObject



24
25
26
27
# File 'lib/mu/command/api.rb', line 24

def password
    get_credentials
    @credentials[1]
end

#read_credentialsObject



45
46
47
# File 'lib/mu/command/api.rb', line 45

def read_credentials
    File.exists?(credentials_file) and File.read(credentials_file).split("\n")        
end

#save_credentialsObject



64
65
66
# File 'lib/mu/command/api.rb', line 64

def save_credentials
    write_credentials
end

#set_credentials_permissionsObject



76
77
78
79
# File 'lib/mu/command/api.rb', line 76

def set_credentials_permissions
    FileUtils.chmod 0700, File.dirname(credentials_file)
    FileUtils.chmod 0600, credentials_file        
end

#userObject



19
20
21
22
# File 'lib/mu/command/api.rb', line 19

def user
    get_credentials
    @credentials[0]
end

#write_credentialsObject



68
69
70
71
72
73
74
# File 'lib/mu/command/api.rb', line 68

def write_credentials
    FileUtils.mkdir_p(File.dirname(credentials_file))
    File.open(credentials_file, 'w') do |f|
      f.puts self.credentials
    end
    set_credentials_permissions
end