Class: Restfulness::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Response

Returns a new instance of Response.



16
17
18
19
# File 'lib/restfulness/response.rb', line 16

def initialize(request)
  @request = request
  @headers = {}
end

Instance Attribute Details

#headersObject (readonly)

Outgoing data



13
14
15
# File 'lib/restfulness/response.rb', line 13

def headers
  @headers
end

#payloadObject

Outgoing data



13
14
15
# File 'lib/restfulness/response.rb', line 13

def payload
  @payload
end

#requestObject (readonly)

Incoming data



7
8
9
# File 'lib/restfulness/response.rb', line 7

def request
  @request
end

#resourceObject

The generated resource object



10
11
12
# File 'lib/restfulness/response.rb', line 10

def resource
  @resource
end

#statusObject

Outgoing data



13
14
15
# File 'lib/restfulness/response.rb', line 13

def status
  @status
end

Instance Method Details

#content_lengthObject



51
52
53
# File 'lib/restfulness/response.rb', line 51

def content_length
  payload.to_s.bytesize.to_s
end

#runObject



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
# File 'lib/restfulness/response.rb', line 21

def run
  @log_begin_at = Time.now
  route = request.route
  if route
    self.resource = route.build_resource(request, self)

    # run callbacks, if any fail, they'll raise an error
    resource.check_callbacks

    # Perform the actual work
    result = resource.call

    update_status_and_payload(result.nil? ? 204 : 200, result)
  else
    update_status_and_payload(404)
  end

rescue HTTPException => e # Deal with HTTP exceptions
  headers.update(e.headers)
  update_status_and_payload(e.status, e.payload)

rescue StandardError, LoadError, SyntaxError => e
  # Useful coding error handling, with backtrace
  log_exception(e)
  update_status_and_payload(500, e.message + "\n")

ensure
  log! if status
end