Class: RESTRack::Response

Inherits:
Object show all
Defined in:
lib/restrack/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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.message
    else
      RESTRack.request_log.info "(<nil-reqid>) #{exception.class.to_s} " + exception.message
    end
    case
      when exception.is_a?( HTTP400BadRequest )
        @status = 400
        @body = exception.message || 'The request cannot be fulfilled due to bad syntax.'
      when exception.is_a?( HTTP401Unauthorized )
        @status = 401
        @body = exception.message || 'You have failed authentication for access to the resource.'
      when exception.is_a?( HTTP403Forbidden )
        @status = 403
        @body = exception.message || 'You are forbidden to access that resource.'
      when exception.is_a?( HTTP404ResourceNotFound )
        @status = 404
        @body = exception.message || 'The resource you requested could not be found.'
      when exception.is_a?( HTTP405MethodNotAllowed )
        @status = 405
        @body = exception.message || 'The resource you requested does not support the request method provided.'
      when exception.is_a?( HTTP409Conflict )
        @status = 409
        @body = exception.message || 'The resource you requested is in a conflicted state.'
      when exception.is_a?( HTTP410Gone )
        @status = 410
        @body = exception.message || 'The resource you requested is no longer available.'
      when exception.is_a?( HTTP422ResourceInvalid )
        @status = 422
        @body = exception.message || 'Invalid attribute values sent for resource.'
      when exception.is_a?( HTTP502BadGateway )
        @status = 502
        @body = exception.message || '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

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/restrack/response.rb', line 3

def body
  @body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



3
4
5
# File 'lib/restrack/response.rb', line 3

def content_type
  @content_type
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/restrack/response.rb', line 3

def headers
  @headers
end

#mime_typeObject (readonly)

Returns the value of attribute mime_type.



3
4
5
# File 'lib/restrack/response.rb', line 3

def mime_type
  @mime_type
end

#requestObject (readonly)

Returns the value of attribute request.



3
4
5
# File 'lib/restrack/response.rb', line 3

def request
  @request
end

#statusObject (readonly)

Returns the value of attribute status.



3
4
5
# File 'lib/restrack/response.rb', line 3

def status
  @status
end

Instance Method Details

#outputObject



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