Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Reduce duplication in normalizing search args #348

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,9 @@ def uid_expunge(uid_set)
end
end

# :call-seq:
# search(criteria, charset = nil) -> result
#
# Sends a {SEARCH command [IMAP4rev1 §6.4.4]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4]
# to search the mailbox for messages that match the given search +criteria+,
# and returns a SearchResult. SearchResult inherits from Array (for
Expand Down Expand Up @@ -2174,10 +2177,13 @@ def uid_expunge(uid_set)
# result = imap.search(["SUBJECT", "hi there", "not", "new"])
# #=> Net::IMAP::SearchResult[1, 6, 7, 8, modseq: 5594]
# result.modseq # => 5594
def search(keys, charset = nil)
return search_internal("SEARCH", keys, charset)
def search(...)
search_internal("SEARCH", ...)
end

# :call-seq:
# uid_search(criteria, charset = nil) -> result
#
# Sends a {UID SEARCH command [IMAP4rev1 §6.4.8]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8]
# to search the mailbox for messages that match the given searching
# criteria, and returns unique identifiers (<tt>UID</tt>s).
Expand All @@ -2187,8 +2193,8 @@ def search(keys, charset = nil)
# capability has been enabled.
#
# See #search for documentation of parameters.
def uid_search(keys, charset = nil)
return search_internal("UID SEARCH", keys, charset)
def uid_search(...)
search_internal("UID SEARCH", ...)
end

# :call-seq:
Expand Down Expand Up @@ -3117,18 +3123,11 @@ def enforce_logindisabled?
end
end

def search_internal(cmd, keys, charset)
if keys.instance_of?(String)
keys = [RawData.new(keys)]
else
normalize_searching_criteria(keys)
end
def search_internal(cmd, keys, charset = nil)
keys = normalize_searching_criteria(keys)
args = charset ? ["CHARSET", charset, *keys] : keys
synchronize do
if charset
send_command(cmd, "CHARSET", charset, *keys)
else
send_command(cmd, *keys)
end
send_command(cmd, *args)
clear_responses("SEARCH").last || []
end
end
Expand Down Expand Up @@ -3175,31 +3174,24 @@ def copy_internal(cmd, set, mailbox)
end

def sort_internal(cmd, sort_keys, search_keys, charset)
if search_keys.instance_of?(String)
search_keys = [RawData.new(search_keys)]
else
normalize_searching_criteria(search_keys)
end
search_keys = normalize_searching_criteria(search_keys)
synchronize do
send_command(cmd, sort_keys, charset, *search_keys)
clear_responses("SORT").last || []
end
end

def thread_internal(cmd, algorithm, search_keys, charset)
if search_keys.instance_of?(String)
search_keys = [RawData.new(search_keys)]
else
normalize_searching_criteria(search_keys)
end
search_keys = normalize_searching_criteria(search_keys)
synchronize do
send_command(cmd, algorithm, charset, *search_keys)
clear_responses("THREAD").last || []
end
end

def normalize_searching_criteria(keys)
keys.collect! do |i|
def normalize_searching_criteria(criteria)
return RawData.new(criteria) if criteria.is_a?(String)
criteria.map do |i|
case i
when -1, Range, Array
SequenceSet.new(i)
Expand Down