Module: Pancake::Mixins::RequestHelper
- Defined in:
- lib/pancake/mixins/request_helper.rb
Overview
Some helpers for requests that come in handy for applications that are part of stacks
Constant Summary collapse
- VARS_KEY =
'request.variables'
Instance Method Summary collapse
-
#base_url(opts = {}) ⇒ Object
Generate the base url for the router that got you to this point.
-
#configuration ⇒ Object
Get the configuration for this request.
- #content_type ⇒ Object
- #content_type=(format) ⇒ Object
-
#env ⇒ Object
An accessor for the rack environment variable.
-
#env=(env) ⇒ Object
A setter for the rack environment.
-
#logger ⇒ Object
Provides access to the logger object in rack.logger.
- #mime_type ⇒ Object
- #mime_type=(mime) ⇒ Object
- #negotiate_content_type!(*allowed_types) ⇒ Object
-
#request ⇒ Object
A handy request method that gets hold of the current request object for the current rack request.
-
#url(name, opts = {}) ⇒ Object
Generate a url for the current stacks router.
-
#url_for(app_name, name_or_opts, opts = {}) ⇒ Object
Generate a url for any registered configuration with a router.
-
#vars ⇒ Object
(also: #v)
A data area that allows you to carry data accross middlewares, controller / views etc.
Instance Method Details
#base_url(opts = {}) ⇒ Object
Generate the base url for the router that got you to this point.
end
@see Usher
@see Pancake::Router.base_url_for
@see Pancake::Router#base_url
@api public
@author Daniel Neighman
70 71 72 |
# File 'lib/pancake/mixins/request_helper.rb', line 70 def base_url(opts={}) configuration.router.base_url(opts) end |
#configuration ⇒ Object
Get the configuration for this request. This will be updated as the request makes its way through the stacks
22 23 24 |
# File 'lib/pancake/mixins/request_helper.rb', line 22 def configuration request.env[Pancake::Router::CONFIGURATION_KEY] end |
#content_type ⇒ Object
150 151 152 |
# File 'lib/pancake/mixins/request_helper.rb', line 150 def content_type env['pancake.request.format'] end |
#content_type=(format) ⇒ Object
154 155 156 |
# File 'lib/pancake/mixins/request_helper.rb', line 154 def content_type=(format) env['pancake.request.format'] = format end |
#env ⇒ Object
An accessor for the rack environment variable
104 105 106 |
# File 'lib/pancake/mixins/request_helper.rb', line 104 def env @env ||= {} end |
#env=(env) ⇒ Object
A setter for the rack environment
98 99 100 |
# File 'lib/pancake/mixins/request_helper.rb', line 98 def env=(env) @env = env end |
#logger ⇒ Object
Provides access to the logger object in rack.logger
125 126 127 |
# File 'lib/pancake/mixins/request_helper.rb', line 125 def logger env[Pancake::Constants::ENV_LOGGER_KEY] end |
#mime_type ⇒ Object
158 159 160 |
# File 'lib/pancake/mixins/request_helper.rb', line 158 def mime_type env['pancake.request.mime'] end |
#mime_type=(mime) ⇒ Object
162 163 164 |
# File 'lib/pancake/mixins/request_helper.rb', line 162 def mime_type=(mime) env['pancake.request.mime'] = mime end |
#negotiate_content_type!(*allowed_types) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/pancake/mixins/request_helper.rb', line 129 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 |
#request ⇒ Object
A handy request method that gets hold of the current request object for the current rack request. Any including class must provide an env
method that exposes the rack request environment
116 117 118 |
# File 'lib/pancake/mixins/request_helper.rb', line 116 def request @request ||= Rack::Request.new(env) end |
#url(name, opts = {}) ⇒ Object
Generate a url for the current stacks router.
46 47 48 |
# File 'lib/pancake/mixins/request_helper.rb', line 46 def url(name, opts = {}) configuration.router.url(name, opts) end |
#url_for(app_name, name_or_opts, opts = {}) ⇒ Object
Generate a url for any registered configuration with a router
:some_app)
url_for(:some_app, :my_named_route)
# An application with no name specified
url_for(MyApp, :my_named_route)
88 89 90 91 92 93 94 |
# File 'lib/pancake/mixins/request_helper.rb', line 88 def url_for(app_name, name_or_opts, opts = {}) if konfig = Pancake.configuration.configs[app_name] konfig.router.generate(name_or_opts, opts) else raise Pancake::Errors::UnknownConfiguration end end |
#vars ⇒ Object Also known as: v
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.
15 16 17 18 |
# File 'lib/pancake/mixins/request_helper.rb', line 15 def vars env[VARS_KEY] ||= Hashie::Mash.new env[VARS_KEY] end |