Class: ReactOnRailsPro::RendererHttpClient::Response

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

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, error: nil, &executor) ⇒ Response

Returns a new instance of Response.



84
85
86
87
88
89
90
91
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 84

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

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



82
83
84
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 82

def status
  @status
end

Instance Method Details

#bodyObject



93
94
95
96
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 93

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

#each(&block) ⇒ Object

Raises:

  • (@error)


103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 103

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)


98
99
100
101
# File 'lib/react_on_rails_pro/renderer_http_client.rb', line 98

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