Class: ReactOnRailsPro::RendererHttpClient::Response

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

Overview

Not thread-safe. Each Response instance is owned and consumed by one renderer request path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: nil, body: nil, headers: nil, error: nil, &executor) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status: (Integer, nil) (defaults to: nil)
  • body: (Array[String], String, nil) (defaults to: nil)
  • error: (StandardError, nil) (defaults to: nil)


340
341
342
343
344
345
346
347
348
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 340

def initialize(status: nil, body: nil, headers: nil, error: nil, &executor)
  @status = status
  @headers = normalize_headers(headers)
  @body_chunks = Array(body || [])
  @body = nil
  @error = error
  @executor = executor
  @consumed = false
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



338
339
340
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 338

def headers
  @headers
end

#statusInteger? (readonly)

Returns the value of attribute status.

Returns:

  • (Integer, nil)


338
339
340
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 338

def status
  @status
end

Instance Method Details

#bodyString

Returns:

  • (String)


350
351
352
353
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 350

def body
  consume unless @consumed
  @body ||= @body_chunks.join
end

#eachEnumerator[String, void] #eachObject

Overloads:

  • #eachEnumerator[String, void]

    Returns:

    • (Enumerator[String, void])
  • #eachObject

    Returns:

    • (Object)

Yields:

Yield Parameters:

  • arg0 (String)

Yield Returns:

  • (Object)

Raises:

  • (@error)


360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 360

def each(&block)
  return enum_for(:each) unless block

  if @executor && !@consumed
    consume(&block)
  else
    @body_chunks.each(&block)
  end

  # Replaying consumed chunks should still surface the stored response failure.
  raise @error if @error
  raise HTTPError, self if error?
end

#error?Boolean

Returns:

  • (Boolean)


355
356
357
358
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 355

def error?
  # nil status means a lazy streaming executor has not run yet, so it is not-yet-an-error.
  !status.nil? && status >= 400
end