Class: Restfulness::Response
- Inherits:
-
Object
- Object
- Restfulness::Response
- Defined in:
- lib/restfulness/response.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Outgoing data.
-
#payload ⇒ Object
readonly
Outgoing data.
-
#request ⇒ Object
readonly
Incoming data.
-
#resource ⇒ Object
readonly
The generated resource object.
-
#status ⇒ Object
Outgoing data.
Instance Method Summary collapse
- #content_length ⇒ Object
-
#initialize(request) ⇒ Response
constructor
A new instance of Response.
- #run ⇒ Object
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
#headers ⇒ Object (readonly)
Outgoing data
13 14 15 |
# File 'lib/restfulness/response.rb', line 13 def headers @headers end |
#payload ⇒ Object
Outgoing data
13 14 15 |
# File 'lib/restfulness/response.rb', line 13 def payload @payload end |
#request ⇒ Object (readonly)
Incoming data
7 8 9 |
# File 'lib/restfulness/response.rb', line 7 def request @request end |
#resource ⇒ Object
The generated resource object
10 11 12 |
# File 'lib/restfulness/response.rb', line 10 def resource @resource end |
#status ⇒ Object
Outgoing data
13 14 15 |
# File 'lib/restfulness/response.rb', line 13 def status @status end |
Instance Method Details
#content_length ⇒ Object
51 52 53 |
# File 'lib/restfulness/response.rb', line 51 def content_length payload.to_s.bytesize.to_s end |
#run ⇒ Object
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. + "\n") ensure log! if status end |