Class: Seahorse::Client::Http::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/seahorse/client/http/response.rb

Direct Known Subclasses

AsyncResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :status_code (Integer) — default: 0
  • :headers (Headers) — default: Headers.new
  • :body (IO) — default: StringIO.new


11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#errorStandardError? (readonly)

Returns:

  • (StandardError, nil)


29
30
31
# File 'lib/seahorse/client/http/response.rb', line 29

def error
  @error
end

#headersHeaders

Returns:



26
27
28
# File 'lib/seahorse/client/http/response.rb', line 26

def headers
  @headers
end

#status_codeInteger

Returns ‘0` if the request failed to generate any response.

Returns:

  • (Integer)

    Returns ‘0` if the request failed to generate any response.



23
24
25
# File 'lib/seahorse/client/http/response.rb', line 23

def status_code
  @status_code
end

Instance Method Details

#bodyIO

Returns:

  • (IO)


32
33
34
# File 'lib/seahorse/client/http/response.rb', line 32

def body
  @body
end

#body=(io) ⇒ Object

Parameters:

  • io (#read, #size, #rewind)


37
38
39
40
41
42
43
# 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_contentsString|Array

Returns:

  • (String|Array)


46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



130
131
132
# File 'lib/seahorse/client/http/response.rb', line 130

def on_data(&callback)
  @listeners[:data] << callback
end

#on_done(status_code_range = nil, &callback) ⇒ Object



134
135
136
137
138
139
140
141
# 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(&callback) ⇒ Object



151
152
153
154
155
156
157
# 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) ⇒ Object



126
127
128
# 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, &callback) ⇒ Object



143
144
145
146
147
148
149
# 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

#resetObject



159
160
161
162
163
164
# 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) ⇒ Object

Parameters:

  • chunk (string)


67
68
69
70
71
72
# File 'lib/seahorse/client/http/response.rb', line 67

def signal_data(chunk)
  unless chunk == ''
    @body.write(chunk)
    emit(:data, chunk)
  end
end

#signal_doneObject #signal_done(options = {}) ⇒ Object

Completes the http response.

Examples:

Completing the response in a single call


http_response.signal_done(
  status_code: 200,
  headers: {},
  body: ''
)

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

Overloads:

  • #signal_done(options = {}) ⇒ Object

    Options Hash (options):

    • :status_code (required, Integer)
    • :headers (required, Hash)
    • :body (required, String)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object

Parameters:

  • networking_error (StandardError)


121
122
123
124
# 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) ⇒ Object

Parameters:

  • status_code (Integer)
  • headers (Hash<String,String>)


60
61
62
63
64
# 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