Skip to content

Commit 7ad42ef

Browse files
committed
Do not require positional args for #authenticate
This API is a little bit confusing, IMO. But it does preserve backward compatibility, while allowing authenticators that don't allow positional parameters to work without crashing. But, authenticators that require only one parameter—or more than three—will still be inaccessible.
1 parent dcb8f31 commit 7ad42ef

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

lib/net/smtp.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,8 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
862862
DEFAULT_AUTH_TYPE = :plain
863863

864864
# call-seq:
865+
# authenticate(type: DEFAULT_AUTH_TYPE, **, &)
866+
# authenticate(type = DEFAULT_AUTH_TYPE, **, &)
865867
# authenticate(username, secret, type: DEFAULT_AUTH_TYPE, **, &)
866868
# authenticate(username, secret, type = DEFAULT_AUTH_TYPE, **, &)
867869
#
@@ -872,11 +874,17 @@ def open_message_stream(from_addr, *to_addrs, &block) # :yield: stream
872874
# All arguments—other than +type+—are forwarded to the authenticator.
873875
# Different authenticators may interpret the +username+ and +secret+
874876
# arguments differently.
875-
def authenticate(user, secret, typearg = nil, type: nil, **kwargs, &block)
877+
def authenticate(*args, type: nil, **kwargs, &block)
878+
case args.length
879+
when 1, 3 then typearg = args.pop
880+
when (4..)
881+
raise ArgumentError, "wrong number of arguments " \
882+
"(given %d, expected 0..3)" % [args.length]
883+
end
876884
authtype = type || typearg || DEFAULT_AUTH_TYPE
877-
check_auth_args(authtype, user, secret, **kwargs)
885+
check_auth_args(authtype, *args, **kwargs)
878886
authenticator = Authenticator.auth_class(authtype).new(self)
879-
critical { authenticator.auth(user, secret, **kwargs) }
887+
critical { authenticator.auth(*args, **kwargs) }
880888
end
881889

882890
private

0 commit comments

Comments
 (0)