class Webmachine::Adapters::Rack3::RequestBody
Wraps the Rack
input so it can be treated like a String or Enumerable. @api private
Public Class Methods
Source
# File lib/webmachine/adapters/rack3_adapter.rb, line 106 def initialize(request) @request = request end
@param [Rack::Request] request the Rack
request
Public Instance Methods
Source
# File lib/webmachine/adapters/rack3_adapter.rb, line 140 def each if @value @value.each { |chunk| yield chunk } elsif @request.body.respond_to?(:each) @value = [] @request.body.each { |chunk| @value << chunk yield chunk } elsif @request.body.respond_to?(:to_ary) @value = @request.body.to_ary @value.each { |chunk| yield chunk } else yield @request.body end end
Iterates over the body in chunks. If the body has previously been read, this method can be called again and get the same sequence of chunks. @yield [chunk] @yieldparam [String] chunk a chunk of the request body
Source
# File lib/webmachine/adapters/rack3_adapter.rb, line 115 def to_io @request.body end
Rack
Servers differ in the way you can access their request bodys. While some allow you to directly get a Ruby IO object others don’t. You have to check the methods they expose, like gets, read, each
, rewind and maybe others. See: github.com/rack/rack/blob/rack-1.5/lib/rack/lint.rb#L296 @return [IO] the request body
Source
# File lib/webmachine/adapters/rack3_adapter.rb, line 122 def to_s if @value @value.join elsif @request.body.respond_to?(:to_ary) @request.body.to_ary.join elsif @request.body.respond_to?(:read) @request.body.rewind if @request.body.respond_to?(:rewind) @request.body.read else @request.body&.to_s || "" end end
Converts the body to a String so you can work with the entire thing. @return [String] the request body as a string