77require "protocol/http/body/buffered"
88require "protocol/http/body/file"
99
10+ require_relative "../adapter/version"
11+
1012module Protocol
1113 module Rack
1214 module Body
@@ -17,18 +19,30 @@ class Enumerable < ::Protocol::HTTP::Body::Readable
1719 # The content-length header key.
1820 CONTENT_LENGTH = "content-length" . freeze
1921
20- # Wraps a Rack response body into an {Enumerable} instance.
21- # If the body is an Array, its total size is calculated automatically.
22- #
23- # @parameter body [Object] The Rack response body that responds to `each`.
24- # @parameter length [Integer] Optional content length of the response body.
25- # @returns [Enumerable] A new enumerable body instance.
26- def self . wrap ( body , length = nil )
27- if body . respond_to? ( :to_ary )
28- # This avoids allocating an enumerator, which is more efficient:
29- return ::Protocol ::HTTP ::Body ::Buffered . new ( body . to_ary , length )
30- else
31- return self . new ( body , length )
22+ if Adapter ::VERSION >= "3"
23+ # Wraps a Rack response body into an {Enumerable} instance.
24+ # If the body is an Array, its total size is calculated automatically.
25+ #
26+ # @parameter body [Object] The Rack response body that responds to `each`.
27+ # @parameter length [Integer] Optional content length of the response body.
28+ # @returns [Enumerable] A new enumerable body instance.
29+ def self . wrap ( body , length = nil )
30+ if body . respond_to? ( :to_ary )
31+ # This avoids allocating an enumerator, which is more efficient:
32+ return ::Protocol ::HTTP ::Body ::Buffered . new ( body . to_ary , length )
33+ else
34+ return self . new ( body , length )
35+ end
36+ end
37+ else
38+ def self . wrap ( body , length = nil )
39+ # Rack 2 does not specify or implement `to_ary` behaviour correctly, so the best we can do is check if it's an Array directly:
40+ if body . is_a? ( Array )
41+ # This avoids allocating an enumerator, which is more efficient:
42+ return ::Protocol ::HTTP ::Body ::Buffered . new ( body , length )
43+ else
44+ return self . new ( body , length )
45+ end
3246 end
3347 end
3448
0 commit comments