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

BODY_ATTRIBUTE =
"BODY[]".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

Returns a new instance of 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



107
108
109
110
111
112
# File 'lib/imap/backup/account/folder.rb', line 107

def append(message)
  body = message.imap_body
  date = message.date&.to_time
  response = client.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?

  client.create(utf7_encoded_name)
end

#exist?Boolean

Returns:

  • (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
# 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
      client.uid_fetch([uid.to_i], [BODY_ATTRIBUTE])
    end
  return nil if fetch_data_items.nil?

  fetch_data_item = fetch_data_items[0]
  attributes = fetch_data_item.attr

  attributes[BODY_ATTRIBUTE]
rescue FolderNotFound
  nil
end

#fetch_multi(uids) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/imap/backup/account/folder.rb', line 87

def fetch_multi(uids)
  examine
  fetch_data_items =
    retry_on_error(errors: UID_FETCH_RETRY_CLASSES) do
      client.uid_fetch(uids, [BODY_ATTRIBUTE])
    end
  return nil if fetch_data_items.nil?

  fetch_data_items.map do |item|
    attributes = item.attr

    {
      uid: attributes["UID"],
      body: attributes[BODY_ATTRIBUTE]
    }
  end
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
      client.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
  client.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.logger.warn message
  []
end