Class: RESTRack::Response
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#mime_type ⇒ Object
readonly
Returns the value of attribute mime_type.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
-
#initialize(request) ⇒ Response
constructor
A new instance of Response.
- #output ⇒ Object
Constructor Details
permalink #initialize(request) ⇒ Response
Returns a new instance of Response.
5 6 7 8 9 10 11 12 13 14 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/restrack/response.rb', line 5 def initialize(request) @request = request begin @request.prepare RESTRack.log.debug "{#{@request.request_id}} Retrieving Output" if RESTRack::CONFIG[:CORS] and @request.request.env['REQUEST_METHOD'] == 'OPTIONS' @body = '' @status = 200 else @body = @request.active_controller.call @status = body.blank? ? 204 : 200 end RESTRack.log.debug "(#{@request.request_id}) HTTP200OK '#{@request.mime_type.to_s}' response data:\n" + @body.inspect RESTRack.request_log.info "(#{@request.request_id}) HTTP200OK" rescue Exception => exception # This will log the returned status code if @request && @request.request_id RESTRack.request_log.info "(#{@request.request_id}) #{exception.class.to_s} " + exception. else RESTRack.request_log.info "(<nil-reqid>) #{exception.class.to_s} " + exception. end case when exception.is_a?( HTTP400BadRequest ) @status = 400 @body = exception. || 'The request cannot be fulfilled due to bad syntax.' when exception.is_a?( HTTP401Unauthorized ) @status = 401 @body = exception. || 'You have failed authentication for access to the resource.' when exception.is_a?( HTTP403Forbidden ) @status = 403 @body = exception. || 'You are forbidden to access that resource.' when exception.is_a?( HTTP404ResourceNotFound ) @status = 404 @body = exception. || 'The resource you requested could not be found.' when exception.is_a?( HTTP405MethodNotAllowed ) @status = 405 @body = exception. || 'The resource you requested does not support the request method provided.' when exception.is_a?( HTTP409Conflict ) @status = 409 @body = exception. || 'The resource you requested is in a conflicted state.' when exception.is_a?( HTTP410Gone ) @status = 410 @body = exception. || 'The resource you requested is no longer available.' when exception.is_a?( HTTP422ResourceInvalid ) @status = 422 @body = exception. || 'Invalid attribute values sent for resource.' when exception.is_a?( HTTP502BadGateway ) @status = 502 @body = exception. || 'The server was acting as a gateway or proxy and received an invalid response from the upstream server.' log_server_warning(exception) else # HTTP500ServerError server_error(exception) end # case Exception end # begin / rescue @mime_type = MIME::Type.new(@request.mime_type) @content_type = @request.content_type end |
Instance Attribute Details
permalink #body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/restrack/response.rb', line 3 def body @body end |
permalink #content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
3 4 5 |
# File 'lib/restrack/response.rb', line 3 def content_type @content_type end |
permalink #headers ⇒ Object (readonly)
Returns the value of attribute headers.
3 4 5 |
# File 'lib/restrack/response.rb', line 3 def headers @headers end |
permalink #mime_type ⇒ Object (readonly)
Returns the value of attribute mime_type.
3 4 5 |
# File 'lib/restrack/response.rb', line 3 def mime_type @mime_type end |
Instance Method Details
permalink #output ⇒ Object
[View source]
63 64 65 66 67 68 69 70 71 |
# File 'lib/restrack/response.rb', line 63 def output @headers ||= {} @headers['Content-Type'] = content_type if RESTRack::CONFIG[:CORS] @headers['Access-Control-Allow-Origin'] = RESTRack::CONFIG[:CORS]['Access-Control-Allow-Origin'] if RESTRack::CONFIG[:CORS]['Access-Control-Allow-Origin'] @headers['Access-Control-Allow-Methods'] = RESTRack::CONFIG[:CORS]['Access-Control-Allow-Methods'] if RESTRack::CONFIG[:CORS]['Access-Control-Allow-Methods'] end return [status, headers, [package(body)] ] end |