Class: Rubyists::Opr::Vault

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyists::opr/model/vault.rb

Overview

Represents a 1password vault

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid:, name:) ⇒ Vault

Returns a new instance of Vault.



28
29
30
31
# File 'lib/rubyists::opr/model/vault.rb', line 28

def initialize(uuid:, name:)
  @name = name
  @uuid = uuid
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/rubyists::opr/model/vault.rb', line 27

def name
  @name
end

#uuidObject (readonly)

Returns the value of attribute uuid.



27
28
29
# File 'lib/rubyists::opr/model/vault.rb', line 27

def uuid
  @uuid
end

Class Method Details

.allObject



14
15
16
17
18
19
20
21
# File 'lib/rubyists::opr/model/vault.rb', line 14

def self.all
  cmd = TTY::Command.new pty: true, printer: :null
  out, err = Opr. { cmd.run Opr.opbin, 'list', 'vaults' }
  raise "Error #{err}" unless err.nil? || err.empty?

  vaults = JSON.parse out
  vaults.map { |v| from_hash v }
end

.find_by_name(name) ⇒ Object



23
24
25
# File 'lib/rubyists::opr/model/vault.rb', line 23

def self.find_by_name(name)
  all.detect { |n| n.name == name }
end

.from_hash(hash) ⇒ Object



10
11
12
# File 'lib/rubyists::opr/model/vault.rb', line 10

def self.from_hash(hash)
  new uuid: hash['uuid'], name: hash['name']
end

.from_output(string) ⇒ Object



6
7
8
# File 'lib/rubyists::opr/model/vault.rb', line 6

def self.from_output(string)
  from_hash JSON.parse(string)
end

Instance Method Details

#insert(title:, password:, type: 'login', username: nil, notes: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rubyists::opr/model/vault.rb', line 33

def insert(title:, password:, type: 'login', username: nil, notes: nil)
  current = Item.find(title, vault: name)
  raise "There is already an item named #{title} in the #{name} vault" if current

  tpl = Opr::LIBDIR.join("commands/templates/gen/#{type}.erb")
  erb = ERB.new(tpl.read)
  json = erb.result(binding)
  Item.create(json, title, name, type: :login)
end

#itemsObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/rubyists::opr/model/vault.rb', line 43

def items
  return @items if @items

  cmd = TTY::Command.new pty: true, printer: :null
  out, err = Opr. { cmd.run "#{Opr.opbin} list items --vault='#{name}'" }
  raise "Error #{err}" unless err.nil? || err.empty?

  array = JSON.parse out
  @items = array.map { |h| Item.from_hash h }
end