Class: Imap::Backup::Account::Folder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/imap/backup/account/folder.rb

Constant Summary collapse

REQUESTED_ATTRIBUTES =
%w[RFC822 FLAGS INTERNALDATE].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, name) ⇒ Folder

Returns a new instance of Folder.



18
19
20
21
22
# File 'lib/imap/backup/account/folder.rb', line 18

def initialize(connection, name)
  @connection = connection
  @name = name
  @uid_validity = nil
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/imap/backup/account/folder.rb', line 13

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/imap/backup/account/folder.rb', line 14

def name
  @name
end

Instance Method Details

#append(message) ⇒ Object



81
82
83
84
85
86
# File 'lib/imap/backup/account/folder.rb', line 81

def append(message)
  body = message.imap_body
  date = message.date&.to_time
  response = imap.append(name, body, nil, date)
  extract_uid(response)
end

#createObject



36
37
38
39
40
# File 'lib/imap/backup/account/folder.rb', line 36

def create
  return if exist?

  imap.create(name)
end

#exist?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/imap/backup/account/folder.rb', line 29

def exist?
  examine
  true
rescue FolderNotFound
  false
end

#fetch(uid) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/imap/backup/account/folder.rb', line 67

def fetch(uid)
  examine
  fetch_data_items = imap.uid_fetch([uid.to_i], REQUESTED_ATTRIBUTES)
  return nil if fetch_data_items.nil?

  fetch_data_item = fetch_data_items[0]
  attributes = fetch_data_item.attr
  return nil if !attributes.key?("RFC822")

  attributes
rescue FolderNotFound
  nil
end

#folderObject

Deprecated: use #name



25
26
27
# File 'lib/imap/backup/account/folder.rb', line 25

def folder
  name
end

#uid_validityObject



42
43
44
45
46
47
48
# File 'lib/imap/backup/account/folder.rb', line 42

def uid_validity
  @uid_validity ||=
    begin
      examine
      imap.responses["UIDVALIDITY"][-1]
    end
end

#uidsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/imap/backup/account/folder.rb', line 50

def uids
  examine
  imap.uid_search(["ALL"]).sort
rescue FolderNotFound
  []
rescue NoMethodError
  message = <<~MESSAGE
    Folder '#{name}' caused NoMethodError
    probably
    `undefined method `[]' for nil:NilClass (NoMethodError)`
    in `search_internal` in stdlib net/imap.rb.
    This is caused by `@responses["SEARCH"] being unset/undefined
  MESSAGE
  Imap::Backup.logger.warn message
  []
end