Class: Doze::Responder

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/doze/responder.rb

Direct Known Subclasses

Error, Main, Resource

Defined Under Namespace

Classes: Error, Main, Resource

Constant Summary

Constants included from Utils

Utils::URI_SCHEMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#escape, #quote, #request_base_uri, #unescape

Constructor Details

#initialize(app, request) ⇒ Responder

Returns a new instance of Responder.



6
7
8
9
# File 'lib/doze/responder.rb', line 6

def initialize(app, request)
  @app = app
  @request = request
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/doze/responder.rb', line 4

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.

Raises:

  • (NotImplementedError)


4
5
6
# File 'lib/doze/responder.rb', line 4

def response
  @response
end

Instance Method Details

#auth_failed_responseObject



52
53
54
55
56
57
# File 'lib/doze/responder.rb', line 52

def auth_failed_response
  error_response(@request.session_authenticated? ?
              STATUS_FORBIDDEN :   # this one, 403, really means 'unauthorized', ie
              STATUS_UNAUTHORIZED  # http status code 401 called 'unauthorized' but really used to mean 'unauthenticated'
  )
end

#callObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/doze/responder.rb', line 31

def call
  begin
    response().finish
  rescue Doze::Error => error
    error_response_from_error(error).finish
  rescue => exception
    raise unless @app.config[:catch_application_errors]
    lines = ["#{exception.class}: #{exception.message}", *exception.backtrace].join("\n")
    @app.logger << lines
    if @app.config[:expose_exception_details]
      error_response(STATUS_INTERNAL_SERVER_ERROR, exception.message, {}, exception.backtrace).finish
    else
      error_response.finish
    end
  end
end

#error_response(status = STATUS_INTERNAL_SERVER_ERROR, message = nil, headers = {}, backtrace = nil) ⇒ Object



23
24
25
# File 'lib/doze/responder.rb', line 23

def error_response(status=STATUS_INTERNAL_SERVER_ERROR, message=nil, headers={}, backtrace=nil)
  error_response_from_error(Doze::Error.new(status, message, headers, backtrace))
end

#error_response_from_error(error) ⇒ Object



27
28
29
# File 'lib/doze/responder.rb', line 27

def error_response_from_error(error)
  Doze::Responder::Error.new(@app, @request, error).response
end

#raise_error(status = STATUS_INTERNAL_SERVER_ERROR, message = nil, headers = {}) ⇒ Object

for use within #response

Raises:



19
20
21
# File 'lib/doze/responder.rb', line 19

def raise_error(status=STATUS_INTERNAL_SERVER_ERROR, message=nil, headers={})
  raise Doze::Error.new(status, message, headers)
end

#recognized_methodObject



11
12
13
14
15
16
# File 'lib/doze/responder.rb', line 11

def recognized_method
  @recognized_method ||= begin
    method = @request.normalized_request_method
    ([:options] + @app.config[:recognized_methods]).find {|m| m.to_s == method} or raise_error(STATUS_NOT_IMPLEMENTED)
  end
end