Class: Doze::Application

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

Constant Summary collapse

DEFAULT_CONFIG =
{
  :error_resource_class => Doze::Resource::Error,

  # Setting this to false is useful for testing, so an exception can make a test fail via
  # the normal channels rather than having to check and parse it out of a response.
  :catch_application_errors => true,

  # useful for development
  :expose_exception_details => true,

  # Methods not included here will be rejected with 'method not implemented'
  # before any resource is called. (methods included here may still be rejected
  # as not supported by individual resources via supports_method).
  # Note: HEAD is supported as part of GET support, and OPTIONS comes for free.
  :recognized_methods => [:get, :post, :put, :delete],

  # You might need to change this depending on what rack middleware you use to
  # authenticate / identify users. Eg could use
  #   env['rack.session'] for use with Rack::Session (the default)
  #   env['REMOTE_USER'] for use with Rack::Auth::Basic / Digest, and direct via Apache and some other front-ends that do http auth
  #   env['rack.auth.openid'] for use with Rack::Auth::OpenID
  # This is used to look up a session or user object in the rack environment
  :session_from_rack_env => proc {|env| env['rack.session']},

  # Used to determine whether the user/session object obtained via :session_from_rack_env is to be treated as authenticated or not.
  # By default this looks for a key :user within a session object.
  # For eg env['REMOTE_USER'] the session is the authenticated username and you probably just want to test for !session.nil?
  :session_authenticated => proc {|session| !session[:user].nil?},

  # Doze has a special facility to use a file extension style suffix on the URI.
  # Instead of passing this file extension through the routing process, it is dropped from the routed URI
  # and handled specially, effectively overriding the Accept header for that request and forcing a response
  # with the media type in question.
  # Relies on media types being registered by extension, see Doze::MediaType.
  # NB: if you enable this setting, be aware that extensions are stripped prior to routing, so you will
  # lose the ability to route based on file extensions, and must be careful to escape the extension delimiter (".")
  # when putting a text fragment for matching at the end of a uri.
  :media_type_extensions => false
}

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(root, config = {}) ⇒ Application

root may be a Router, a Resource, or both. If a resource, its uri should return ‘/’



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/doze/application.rb', line 66

def initialize(root, config={})
  @config = DEFAULT_CONFIG.merge(config)
  @logger = @config[:logger] || STDOUT
  @root = root

  # This is done on application initialization to ensure that statically-known
  # information about routing paths is propagated as far as possible, so that
  # resource instances can know their uris without necessarily having been
  # a part of the routing chain for the current request.
  # TODO use SCRIPT_NAME from the rack env of the first request we get, rather than ''
  @root.propagate_static_routes('') if @root.respond_to?(:propagate_static_routes)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



62
63
64
# File 'lib/doze/application.rb', line 62

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



62
63
64
# File 'lib/doze/application.rb', line 62

def logger
  @logger
end

#rootObject (readonly)

Returns the value of attribute root.



62
63
64
# File 'lib/doze/application.rb', line 62

def root
  @root
end

Instance Method Details

#call(env) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/doze/application.rb', line 79

def call(env)
  begin
    request = Doze::Request.new(self, env)
    responder = Doze::Responder::Main.new(self, request)
    responder.call
  rescue => exception
    raise unless config[:catch_application_errors]
    lines = ['500 response via error resource failed', "#{exception.class}: #{exception.message}", *exception.backtrace]
    @logger << lines.join("\n")
    body = config[:expose_exception_details] ? lines : [lines.first]
    [STATUS_INTERNAL_SERVER_ERROR, {}, [body.join("\n")]]
  end
end