Module: UU::OS::Util::ResponseBuilder
- Included in:
- Application::ErrorHandlingMiddleware, Server::AbstractServlet
- Defined in:
- uu_os_connection-server-2.3.1/lib/uu/os/util/response_builder.rb
Instance Method Summary (collapse)
-
- (Object) handle_response(result)
Method for handling response from controller.
-
- (Object) response(status, headers, body)
Method for construction of custom response.
-
- (Object) streamed_response(headers, &block)
Method allows asynchronous streaming of body with HTTP status code 200.
Instance Method Details
- (Object) handle_response(result)
Method for handling response from controller.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'uu_os_connection-server-2.3.1/lib/uu/os/util/response_builder.rb', line 50 def handle_response(result) if result.nil? return response(204, {}, result) elsif result.kind_of? UU::OS::Lang::BinaryValue name = result.name size = result.size content_type = result.content_type headers = {} headers[:content_type] = content_type || 'application/octet-stream' headers[:content_disposition] = 'attachment' headers[:content_disposition] << "; filename=\"#{name}\"" if name headers[:content_length] = size if size return response(200, headers, result.content) elsif result.kind_of?(Array) && (result.size == 3) && result[0].kind_of?(Fixnum) && result[1].kind_of?(Hash) && result[2].respond_to?(:each) return result elsif result.respond_to?(:read) return response(200, {content_type: 'application/json'}, result) else return response(200, {content_type: 'application/json'}, result.to_json) end end |
- (Object) response(status, headers, body)
Method for construction of custom response.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'uu_os_connection-server-2.3.1/lib/uu/os/util/response_builder.rb', line 15 def response(status, headers, body) if !headers.nil? && !headers.kind_of?(Hash) raise "invalid headers, expected hash but was #{headers.class}" end if !body.nil? && !body.kind_of?(String) && !body.respond_to?(:read) raise "cannot send #{body.class} as response" end headers ||= {} headers = headers.inject({}) do |tmp, (key, value)| key = key.to_s.gsub('_', '-').downcase if value.kind_of?(Array) tmp[key] = value.join(',') else tmp[key] = value.to_s end tmp end headers['content-type'] ||= 'application/octet-stream' body = body || '' if body.respond_to?(:read) body = ChunkedIO.new(body) elsif !body.respond_to?(:each) body = [body] end # Actual support of streaming depends on particular implementation of # rack handler. Body is read using each, however handler implementation # provide output into which body is written. return [status.to_i, headers, body] end |
- (Object) streamed_response(headers, &block)
Method allows asynchronous streaming of body with HTTP status code 200.
78 79 80 81 82 83 84 85 86 |
# File 'uu_os_connection-server-2.3.1/lib/uu/os/util/response_builder.rb', line 78 def streamed_response(headers, &block) raise 'no block' if block.nil? read, write = IO.pipe write.write('') Thread.new { block.yield(write) } return response(200, headers, read) end |