Class: Login

Inherits:
Object
  • Object
show all
Defined in:
lib/canzea/commands/login.rb

Instance Method Summary collapse

Instance Method Details

#do(name, password) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/canzea/commands/login.rb', line 14

def do(name, password)
    credFile = "#{Dir.home}/.canzearc"

    uri = URI(Canzea::config[:canzea_platform_uri] + "/api/x/user/login")

    req = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
    req.body = {name: name, password: password}.to_json

    https = Net::HTTP.new(uri.hostname,uri.port)
    https.use_ssl = uri.instance_of? URI::HTTPS

    res = https.request(req)

    case res
        when Net::HTTPSuccess, Net::HTTPRedirection
            token = JSON.parse(res.body)

            File.open(credFile, 'w') { |file| file.write(token['token']) }
            puts "Login successful.  Authorization cached."
        else
          puts res.body
    end

end

#getObject



45
46
47
48
49
# File 'lib/canzea/commands/login.rb', line 45

def get()
    credFile = "#{Dir.home}/.canzearc"
    token = File.read(credFile)
    return token
end

#logoutObject



39
40
41
42
43
# File 'lib/canzea/commands/login.rb', line 39

def logout()
    credFile = "#{Dir.home}/.canzearc"
    File.delete(credFile)
    puts "Authorization cache cleared."
end