Class: KBSecret::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/kbsecret/session.rb

Overview

Represents a session of N keybase users with collective read/write access to a collection of records.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: :default) ⇒ Session

Note:

This does not create a new session, but loads one already specified in Config::CONFIG_FILE. To create a new session, see Config.configure_session.

Returns a new instance of Session.

Parameters:

  • label (Symbol) (defaults to: :default)

    the label of the session to initialize



18
19
20
21
22
23
24
# File 'lib/kbsecret/session.rb', line 18

def initialize(label: :default)
  @label = label
  @config = Config.session(label.to_sym)

  @directory = rel_path config[:root], mkdir: true
  @records = load_records!
end

Instance Attribute Details

#configHash (readonly)

Returns the session-specific configuration, from Config::CONFIG_FILE.

Returns:



10
11
12
# File 'lib/kbsecret/session.rb', line 10

def config
  @config
end

#directoryObject (readonly)

Returns the value of attribute directory.



11
12
13
# File 'lib/kbsecret/session.rb', line 11

def directory
  @directory
end

#labelSymbol (readonly)

Returns the session's label.

Returns:

  • (Symbol)

    the session's label



6
7
8
# File 'lib/kbsecret/session.rb', line 6

def label
  @label
end

#recordsObject (readonly)

Returns the value of attribute records.



12
13
14
# File 'lib/kbsecret/session.rb', line 12

def records
  @records
end

Instance Method Details

#add_record(type, label, *args) ⇒ void

This method returns an undefined value.

Add a record to the session.

Parameters:

  • type (String)

    the type of record (see Record.record_types)

  • label (Symbol)

    the new record's label

  • args (Array<String>)

    the record-type specific arguments

Raises:

  • RecordCreationArityError if the number of specified record arguments does not match the record type's constructor



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kbsecret/session.rb', line 40

def add_record(type, label, *args)
  klass = Record.record_classes.find { |k| k.type == type }
  arity = klass.instance_method(:initialize).arity - 2

  unless arity == args.size
    raise RecordCreationArityError.new(arity, args.size)
  end

  record = klass.new(self, label, *args)
  records << record
  record.sync!
end

#delete_record(rec_label) ⇒ void

This method returns an undefined value.

Delete a record from the session, if it exists. Does nothing if no such record can be found.

Parameters:

  • rec_label (Symbol)

    the label of the record to delete



57
58
59
60
61
62
63
# File 'lib/kbsecret/session.rb', line 57

def delete_record(rec_label)
  record = records.find { |r| r.label == rec_label }
  return unless record

  File.delete(record.path)
  records.delete(record)
end

#record?(label) ⇒ Boolean

Returns whether or not the session contains a record with the given label.

Returns:

  • (Boolean)

    whether or not the session contains a record with the given label



67
68
69
# File 'lib/kbsecret/session.rb', line 67

def record?(label)
  record_labels.include?(label)
end

#record_labelsArray<Symbol>

Returns the labels of all records known to the session.

Examples:

session.record_labels # => [:website1, :apikey1, :website2]

Returns:

  • (Array<Symbol>)

    the labels of all records known to the session



29
30
31
# File 'lib/kbsecret/session.rb', line 29

def record_labels
  records.map(&:label)
end