Skip to content

Commit

Permalink
🔀 Merge pull request ruby#348 from ruby/search-refactor-normalize-args
Browse files Browse the repository at this point in the history
♻️  Reduce duplication in normalizing search args
  • Loading branch information
nevans authored Nov 8, 2024
2 parents 14205d5 + 8323ce3 commit 8591ec0
Showing 1 changed file with 19 additions and 27 deletions.
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

0 comments on commit 8591ec0

Please sign in to comment.