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

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

Constant Summary collapse

REQUESTED_ATTRIBUTES =
%w[RFC822 FLAGS INTERNALDATE].freeze
UID_FETCH_RETRY_CLASSES =
[EOFError].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RetryOnError

#retry_on_error

Constructor Details

#initialize(connection, name) ⇒ Folder



22
23
24
25
26
# File 'lib/imap/backup/account/folder.rb', line 22

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

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



17
18
19
# File 'lib/imap/backup/account/folder.rb', line 17

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#append(message) ⇒ Object



88
89
90
91
92
93
# File 'lib/imap/backup/account/folder.rb', line 88

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

#createObject



40
41
42
43
44
# File 'lib/imap/backup/account/folder.rb', line 40

def create
  return if exist?

  imap.create(utf7_encoded_name)
end

#exist?Boolean



33
34
35
36
37
38
# File 'lib/imap/backup/account/folder.rb', line 33

def exist?
  examine
  true
rescue FolderNotFound
  false
end

#fetch(uid) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/imap/backup/account/folder.rb', line 71

def fetch(uid)
  examine
  fetch_data_items =
    retry_on_error(errors: UID_FETCH_RETRY_CLASSES) do
      imap.uid_fetch([uid.to_i], REQUESTED_ATTRIBUTES)
    end
  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



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

def folder
  name
end

#uid_validityObject



46
47
48
49
50
51
52
# File 'lib/imap/backup/account/folder.rb', line 46

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

#uidsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/imap/backup/account/folder.rb', line 54

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