Skip to content

Commit 8655aeb

Browse files
committed
✨ Add keyword argument for search charset
Someday, I'd like to deprecate the positional `charset` parameter.
1 parent 2399044 commit 8655aeb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/net/imap.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1931,6 +1931,7 @@ def uid_expunge(uid_set)
19311931

19321932
# :call-seq:
19331933
# search(criteria, charset = nil) -> result
1934+
# search(criteria, charset: nil) -> result
19341935
#
19351936
# Sends a {SEARCH command [IMAP4rev1 §6.4.4]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.4]
19361937
# to search the mailbox for messages that match the given search +criteria+,
@@ -2210,6 +2211,7 @@ def search(...)
22102211

22112212
# :call-seq:
22122213
# uid_search(criteria, charset = nil) -> result
2214+
# uid_search(criteria, charset: nil) -> result
22132215
#
22142216
# Sends a {UID SEARCH command [IMAP4rev1 §6.4.8]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.4.8]
22152217
# to search the mailbox for messages that match the given searching
@@ -3150,7 +3152,11 @@ def enforce_logindisabled?
31503152
end
31513153
end
31523154

3153-
def search_args(keys, charset = nil)
3155+
def search_args(keys, charset_arg = nil, charset: nil)
3156+
if charset && charset_arg
3157+
raise ArgumentError, "multiple charset arguments"
3158+
end
3159+
charset ||= charset_arg
31543160
# NOTE: not handling combined RETURN and CHARSET for raw strings
31553161
if charset && keys in /\ACHARSET\b/i | Array[/\ACHARSET\z/i, *]
31563162
raise ArgumentError, "multiple charset arguments"

test/net/imap/test_imap.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,9 @@ def test_unselect
12351235
imap.search('CHARSET UTF-8 SUBJECT "Hello world"')
12361236
assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args
12371237

1238+
imap.search('SUBJECT "Hello world"', charset: "UTF-8")
1239+
assert_equal 'CHARSET UTF-8 SUBJECT "Hello world"', server.commands.pop.args
1240+
12381241
imap.search([:*])
12391242
assert_equal "*", server.commands.pop.args
12401243

@@ -1276,6 +1279,15 @@ def seqset_coercible.to_sequence_set
12761279
assert_raise(ArgumentError) do
12771280
imap.search("charset foo ALL", "bar")
12781281
end
1282+
assert_raise(ArgumentError) do
1283+
imap.search(["ALL"], "foo", charset: "bar")
1284+
end
1285+
assert_raise(ArgumentError) do
1286+
imap.search(["charset", "foo", "ALL"], charset: "bar")
1287+
end
1288+
assert_raise(ArgumentError) do
1289+
imap.search("charset foo ALL", charset: "bar")
1290+
end
12791291
# Parsing return opts is too complicated, for now.
12801292
# assert_raise(ArgumentError) do
12811293
# imap.search("return () charset foo ALL", "bar")

0 commit comments

Comments
 (0)