Class: ReactOnRailsPro::RendererHttpClient::MultipartBody

Inherits:
Protocol::HTTP::Body::Readable
  • Object
show all
Defined in:
lib/react_on_rails_pro/renderer_http_client.rb,
sig/react_on_rails_pro/renderer_http_client.rbs

Instance Method Summary collapse

Constructor Details

#initializeMultipartBody

Returns a new instance of MultipartBody.



152
153
154
155
156
157
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 152

def initialize
  super()
  @chunks = []
  @index = 0
  @closed = false
end

Instance Method Details

#<<(chunk) ⇒ Object

Parameters:

  • chunk (Object)

Returns:

  • (Object)


159
160
161
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 159

def <<(chunk)
  @chunks << (chunk.respond_to?(:read) ? chunk : chunk.to_s.b)
end

#bytesizeInteger?

Returns:

  • (Integer, nil)


188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 188

def bytesize
  total = 0
  index = 0
  while index < @chunks.length
    chunk = @chunks[index]
    chunk_size = chunk.respond_to?(:bytesize) ? chunk.bytesize : nil
    return unless chunk_size.is_a?(Integer)

    total += chunk_size
    index += 1
  end
  total
end

#close(error = nil) ⇒ Object

Parameters:

  • error (StandardError, nil) (defaults to: nil)

Returns:

  • (Object)


182
183
184
185
186
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 182

def close(error = nil)
  @closed = true
  @chunks[@index..]&.each { |chunk| chunk.close if chunk.respond_to?(:close) }
  super
end

#readString?

Returns:

  • (String, nil)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 163

def read
  return if @closed

  while @index < @chunks.length
    chunk = @chunks[@index]
    if chunk.respond_to?(:read)
      data = chunk.read
      return data if data

      chunk.close if chunk.respond_to?(:close)
      @index += 1
      next
    end

    @index += 1
    return chunk
  end
end