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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response

Options Hash (options):

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

9
10
11
12
13
14
15
# File 'aws-sdk-core/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
end

Instance Attribute Details

#bodyIO


25
26
27
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 25

def body
  @body
end

#errorStandardError? (readonly)


28
29
30
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 28

def error
  @error
end

#headersHeaders


22
23
24
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 22

def headers
  @headers
end

#status_codeInteger


19
20
21
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 19

def status_code
  @status_code
end

Instance Method Details

#body_contentsString


40
41
42
43
44
45
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 40

def body_contents
  body.rewind
  contents = body.read
  body.rewind
  contents
end

#on_data(&callback) ⇒ Object


119
120
121
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 119

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

#on_done(status_code_range, &callback) ⇒ Object


123
124
125
126
127
128
129
130
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 123

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


140
141
142
143
144
145
146
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 140

def on_error(&callback)
  on_done(0..599) do
    if @error
      yield(@error)
    end
  end
end

#on_headers(status_code_range = nil, &block) ⇒ Object


115
116
117
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 115

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


132
133
134
135
136
137
138
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 132

def on_success(status_code_range = 200..599, &callback)
  on_done(status_code_range) do
    unless @error
      yield
    end
  end
end

#resetObject


148
149
150
151
152
153
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 148

def reset
  @status_code = 0
  @headers.clear
  @body.truncate(0)
  @error = nil
end

#signal_data(chunk) ⇒ Object


56
57
58
59
60
61
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 56

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 singal 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)

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 93

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) ⇒ Object


110
111
112
113
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 110

def signal_error(networking_error)
  @error = networking_error
  signal_done
end

#signal_headers(status_code, headers) ⇒ Object


49
50
51
52
53
# File 'aws-sdk-core/lib/seahorse/client/http/response.rb', line 49

def signal_headers(status_code, headers)
  @status_code = status_code
  @headers = Headers.new(headers)
  emit(:headers, @status_code, @headers)
end