Class: Lastpass::Group

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

Overview

Interact with a Lastpass group/folder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Group

Returns a new instance of Group.

Parameters:

  • params (Hash)


9
10
11
12
# File 'lib/lastpass-api/group.rb', line 9

def initialize( params )
  params[:name].chomp!( '/' ) if params[:name]&.end_with? '/'
  params_to_group( params )
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/lastpass-api/group.rb', line 6

def name
  @name
end

Instance Method Details

#deleteObject

Delete the group

Examples:

group = @lastpass.groups.find( 1234 )
group.delete


52
53
54
55
# File 'lib/lastpass-api/group.rb', line 52

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



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

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

#saveObject

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

Examples:

# Update using instance variables
group = @lastpass.groups.find( 'Group1' )
group.name = 'Group1 EDIT'
group.save


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lastpass-api/group.rb', line 35

def save
  deleted! if @deleted
  # If there is an ID, update that entry
  if @id
    Cli.edit_group( @id, name: @name )
  else # If no ID, that means this is a new entry
    Cli.add_group( @name )
    set_id_after_save
  end
  self
end

#to_hashHash Also known as: to_h

Hash representation of the account object

Examples:

puts group.to_h
# => { id: '1234', name: 'Group1' }

Returns:

  • (Hash)


63
64
65
66
67
68
# File 'lib/lastpass-api/group.rb', line 63

def to_hash
  params = {}
  params[:id]   = @id   if @id
  params[:name] = @name if @name
  params
end

#update(params) ⇒ Object

Update the group details

Examples:

group = @lastpass.groups.find( 'Group1' )
group.update( name: 'Group1 EDIT' )

Parameters:

  • params (Hash)


20
21
22
23
24
25
# File 'lib/lastpass-api/group.rb', line 20

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