Class: OnePassword::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/one_password/item.rb

Constant Summary collapse

INDEX_UUID =
0
INDEX_TYPE =
1
INDEX_NAME =
2
INDEX_URL =
3
INDEX_DATE =
4
INDEX_FOLDER =
5
INDEX_PASSWORD_STRENGTH =
6
INDEX_TRASHED =
7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, data) ⇒ Item

Returns a new instance of Item.

Parameters:



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/one_password/item.rb', line 34

def initialize(profile, data)
  @profile        = profile
  self.attributes = {
    uuid:       data[INDEX_UUID],
    type:       data[INDEX_TYPE],
    title:      data[INDEX_NAME],
    domain:     data[INDEX_URL],
    updated_at: data[INDEX_DATE],
    trashed:    data[INDEX_TRASHED]
  }
end

Instance Attribute Details

#domainString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def domain
  @domain
end

#encryptedString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def encrypted
  @encrypted
end

#open_contentsString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def open_contents
  @open_contents
end

#profileOnePassword::Profile (readonly)



30
31
32
# File 'lib/one_password/item.rb', line 30

def profile
  @profile
end

#security_levelString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def security_level
  @security_level
end

#titleString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def title
  @title
end

#trashedString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def trashed
  @trashed
end

#typeString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def type
  @type
end

#updated_atString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def updated_at
  @updated_at
end

#uuidString

Returns:

  • (String)


16
17
18
# File 'lib/one_password/item.rb', line 16

def uuid
  @uuid
end

Instance Method Details

#attributesObject



132
133
134
135
136
137
138
139
# File 'lib/one_password/item.rb', line 132

def attributes
  @attributes ||= {}
  fields = instance_variables.inject({}) do |result, ivar|
    result[ivar] = instance_variable_get(ivar) unless ivar == :@profile
    result
  end
  @attributes.merge(fields)
end

#attributes=(attrs) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/one_password/item.rb', line 141

def attributes=(attrs)
  attrs.each do |name, value|
    if value.present?
      attribute = name.to_s.underscore
      writer    = "#{attribute}="
      if respond_to?(writer)
        send(writer, value)
      else
        @attributes            ||= {}
        @attributes[attribute] = value
      end
    end
  end
end

#categoryObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/one_password/item.rb', line 66

def category
  @category ||= case type
                when SOFTWARE_LICENSES, WEBFORMS, NOTES, IDENTITIES, PASSWORDS
                  type.to_sym
                else
                  if type == FOLDERS
                    :folders
                  elsif wallet?
                    WALLET
                  else
                    ACCOUNT.to_sym
                  end
                end
end

#created_at=(seconds) ⇒ Object



50
51
52
# File 'lib/one_password/item.rb', line 50

def created_at=(seconds)
  @created_at = Time.at(seconds)
end

#decrypt_dataObject



106
107
108
109
110
111
112
113
# File 'lib/one_password/item.rb', line 106

def decrypt_data
  unless @decrypted
    @decrypted      = true
    plain_text = Encryption.decrypt_using_key(encrypted, encryption_key)
    attrs = JSON.parse(plain_text)
    self.attributes = attrs
  end
end

#encryption_keyObject



93
94
95
# File 'lib/one_password/item.rb', line 93

def encryption_key
  profile.encryption_keys[security_level].decrypted_key
end

#file_nameObject



85
86
87
# File 'lib/one_password/item.rb', line 85

def file_name
  profile.directory.join("#{uuid}.1password")
end

#find_field_with_designation(designation) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/one_password/item.rb', line 123

def find_field_with_designation(designation)
  fields = attributes['fields']
  field = fields.find do |field|
    field['designation'] == designation
  end if fields

  field['value'] if field
end

#load_encrypted_dataObject



97
98
99
# File 'lib/one_password/item.rb', line 97

def load_encrypted_data
  self.attributes = JSON.parse(File.read(file_name))
end

#login_passwordObject



119
120
121
# File 'lib/one_password/item.rb', line 119

def 
  attributes['password'].presence || find_field_with_designation('password')
end

#login_usernameObject



115
116
117
# File 'lib/one_password/item.rb', line 115

def 
  find_field_with_designation('username')
end

#matches?(text) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/one_password/item.rb', line 81

def matches?(text)
  title.downcase.index(text) || domain.downcase.index(text)
end

#system?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/one_password/item.rb', line 54

def system?
  @type =~ /^system/
end

#trashed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/one_password/item.rb', line 62

def trashed?
  @trashed == 'Y'
end

#wallet?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/one_password/item.rb', line 58

def wallet?
  @type =~ /^wallet\.(membership|financial|government)}/
end