class Rubyists::Opr::Vault

Represents a 1password vault

Attributes

name[R]
uuid[R]

Public Class Methods

all() click to toggle source
# File lib/rubyists::opr/model/vault.rb, line 14
def self.all
  cmd = TTY::Command.new pty: true, printer: :null
  out, err = Opr.with_login { 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/rubyists::opr/model/vault.rb, line 6
def self.from_output(string)
  from_hash JSON.parse(string)
end
new(uuid:, name:) click to toggle source
# File lib/rubyists::opr/model/vault.rb, line 28
def initialize(uuid:, name:)
  @name = name
  @uuid = uuid
end

Public Instance Methods

insert(title:, password:, type: 'login', username: nil, notes: nil) click to toggle source
# 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
items() click to toggle source
# 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.with_login { 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