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 (String, Symbol) (defaults to: :default)

    the label of the session to initialize



23
24
25
26
27
28
29
# File 'lib/kbsecret/session.rb', line 23

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

  @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:



14
15
16
# File 'lib/kbsecret/session.rb', line 14

def config
  @config
end

#directoryString (readonly)

Returns the fully-qualified path of the session.

Returns:

  • (String)

    the fully-qualified path of the session



17
18
19
# File 'lib/kbsecret/session.rb', line 17

def directory
  @directory
end

#labelSymbol (readonly)

Returns the session's label.

Returns:

  • (Symbol)

    the session's label



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

def label
  @label
end

Instance Method Details

#[](label) ⇒ Record::Abstract?

Returns the record with the requested label, if extant.

Parameters:

  • label (String, Symbol)

    the label of the record to fetch

Returns:



33
34
35
# File 'lib/kbsecret/session.rb', line 33

def [](label)
  @records.find { |r| r.label == label.to_s }
end

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

This method returns an undefined value.

Add a record to the session.

Parameters:

  • type (String, Symbol)

    the type of record (see Record.record_types)

  • label (String, Symbol)

    the new record's label

  • args (Array<String>)

    the record-type specific arguments

Raises:

  • UnknownRecordTypeError if the requested type does not exist in Record.record_types

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



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kbsecret/session.rb', line 64

def add_record(type, label, *args)
  klass = Record.class_for(type.to_sym)
  raise RecordTypeUnknownError, type unless klass

  arity = klass.instance_method(:initialize).arity - 2
  raise RecordCreationArityError.new(arity, args.size) unless arity == args.size

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

#delete_record(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:

  • label (String, Symbol)

    the label of the record to delete



80
81
82
83
84
85
86
# File 'lib/kbsecret/session.rb', line 80

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

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

#load_records!Array<Record::Abstract>

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.

Load all records associated with the session.

Returns:



112
113
114
115
116
# File 'lib/kbsecret/session.rb', line 112

def load_records!
  record_paths.map do |path|
    Record.load_record! self, path
  end
end

#record?(label) ⇒ Boolean

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

Parameters:

  • label (String, Symbol)

    the label to test for

Returns:

  • (Boolean)

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



91
92
93
# File 'lib/kbsecret/session.rb', line 91

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

#record_labelsArray<String>

Returns the labels of all records known to the session.

Examples:

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

Returns:

  • (Array<String>)

    the labels of all records known to the session



51
52
53
# File 'lib/kbsecret/session.rb', line 51

def record_labels
  records.map(&:label)
end

#record_pathsArray<String>

Returns the fully qualified paths of all records in the session.

Returns:

  • (Array<String>)

    the fully qualified paths of all records in the session



105
106
107
# File 'lib/kbsecret/session.rb', line 105

def record_paths
  Dir[File.join(directory, "*.json")]
end

#records(type = nil) ⇒ Array<Record::Abstract>

All records (of a given type) in the session.

Parameters:

  • type (String, Symbol) (defaults to: nil)

    the type of the records to return (or nil for all)

Returns:



40
41
42
43
44
45
46
# File 'lib/kbsecret/session.rb', line 40

def records(type = nil)
  if type
    @records.select { |r| r.type == type.to_sym }
  else
    @records
  end
end

#rel_path(rel, mkdir: false) ⇒ String

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.

Returns the fully qualified path to the session.

Parameters:

  • rel (String, Symbol)

    the "root" of the session

  • mkdir (Boolean) (defaults to: false)

    whether or not to make the session directory

Returns:

  • (String)

    the fully qualified path to the session



122
123
124
125
126
127
128
129
# File 'lib/kbsecret/session.rb', line 122

def rel_path(rel, mkdir: false)
  # /keybase/private/[username]/kbsecret/[session]
  path = File.join(Config[:session_root], rel)

  FileUtils.mkdir_p path if mkdir

  path
end

#unlink!void

Note:

Use this with caution, as all files under the session directory will be deleted. Furthermore, the session directory itself will be deleted, and this object will become garbage.

This method returns an undefined value.

Delete the entire session.



100
101
102
# File 'lib/kbsecret/session.rb', line 100

def unlink!
  FileUtils.rm_rf(directory)
end