Skip to content

Commit 8591ec0

Browse files
authored
🔀 Merge pull request #348 from ruby/search-refactor-normalize-args
♻️ Reduce duplication in normalizing search args
2 parents 14205d5 + 8323ce3 commit 8591ec0

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

lib/net/imap.rb

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,9 @@ def uid_expunge(uid_set)
19291929
end
19301930
end
19311931

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

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

21942200
# :call-seq:
@@ -3117,18 +3123,11 @@ def enforce_logindisabled?
31173123
end
31183124
end
31193125

3120-
def search_internal(cmd, keys, charset)
3121-
if keys.instance_of?(String)
3122-
keys = [RawData.new(keys)]
3123-
else
3124-
normalize_searching_criteria(keys)
3125-
end
3126+
def search_internal(cmd, keys, charset = nil)
3127+
keys = normalize_searching_criteria(keys)
3128+
args = charset ? ["CHARSET", charset, *keys] : keys
31263129
synchronize do
3127-
if charset
3128-
send_command(cmd, "CHARSET", charset, *keys)
3129-
else
3130-
send_command(cmd, *keys)
3131-
end
3130+
send_command(cmd, *args)
31323131
clear_responses("SEARCH").last || []
31333132
end
31343133
end
@@ -3175,31 +3174,24 @@ def copy_internal(cmd, set, mailbox)
31753174
end
31763175

31773176
def sort_internal(cmd, sort_keys, search_keys, charset)
3178-
if search_keys.instance_of?(String)
3179-
search_keys = [RawData.new(search_keys)]
3180-
else
3181-
normalize_searching_criteria(search_keys)
3182-
end
3177+
search_keys = normalize_searching_criteria(search_keys)
31833178
synchronize do
31843179
send_command(cmd, sort_keys, charset, *search_keys)
31853180
clear_responses("SORT").last || []
31863181
end
31873182
end
31883183

31893184
def thread_internal(cmd, algorithm, search_keys, charset)
3190-
if search_keys.instance_of?(String)
3191-
search_keys = [RawData.new(search_keys)]
3192-
else
3193-
normalize_searching_criteria(search_keys)
3194-
end
3185+
search_keys = normalize_searching_criteria(search_keys)
31953186
synchronize do
31963187
send_command(cmd, algorithm, charset, *search_keys)
31973188
clear_responses("THREAD").last || []
31983189
end
31993190
end
32003191

3201-
def normalize_searching_criteria(keys)
3202-
keys.collect! do |i|
3192+
def normalize_searching_criteria(criteria)
3193+
return RawData.new(criteria) if criteria.is_a?(String)
3194+
criteria.map do |i|
32033195
case i
32043196
when -1, Range, Array
32053197
SequenceSet.new(i)

0 commit comments

Comments
 (0)