class Seahorse::Client::Http::Response
Attributes
error[R]
@return [StandardError, nil]
headers[RW]
@return [Headers]
status_code[RW]
@return [Integer] Returns `0` if the request failed to generate
any response.
Public Class Methods
new(options = {})
click to toggle source
@option options [Integer] :status_code (0) @option options [Headers] :headers (Headers.new) @option options [IO] :body (StringIO.new)
# File lib/seahorse/client/http/response.rb, line 9 def initialize(options = {}) @status_code = options[:status_code] || 0 @headers = options[:headers] || Headers.new @body = options[:body] || StringIO.new @listeners = Hash.new { |h,k| h[k] = [] } @complete = false @done = nil @error = nil end
Public Instance Methods
body()
click to toggle source
@return [IO]
# File lib/seahorse/client/http/response.rb, line 30 def body @body end
body=(io)
click to toggle source
@param [#read, size, rewind] io
# File lib/seahorse/client/http/response.rb, line 35 def body=(io) @body = case io when nil then StringIO.new('') when String then StringIO.new(io) else io end end
body_contents()
click to toggle source
@return [String]
# File lib/seahorse/client/http/response.rb, line 44 def body_contents body.rewind contents = body.read body.rewind contents end
on_data(&callback)
click to toggle source
# File lib/seahorse/client/http/response.rb, line 123 def on_data(&callback) @listeners[:data] << Proc.new end
on_done(status_code_range, &callback)
click to toggle source
# File lib/seahorse/client/http/response.rb, line 127 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() { |error| ... }
click to toggle source
# File lib/seahorse/client/http/response.rb, line 144 def on_error(&callback) on_done(0..599) do if @error yield(@error) end end end
on_headers(status_code_range = nil, &block)
click to toggle source
# File lib/seahorse/client/http/response.rb, line 119 def on_headers(status_code_range = nil, &block) @listeners[:headers] << listener(status_code_range, Proc.new) end
on_success(status_code_range = 200..599) { || ... }
click to toggle source
# File lib/seahorse/client/http/response.rb, line 136 def on_success(status_code_range = 200..599, &callback) on_done(status_code_range) do unless @error yield end end end
reset()
click to toggle source
# File lib/seahorse/client/http/response.rb, line 152 def reset @status_code = 0 @headers.clear @body.truncate(0) @error = nil end
signal_data(chunk)
click to toggle source
@param [string] chunk
# File lib/seahorse/client/http/response.rb, line 60 def signal_data(chunk) unless chunk == '' @body.write(chunk) emit(:data, chunk) end end
signal_done(options = {})
click to toggle source
Completes the http response.
@example Completing the response in a singal call
http_response.signal_done( status_code: 200, headers: {}, body: '' )
@example Complete the response in parts
# signal headers straight-way http_response.signal_headers(200, {}) # signal data as it is received from the socket http_response.signal_data("...") http_response.signal_data("...") http_response.signal_data("...") # signal done once the body data is all written http_response.signal_done
@overload #signal_done()
@overload #signal_done(options = {})
@option options [required, Integer] :status_code @option options [required, Hash] :headers @option options [required, String] :body
# File lib/seahorse/client/http/response.rb, line 97 def signal_done(options = {}) if options.keys.sort == [:body, :headers, :status_code] signal_headers(options[:status_code], options[:headers]) signal_data(options[:body]) signal_done elsif options.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)
click to toggle source
@param [StandardError] networking_error
# File lib/seahorse/client/http/response.rb, line 114 def signal_error(networking_error) @error = networking_error signal_done end
signal_headers(status_code, headers)
click to toggle source
@param [Integer] #status_code @param [Hash<String,String>] headers
# File lib/seahorse/client/http/response.rb, line 53 def signal_headers(status_code, headers) @status_code = status_code @headers = Headers.new(headers) emit(:headers, @status_code, @headers) end
Private Instance Methods
emit(event_name, *args)
click to toggle source
# File lib/seahorse/client/http/response.rb, line 174 def emit(event_name, *args) @listeners[event_name].each { |listener| listener.call(*args) } end
listener(range, callback)
click to toggle source
# File lib/seahorse/client/http/response.rb, line 161 def listener(range, callback) range = range..range if Integer === range if range lambda do |*args| if range.include?(@status_code) callback.call(*args) end end else callback end end