lib/pancake/mixins/request_helper.rb in pancake-0.1.29 vs lib/pancake/mixins/request_helper.rb in pancake-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ module Pancake module Mixins # Some helpers for requests that come in handy for applications that # are part of stacks module RequestHelper - VARS_KEY = 'pancake.request.vars' + VARS_KEY = 'request.variables' # A data area that allows you to carry data accross middlewares, controller / views etc. # Stores the data in session for the length of the request. # # @example @@ -122,9 +122,46 @@ # # @api public # @author Daniel Neighman def logger env[Pancake::Constants::ENV_LOGGER_KEY] + end + + def negotiate_content_type!(*allowed_types) + return content_type if content_type + + allowed_types = allowed_types.flatten + opts = allowed_types.pop if allowed_types.last.kind_of?(Hash) + opts ||= {} + if opts[:format] + cont, ct, mt = Pancake::MimeTypes.negotiate_by_extension(opts[:format].to_s, allowed_types) + else + env["HTTP_ACCEPT"] ||= "*/*" + cont, ct, mt = Pancake::MimeTypes.negotiate_accept_type(env["HTTP_ACCEPT"], allowed_types) + end + + raise Errors::NotAcceptable unless cont + + headers["Content-Type"] = ct + self.mime_type = mt + self.content_type = cont + cont + end + + def content_type + env['pancake.request.format'] + end + + def content_type=(format) + env['pancake.request.format'] = format + end + + def mime_type + env['pancake.request.mime'] + end + + def mime_type=(mime) + env['pancake.request.mime'] = mime end end # RequestHelper end # Mixins end # Pancake