Class: Lastpass::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lastpass-api/client.rb

Overview

Main class to interact with Lastpass API

Instance Method Summary collapse

Constructor Details

#initialize(verbose: false) ⇒ Client

Returns a new instance of Client.

Examples:

Without verbose

require 'lastpass-api'
@lastpass = Lastpass::Client.new

With verbose mode on

require 'lastpass-api'
@lastpass = Lastpass::Client.new( verbose: true )

Parameters:

  • verbose (Boolean) (defaults to: false)

    Set if you want to turn on verbose mode. Turning on verbose will show much more output. This is good for debugging. It will output any commands that are executed with “lpass”. (default: false)



15
16
17
# File 'lib/lastpass-api/client.rb', line 15

def initialize( verbose: false )
  Lastpass.verbose = verbose
end

Instance Method Details

#accountsLastpass::Accounts

Interface to interacting with Lastpass accounts

Returns:



76
77
78
# File 'lib/lastpass-api/client.rb', line 76

def accounts
  Accounts.new
end

#groupsLastpass::Groups

Interface to interacting with Lastpass groups

Returns:



83
84
85
# File 'lib/lastpass-api/client.rb', line 83

def groups
  Groups.new
end

#inspectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Hide instance variables and values



90
91
92
93
# File 'lib/lastpass-api/client.rb', line 90

def inspect
  original_inspect = super
  original_inspect.split( ' ' ).first << '>'
end

#logged_in?Boolean

Check to see if currently logged into Lastpass

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/lastpass-api/client.rb', line 60

def logged_in?
  Cli.status.include? 'Logged in'
rescue
  false
end

#logged_out?Boolean

Check to see if logged out of Lastpass

Returns:

  • (Boolean)


69
70
71
# File 'lib/lastpass-api/client.rb', line 69

def logged_out?
  !logged_in?
end

#login(email:, password:) ⇒ Boolean

Note:

This is not thread safe. Only one login session can be active at a time.

Note:

If there is a valid active login session already when this is called, it will use that session rather than create a new one.

Login to Lastpass

Examples:

@lastpass.( email: '[email protected]', password: 'secret' )
puts @lastpass.logged_in?

Parameters:

  • email (String)

    Lastpass master email

  • password (String)

    Lastpass master password

Returns:

  • (Boolean)

    if login was successful

Raises:

  • (RuntimeError)

    if login failed



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lastpass-api/client.rb', line 31

def ( email:, password: )
  if logged_in?
    Cli.sync
    return true
  end
  response = Cli.( email, password: password )
  raise "Login failed! #{response}" unless response.include? 'Success'
  Cli.sync
  @password = nil # Clear out password
  true
end

#logoutBoolean

Logout of Lastpass

Examples:

@lastpass.logout
puts @lastpass.logged_out?

Returns:

  • (Boolean)

    if logout was successful



49
50
51
52
53
54
55
# File 'lib/lastpass-api/client.rb', line 49

def logout
  return true if logged_out?
  Cli.logout
  true
rescue
  false
end