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:


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

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)

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

def domain
  @domain
end

#encryptedString

Returns:

  • (String)

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

def encrypted
  @encrypted
end

#open_contentsString

Returns:

  • (String)

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

def open_contents
  @open_contents
end

#profileOnePassword::Profile (readonly)


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

def profile
  @profile
end

#security_levelString

Returns:

  • (String)

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

def security_level
  @security_level
end

#titleString

Returns:

  • (String)

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

def title
  @title
end

#trashedString

Returns:

  • (String)

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

def trashed
  @trashed
end

#typeString

Returns:

  • (String)

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

def type
  @type
end

#updated_atString

Returns:

  • (String)

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

def updated_at
  @updated_at
end

#uuidString

Returns:

  • (String)

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

def uuid
  @uuid
end

Instance Method Details

#attributesObject


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

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


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

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


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

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


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

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

#decrypt_dataObject


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

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


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

def encryption_key
  profile.encryption_keys[security_level].decrypted_key
end

#file_nameObject


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

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

#find_field_with_designation(designation) ⇒ Object


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

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


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

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

#login_passwordObject


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

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

#login_usernameObject


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

def 
  find_field_with_designation('username')
end

#matches?(text) ⇒ Boolean

Returns:

  • (Boolean)

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

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

#system?Boolean

Returns:

  • (Boolean)

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

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

#trashed?Boolean

Returns:

  • (Boolean)

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

def trashed?
  @trashed == 'Y'
end

#wallet?Boolean

Returns:

  • (Boolean)

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

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