Class: Seahorse::Client::Http::Response
- Inherits:
-
Object
- Object
- Seahorse::Client::Http::Response
- Defined in:
- lib/seahorse/client/http/response.rb
Instance Attribute Summary collapse
- #body ⇒ IO
- #error ⇒ StandardError? readonly
- #headers ⇒ Headers
-
#status_code ⇒ Integer
Returns ‘0` if the request failed to generate any response.
Instance Method Summary collapse
- #body_contents ⇒ String
-
#initialize(options = {}) ⇒ Response
constructor
A new instance of Response.
- #on_data(&callback) ⇒ Object
- #on_done(status_code_range, &callback) ⇒ Object
- #on_error(&callback) ⇒ Object
- #on_headers(status_code_range = nil, &block) ⇒ Object
- #on_success(status_code_range = 200..599, &callback) ⇒ Object
- #reset ⇒ Object
- #signal_data(chunk) ⇒ Object
-
#signal_done(options = {}) ⇒ Object
Completes the http response.
- #signal_error(networking_error) ⇒ Object
- #signal_headers(status_code, headers) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Response
Returns a new instance of Response.
9 10 11 12 13 14 15 16 17 |
# File 'lib/seahorse/client/http/response.rb', line 9 def initialize( = {}) @status_code = [:status_code] || 0 @headers = [:headers] || Headers.new @body = [:body] || StringIO.new @listeners = Hash.new { |h,k| h[k] = [] } @complete = false @done = nil @error = nil end |
Instance Attribute Details
#body ⇒ IO
27 28 29 |
# File 'lib/seahorse/client/http/response.rb', line 27 def body @body end |
#error ⇒ StandardError? (readonly)
30 31 32 |
# File 'lib/seahorse/client/http/response.rb', line 30 def error @error end |
#headers ⇒ Headers
24 25 26 |
# File 'lib/seahorse/client/http/response.rb', line 24 def headers @headers end |
#status_code ⇒ Integer
Returns ‘0` if the request failed to generate any response.
21 22 23 |
# File 'lib/seahorse/client/http/response.rb', line 21 def status_code @status_code end |
Instance Method Details
#body_contents ⇒ String
42 43 44 45 46 47 |
# File 'lib/seahorse/client/http/response.rb', line 42 def body_contents body.rewind contents = body.read body.rewind contents end |
#on_data(&callback) ⇒ Object
121 122 123 |
# File 'lib/seahorse/client/http/response.rb', line 121 def on_data(&callback) @listeners[:data] << Proc.new end |
#on_done(status_code_range, &callback) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/seahorse/client/http/response.rb', line 125 def on_done(status_code_range, &callback) listener = listener(status_code_range, Proc.new) if @done listener.call else @listeners[:done] << listener end end |
#on_error(&callback) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/seahorse/client/http/response.rb', line 142 def on_error(&callback) on_done(0..599) do if @error yield(@error) end end end |
#on_headers(status_code_range = nil, &block) ⇒ Object
117 118 119 |
# File 'lib/seahorse/client/http/response.rb', line 117 def on_headers(status_code_range = nil, &block) @listeners[:headers] << listener(status_code_range, Proc.new) end |
#on_success(status_code_range = 200..599, &callback) ⇒ Object
134 135 136 137 138 139 140 |
# File 'lib/seahorse/client/http/response.rb', line 134 def on_success(status_code_range = 200..599, &callback) on_done(status_code_range) do unless @error yield end end end |
#reset ⇒ Object
150 151 152 153 154 155 |
# File 'lib/seahorse/client/http/response.rb', line 150 def reset @status_code = 0 @headers.clear @body.truncate(0) @error = nil end |
#signal_data(chunk) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/seahorse/client/http/response.rb', line 58 def signal_data(chunk) unless chunk == '' @body.write(chunk) emit(:data, chunk) end end |
#signal_done ⇒ Object #signal_done(options = {}) ⇒ Object
Completes the http response.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/seahorse/client/http/response.rb', line 95 def signal_done( = {}) if .keys.sort == [:body, :headers, :status_code] signal_headers([:status_code], [:headers]) signal_data([:body]) signal_done elsif .empty? @body.rewind if @body.respond_to?(:rewind) @done = true emit(:done) else msg = "options must be empty or must contain :status_code, :headers, " msg << "and :body" raise ArgumentError, msg end end |
#signal_error(networking_error) ⇒ Object
112 113 114 115 |
# File 'lib/seahorse/client/http/response.rb', line 112 def signal_error(networking_error) @error = networking_error signal_done end |