Class: Lastpass::Account

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

Overview

Interact with a Lastpass account

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Account

Returns a new instance of Account.

Parameters:

  • params (Hash)


11
12
13
# File 'lib/lastpass-api/account.rb', line 11

def initialize( params )
  ( params )
end

Instance Attribute Details

#groupObject (readonly)

TODO:

Make group editable eventually



7
8
9
# File 'lib/lastpass-api/account.rb', line 7

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/lastpass-api/account.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/lastpass-api/account.rb', line 8

def name
  @name
end

#notesObject

Returns the value of attribute notes.



8
9
10
# File 'lib/lastpass-api/account.rb', line 8

def notes
  @notes
end

#passwordObject

Returns the value of attribute password.



8
9
10
# File 'lib/lastpass-api/account.rb', line 8

def password
  @password
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/lastpass-api/account.rb', line 8

def url
  @url
end

#usernameObject

Returns the value of attribute username.



8
9
10
# File 'lib/lastpass-api/account.rb', line 8

def username
  @username
end

Instance Method Details

#deleteObject

Delete the account

Examples:

 = @lastpass.accounts.find( 1234 )
.delete


78
79
80
81
# File 'lib/lastpass-api/account.rb', line 78

def delete
  Cli.rm( @id )
  @deleted = true
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



106
107
108
109
# File 'lib/lastpass-api/account.rb', line 106

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

#saveObject

TODO:

This does not support changing groups yet!

Either create or update an account, depending on what was changed on the account object before this method was called.

Examples:

# Update using instance variables
 = @lastpass.accounts.find( 'MyAccount' )
.name = 'MyAccount EDIT'
.username = 'root EDIT'
.password = 'pass EDIT'
.url = 'http://www.exampleEDIT.com'
.notes = 'This is my notes. EDIT'
.save


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lastpass-api/account.rb', line 48

def save
  deleted! if @deleted
  # If there is an ID, update that entry
  if @id
    Cli.edit( @id,
      name:     @name,
      username: @username,
      password: @password,
      url:      @url,
      notes:    @notes,
      group:    @group
    )
  else # If no ID, that means this is a new entry
    Cli.add( @name,
      username: @username,
      password: @password,
      url:      @url,
      notes:    @notes,
      group:    @group
    )
    set_id_after_save
  end
  self
end

#to_hashHash Also known as: to_h

Hash representation of the account object

Examples:

puts .to_h
# => { id: '1234', name: 'MyAccount', username: 'root', password: 'pass', url: 'http://www.example.com', notes: 'This is my note.', group: 'MyGroup' }

Returns:

  • (Hash)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lastpass-api/account.rb', line 89

def to_hash
  params = {}
  params[:id]       = @id       if @id
  params[:name]     = @name     if @name
  params[:username] = @username if @username
  params[:password] = @password if @password
  params[:url]      = @url      if @url
  params[:notes]    = @notes    if @notes
  params[:group]    = @group    if @group
  params
end

#update(params) ⇒ Object

Update the account details

Examples:

 = @lastpass.accounts.find( 'MyAccount' )
.update(
  name: 'MyAccount EDIT',
  username: 'root EDIT',
  password: 'pass EDIT',
  url: 'http://www.exampleEDIT.com',
  notes: 'This is my note. EDIT'
)

Parameters:

  • params (Hash)


27
28
29
30
31
32
33
# File 'lib/lastpass-api/account.rb', line 27

def update( params )
  deleted! if @deleted
  params.delete( :id ) # Prevent overwriting ID
  params.delete( :group ) # Prevent overwriting group
  ( params )
  save
end