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 11
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 32
def body
  @body
end
body=(io) click to toggle source

@param [#read, size, rewind] io

# File lib/seahorse/client/http/response.rb, line 37
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|Array]

# File lib/seahorse/client/http/response.rb, line 46
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
on_data(&callback) click to toggle source
# File lib/seahorse/client/http/response.rb, line 130
def on_data(&callback)
  @listeners[:data] << callback
end
on_done(status_code_range = nil, &callback) click to toggle source
# File lib/seahorse/client/http/response.rb, line 134
def on_done(status_code_range = nil, &callback)
  listener = listener(status_code_range, callback)
  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 151
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 126
def on_headers(status_code_range = nil, &block)
  @listeners[:headers] << listener(status_code_range, block)
end
on_success(status_code_range = 200..599) { || ... } click to toggle source
# File lib/seahorse/client/http/response.rb, line 143
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 159
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 67
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 single 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 104
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
signal_error(networking_error) click to toggle source

@param [StandardError] networking_error

# File lib/seahorse/client/http/response.rb, line 121
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 60
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 181
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 168
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