class Seahorse::Client::Http::Response
def body
-
(IO)
-
def body @body end
def body=(io)
-
io
(#read, #size, #rewind
) --
def body=(io) @body = case io when nil then StringIO.new('') when String then StringIO.new(io) else io end end
def body_contents
-
(String|Array)
-
def body_contents if body.is_a?(Array) # an array of parsed events body else body.rewind contents = body.read body.rewind contents end end
def emit(event_name, *args)
def emit(event_name, *args) @listeners[event_name].each { |listener| listener.call(*args) } end
def initialize(options = {})
(**options)
-
:body
(IO
) -- -
:headers
(Headers
) -- -
:status_code
(Integer
) --
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
def listener(range, callback)
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
def on_data(&callback)
def on_data(&callback) @listeners[:data] << callback end
def on_done(status_code_range = nil, &callback)
def on_done(status_code_range = nil, &callback) listener = listener(status_code_range, callback) if @done listener.call else @listeners[:done] << listener end end
def on_error(&callback)
def on_error(&callback) on_done(0..599) do if @error yield(@error) end end end
def on_headers(status_code_range = nil, &block)
def on_headers(status_code_range = nil, &block) @listeners[:headers] << listener(status_code_range, block) end
def on_success(status_code_range = 200..599, &callback)
def on_success(status_code_range = 200..599, &callback) on_done(status_code_range) do unless @error yield end end end
def reset
def reset @status_code = 0 @headers.clear @body.truncate(0) @error = nil end
def signal_data(chunk)
-
chunk
(string
) --
def signal_data(chunk) unless chunk == '' @body.write(chunk) emit(:data, chunk) end end
def signal_done(options = {})
(**options)
-
:body
(required, String
) -- -
:headers
(required, Hash
) -- -
:status_code
(required, Integer
) --
Overloads:
-
signal_done(options = {})
-
signal_done()
Other tags:
- Example: Complete the response in parts -
Example: Completing the response in a single call -
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, '\ 'and :body' raise ArgumentError, msg end end
def signal_error(networking_error)
-
networking_error
(StandardError
) --
def signal_error(networking_error) @error = networking_error signal_done end
def signal_headers(status_code, headers)
-
headers
(Hash
) -- -
status_code
(Integer
) --
def signal_headers(status_code, headers) @status_code = status_code @headers = Headers.new(headers) emit(:headers, @status_code, @headers) end