lib/imap_guard/guard.rb in imap_guard-0.0.4 vs lib/imap_guard/guard.rb in imap_guard-1.0.0
- old
+ new
@@ -78,10 +78,27 @@
'deleted'.red
}
process query, operation, &filter
end
+ # Runs operation on messages matching the query
+ # @param query IMAP query
+ # @param opration Lambda to call on each message
+ # @return [void]
+ def each query
+ operation = lambda { |message_id| yield message_id }
+ process query, operation
+ end
+
+ # Fetches a message from its UID
+ # @return [Mail]
+ # @note We use "BODY.PEEK[]" to avoid setting the \Seen flag.
+ def fetch_mail message_id
+ msg = @imap.fetch(message_id, 'BODY.PEEK[]').first.attr['BODY[]']
+ Mail.read_from_string msg
+ end
+
# @return [Array<String>] Sorted list of all mailboxes
def list
@imap.list("", "*").map(&:name).sort
end
@@ -125,24 +142,14 @@
result = yield(mail)
verbose.print "(given filter result: #{result.inspect}) "
end
end
- if result
- puts operation.call(message_id)
- else
- puts "ignored".green
- end
+ puts result ? operation.call(message_id) : "ignored".green
end
ensure
expunge
- end
-
- # @note We use "BODY.PEEK[]" to avoid setting the \Seen flag.
- def fetch_mail message_id
- msg = @imap.fetch(message_id, 'BODY.PEEK[]').first.attr['BODY[]']
- Mail.read_from_string msg
end
def search query
unless [Array, String].any? { |type| query.is_a? type }
raise TypeError, "Query must be either a string holding the entire search string, or a single-dimension array of search keywords and arguments."