Class: Rubyists::Opr::Item

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

Overview

An item

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid:, name:, raw:) ⇒ Item

Returns a new instance of Item.



36
37
38
39
40
# File 'lib/rubyists::opr/model/item.rb', line 36

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



35
36
37
# File 'lib/rubyists::opr/model/item.rb', line 35

def name
  @name
end

#rawObject (readonly)

Returns the value of attribute raw.



35
36
37
# File 'lib/rubyists::opr/model/item.rb', line 35

def raw
  @raw
end

#uuidObject (readonly)

Returns the value of attribute uuid.



35
36
37
# File 'lib/rubyists::opr/model/item.rb', line 35

def uuid
  @uuid
end

Class Method Details

.create(hash, title, vault = 'Private', type: :login) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rubyists::opr/model/item.rb', line 13

def self.create(hash, title, vault = 'Private', type: :login)
  cmd = TTY::Command.new pty: true, printer: :null
  
  Opr. do
    cmd.run "#{Opr.opbin} create item '#{type.capitalize}' '#{hash}' --vault='#{vault}' --title='#{title}'"
  end
end

.find(item, vault:) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubyists::opr/model/item.rb', line 21

def self.find(item, vault:)
  cmd = TTY::Command.new pty: true, printer: :null
  out, err = Opr. do
    cmd.run "#{Opr.opbin} get item '#{item}' --vault='#{vault}'"
  end
  raise Error, err unless err.empty?

  from_output out
rescue TTY::Command::ExitError => e
  return nil if e.to_s.match? /item with query "#{Regexp.escape(item)}" not found/

  raise
end

.from_hash(hash) ⇒ Object



9
10
11
# File 'lib/rubyists::opr/model/item.rb', line 9

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

.from_output(string) ⇒ Object



5
6
7
# File 'lib/rubyists::opr/model/item.rb', line 5

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

Instance Method Details

#delete!Object



46
47
48
49
50
51
# File 'lib/rubyists::opr/model/item.rb', line 46

def delete!
  Opr. do
    cmd = TTY::Command.new pty: true, printer: :null
    cmd.run "#{Opr.opbin} delete item '#{name}' --vault='#{vault_uuid}'"
  end
end

#inspectObject



62
63
64
# File 'lib/rubyists::opr/model/item.rb', line 62

def inspect
  "<#{self.class}:#{object_id} name: #{name} uuid: #{uuid}>"
end

#passwordObject



53
54
55
56
# File 'lib/rubyists::opr/model/item.rb', line 53

def password
  pass_field = @raw['details']['fields'].detect { |f| f['designation'] == 'password' }
  pass_field['value']
end

#titleObject



58
59
60
# File 'lib/rubyists::opr/model/item.rb', line 58

def title
  @title ||= raw['overview']['title']
end

#vault_uuidObject



42
43
44
# File 'lib/rubyists::opr/model/item.rb', line 42

def vault_uuid
  @vault_uuid ||= raw['vaultUuid']
end