Skip to content

Commit 7727383

Browse files
committed
📚 Update Net::IMAP.new documentation
Updated rdoc for #initialize: * Various stylistic changes, mostly for options and exceptions. * Added examples to demonstrate connecting with clear-text, TLS, and checking the greeting for "OK" or "PREAUTH". * Added links to OpenSSL::SSL::SSLContext rdoc.
1 parent a8369da commit 7727383

File tree

2 files changed

+86
-29
lines changed

2 files changed

+86
-29
lines changed

lib/net/imap.rb

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -722,34 +722,79 @@ class << self
722722
# The port this client connected to
723723
attr_reader :port
724724

725-
# :call-seq:
726-
# Net::IMAP.new(host, options = {})
727-
#
728725
# Creates a new Net::IMAP object and connects it to the specified
729726
# +host+.
730727
#
731-
# +options+ is an option hash, each key of which is a symbol.
732-
#
733-
# The available options are:
728+
# ==== Options
729+
#
730+
# Accepts the following options:
731+
#
732+
# [port]
733+
# Port number. Defaults to 993 when +ssl+ is truthy, and 143 otherwise.
734+
#
735+
# [ssl]
736+
# If +true+, the connection will use TLS with the default params set by
737+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params].
738+
# If +ssl+ is a hash, it's passed to
739+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params];
740+
# the keys are names of attribute assignment methods on
741+
# SSLContext[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html].
742+
#
743+
# [open_timeout]
744+
# Seconds to wait until a connection is opened
745+
# [idle_response_timeout]
746+
# Seconds to wait until an IDLE response is received
747+
#
748+
# See DeprecatedClientOptions for obsolete backwards compatible arguments.
749+
#
750+
# ==== Examples
751+
#
752+
# Connect to cleartext port 143 at mail.example.com and recieve the server greeting:
753+
# imap = Net::IMAP.new('mail.example.com', ssl: false) # => #<Net::IMAP:0x00007f79b0872bd0>
754+
# imap.port => 143
755+
# imap.tls_verified? => false
756+
# imap.greeting => name: ("OK" | "PREAUTH") => status
757+
# status # => "OK"
758+
# # The client is connected in the "Not Authenticated" state.
759+
#
760+
# Connect with TLS to port 993 at mail.example.com:
761+
# imap = Net::IMAP.new('mail.example.com', ssl: true) # => #<Net::IMAP:0x00007f79b0872bd0>
762+
# imap.port => 993
763+
# imap.tls_verified? => true
764+
# imap.greeting => name: ("OK" | "PREAUTH") => status
765+
# status # => "OK"
766+
# # The client is connected in the "Not Authenticated" state.
767+
#
768+
# Connect with prior authentication, for example using an SSL certificate:
769+
# ssl_ctx_params = {
770+
# cert: OpenSSL::X509::Certificate.new(File.read("client.crt")),
771+
# key: OpenSSL::PKey::EC.new(File.read('client.key')),
772+
# extra_chain_cert: [
773+
# OpenSSL::X509::Certificate.new(File.read("intermediate.crt")),
774+
# ],
775+
# }
776+
# imap = Net::IMAP.new('mail.example.com', ssl: ssl_ctx_params)
777+
# imap.port => 993
778+
# imap.tls_verified? => true
779+
# imap.greeting => name: "PREAUTH"
780+
# # The client is connected in the "Authenticated" state.
734781
#
735-
# port:: Port number (default value is 143 for imap, or 993 for imaps)
736-
# ssl:: If +options[:ssl]+ is true, then an attempt will be made
737-
# to use SSL (now TLS) to connect to the server.
738-
# If +options[:ssl]+ is a hash, it's passed to
739-
# OpenSSL::SSL::SSLContext#set_params as parameters.
740-
# open_timeout:: Seconds to wait until a connection is opened
741-
# idle_response_timeout:: Seconds to wait until an IDLE response is received
782+
# ==== Exceptions
742783
#
743784
# The most common errors are:
744785
#
745-
# Errno::ECONNREFUSED:: Connection refused by +host+ or an intervening
746-
# firewall.
747-
# Errno::ETIMEDOUT:: Connection timed out (possibly due to packets
748-
# being dropped by an intervening firewall).
749-
# Errno::ENETUNREACH:: There is no route to that network.
750-
# SocketError:: Hostname not known or other socket error.
751-
# Net::IMAP::ByeResponseError:: The connected to the host was successful, but
752-
# it immediately said goodbye.
786+
# [Errno::ECONNREFUSED]
787+
# Connection refused by +host+ or an intervening firewall.
788+
# [Errno::ETIMEDOUT]
789+
# Connection timed out (possibly due to packets being dropped by an
790+
# intervening firewall).
791+
# [Errno::ENETUNREACH]
792+
# There is no route to that network.
793+
# [SocketError]
794+
# Hostname not known or other socket error.
795+
# [Net::IMAP::ByeResponseError]
796+
# Connected to the host successfully, but it immediately said goodbye.
797+
#
753798
def initialize(host, port_or_options = {},
754799
usessl = false, certs = nil, verify = true)
755800
super()

test/net/imap/fake_server/test_helper.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,38 @@
44

55
module Net::IMAP::FakeServer::TestHelper
66

7-
def with_fake_server(select: nil, timeout: 5, **opts)
7+
def run_fake_server_in_thread(timeout: 5, **opts)
88
Timeout.timeout(timeout) do
99
server = Net::IMAP::FakeServer.new(timeout: timeout, **opts)
1010
@threads << Thread.new do server.run end if @threads
11+
yield server
12+
ensure
13+
server&.shutdown
14+
end
15+
end
16+
17+
def with_client(*args, **kwargs)
18+
client = Net::IMAP.new(*args, **kwargs)
19+
yield client
20+
ensure
21+
if client && !client.disconnected?
22+
client.logout rescue pp $!
23+
client.disconnect unless client.disconnected?
24+
end
25+
end
26+
27+
def with_fake_server(select: nil, **opts)
28+
run_fake_server_in_thread(**opts) do |server|
1129
tls = opts[:implicit_tls]
1230
tls = {ca_file: server.config.tls[:ca_file]} if tls == true
13-
client = Net::IMAP.new("localhost", port: server.port, ssl: tls)
14-
begin
31+
with_client("localhost", port: server.port, ssl: tls) do |client|
1532
if select
1633
client.select(select)
1734
server.commands.pop
1835
assert server.state.selected?
1936
end
2037
yield server, client
21-
ensure
22-
client.logout rescue pp $!
23-
client.disconnect if !client.disconnected?
2438
end
25-
ensure
26-
server&.shutdown
2739
end
2840
end
2941

0 commit comments

Comments
 (0)