Class: Webmachine::Adapters::Rack3::RequestBody Private

Inherits:
Object
  • Object
show all
Defined in:
lib/webmachine/adapters/rack3_adapter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wraps the Rack input so it can be treated like a String or Enumerable.

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ RequestBody

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of RequestBody.

Parameters:

  • request (Rack::Request)

    the Rack request



106
107
108
# File 'lib/webmachine/adapters/rack3_adapter.rb', line 106

def initialize(request)
  @request = request
end

Instance Method Details

#each {|chunk| ... } ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Yields:

  • (chunk)

Yield Parameters:

  • chunk (String)

    a chunk of the request body



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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

#to_ioIO

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (IO)

    the request body



115
116
117
# File 'lib/webmachine/adapters/rack3_adapter.rb', line 115

def to_io
  @request.body
end

#to_sString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the body to a String so you can work with the entire thing.

Returns:

  • (String)

    the request body as a string



122
123
124
125
126
127
128
129
130
131
132
133
# 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