@@ -28,6 +28,11 @@ class Endpoint < ::IO::Endpoint::Generic
2828 "wss" => URI ::WSS ,
2929 }
3030
31+ # Parse a URL string into an endpoint.
32+ # @parameter string [String] The URL to parse.
33+ # @parameter endpoint [IO::Endpoint::Generic | Nil] An optional underlying endpoint to use.
34+ # @parameter options [Hash] Additional options to pass to {initialize}.
35+ # @returns [Endpoint] The parsed endpoint.
3136 def self . parse ( string , endpoint = nil , **options )
3237 url = URI . parse ( string ) . normalize
3338
@@ -80,6 +85,7 @@ def initialize(url, endpoint = nil, **options)
8085 end
8186 end
8287
88+ # @returns [URI] The URL representation of this endpoint, including port if non-default.
8389 def to_url
8490 url = @url . dup
8591
@@ -90,24 +96,29 @@ def to_url
9096 return url
9197 end
9298
99+ # @returns [String] A short string representation of this endpoint.
93100 def to_s
94101 "\# <#{ self . class } #{ self . to_url } #{ @options } >"
95102 end
96103
104+ # @returns [String] A detailed string representation of this endpoint.
97105 def inspect
98106 "\# <#{ self . class } #{ self . to_url } #{ @options . inspect } >"
99107 end
100108
101109 attr :url
102110
111+ # @returns [Addrinfo] The address of the underlying endpoint.
103112 def address
104113 endpoint . address
105114 end
106115
116+ # @returns [Boolean] Whether this endpoint uses a secure protocol (HTTPS or WSS).
107117 def secure?
108118 [ "https" , "wss" ] . include? ( self . scheme )
109119 end
110120
121+ # @returns [Protocol] The protocol to use for this endpoint.
111122 def protocol
112123 @options . fetch ( :protocol ) do
113124 if secure?
@@ -118,14 +129,17 @@ def protocol
118129 end
119130 end
120131
132+ # @returns [Integer] The default port for this endpoint's scheme.
121133 def default_port
122134 secure? ? 443 : 80
123135 end
124136
137+ # @returns [Boolean] Whether the endpoint's port is the default for its scheme.
125138 def default_port?
126139 port == default_port
127140 end
128141
142+ # @returns [Integer] The port number for this endpoint.
129143 def port
130144 @options [ :port ] || @url . port || default_port
131145 end
@@ -135,10 +149,12 @@ def hostname
135149 @options [ :hostname ] || @url . hostname
136150 end
137151
152+ # @returns [String] The URL scheme, e.g. `"http"` or `"https"`.
138153 def scheme
139154 @options [ :scheme ] || @url . scheme
140155 end
141156
157+ # @returns [String] The authority component (hostname and optional port).
142158 def authority ( ignore_default_port = true )
143159 if ignore_default_port and default_port?
144160 @url . hostname
@@ -158,10 +174,12 @@ def path
158174 return buffer
159175 end
160176
177+ # @returns [Array(String)] The ALPN protocol names for TLS negotiation.
161178 def alpn_protocols
162179 @options . fetch ( :alpn_protocols ) { self . protocol . names }
163180 end
164181
182+ # @returns [Boolean] Whether the endpoint refers to a localhost address.
165183 def localhost?
166184 @url . hostname =~ /^(.*?\. )?localhost\. ?$/
167185 end
@@ -175,6 +193,7 @@ def ssl_verify_mode
175193 end
176194 end
177195
196+ # @returns [OpenSSL::SSL::SSLContext] The SSL context for TLS connections.
178197 def ssl_context
179198 @options [ :ssl_context ] || OpenSSL ::SSL ::SSLContext . new . tap do |context |
180199 if alpn_protocols = self . alpn_protocols
@@ -187,6 +206,9 @@ def ssl_context
187206 end
188207 end
189208
209+ # Build a suitable endpoint, optionally wrapping in TLS for secure connections.
210+ # @parameter endpoint [IO::Endpoint::Generic | Nil] An optional underlying endpoint to wrap.
211+ # @returns [IO::Endpoint::Generic] The constructed endpoint.
190212 def build_endpoint ( endpoint = nil )
191213 endpoint ||= tcp_endpoint
192214
@@ -202,22 +224,30 @@ def build_endpoint(endpoint = nil)
202224 return endpoint
203225 end
204226
227+ # @returns [IO::Endpoint::Generic] The resolved endpoint, built on demand.
205228 def endpoint
206229 @endpoint ||= build_endpoint
207230 end
208231
232+ # Set the underlying endpoint, wrapping it as needed.
233+ # @parameter endpoint [IO::Endpoint::Generic] The endpoint to assign.
209234 def endpoint = ( endpoint )
210235 @endpoint = build_endpoint ( endpoint )
211236 end
212237
238+ # Bind to the endpoint.
213239 def bind ( *arguments , &block )
214240 endpoint . bind ( *arguments , &block )
215241 end
216242
243+ # Connect to the endpoint.
217244 def connect ( &block )
218245 endpoint . connect ( &block )
219246 end
220247
248+ # Enumerate all resolved endpoints.
249+ # @yields {|endpoint| ...} Each resolved endpoint.
250+ # @parameter endpoint [Endpoint] The resolved endpoint.
221251 def each
222252 return to_enum unless block_given?
223253
@@ -226,14 +256,17 @@ def each
226256 end
227257 end
228258
259+ # @returns [Array] A key suitable for identifying this endpoint in a hash.
229260 def key
230261 [ @url , @options ]
231262 end
232263
264+ # @returns [Boolean] Whether two endpoints are equal.
233265 def eql? other
234266 self . key . eql? other . key
235267 end
236268
269+ # @returns [Integer] The hash code for this endpoint.
237270 def hash
238271 self . key . hash
239272 end
0 commit comments