Module: PrestoHTTP::Config
- Includes:
- PrestoCore::Utils
- Included in:
- ClassAPI, Slice
- Defined in:
- lib/presto/http/config.rb
Overview
configs here are set by both controller and slice
methods here used to setup the HTTP API. to have an consistent setup, it should be write-able only at class definition. any later updates should be prohibited. to accomplish this, any updates for mounted controller are silently dropped.
Constant Summary
Constant Summary
Constants included from PrestoCore::Utils
PrestoCore::Utils::PATH_MODIFIERS, PrestoCore::Utils::STATUS__NOT_FOUND, PrestoCore::Utils::STATUS__OK, PrestoCore::Utils::STATUS__PERMANENT_REDIRECT, PrestoCore::Utils::STATUS__REDIRECT, PrestoCore::Utils::STATUS__RESTRICTED, PrestoCore::Utils::STATUS__SERVER_ERROR
Instance Attribute Summary (collapse)
-
- (Object) cache_pool(pool = nil)
by default, Presto will use an in memory cache pool.
-
- (String) content_type(*actions, &proc)
(also: #provide)
content type to be returned by action(s).
-
- (Object) middleware
each controller may have own middleware.
-
- (Object) path_rules(rules = nil)
allow app to define its own rewriting rules for method names.
-
- (Object) rewrite_rules
readonly
Returns the value of attribute rewrite_rules.
Instance Method Summary (collapse)
-
- (Object) after(*actions, &proc)
hooks to be executed before/after given action(s) or before/after any action if no actions given.
-
- (Object) auth(*actions_and_or_opts, &proc)
making some actions, or all, to require authorization.
- - (Object) basic_auth(*args, &proc)
-
- (Object) before(*actions, &proc)
hooks to be executed before/after given action(s) or before/after any action if no actions given.
-
- (Object) cache(*actions, &proc)
setting some actions(or all) to use cache.
- - (Object) digest_auth(*args, &proc)
- - (Object) encoding(*actions, &proc) (also: #charset)
-
- (Object) error(code, &proc)
define callbacks to be executed on HTTP errors.
- - (Object) html_auth(*args, &proc)
-
- (Config) initialize(*args)
A new instance of Config.
- - (Object) rewrite(rule, &proc)
-
- (Object) use(ware, *args, &proc)
each controller may have own middleware.
Methods included from PrestoCore::Utils
build_path, #extract_controllers, is_controller?, normalize_path, rootify_url
Instance Attribute Details
- (Object) cache_pool(pool = nil)
by default, Presto will use an in memory cache pool. it is well and fast as long as your content fit into available RAM. to keep memory low, consider to use some fast key/value db.
345 346 347 348 349 350 |
# File 'lib/presto/http/config.rb', line 345 def cache_pool pool = nil @cache_pool = pool if pool && configurable? @setup[:cache_pool] ||= @cache_pool || (@controller.ctrl.slice.http.cache_pool if @controller) || ::Presto.setup.http.cache_pool end |
- (String) content_type(*actions, &proc) Also known as: provide
content type to be returned by action(s). default is text/html
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/presto/http/config.rb', line 84 def content_type *actions, &proc if proc && configurable? actions = [:*] if actions.size == 0 actions.each { |a| @content_type[a] = proc } end action = actions.first @setup['content_type::%s' % action] ||= @content_type[action] || @content_type[:*] || (@controller.ctrl.slice.http.content_type(action) if @controller) end |
- (Object) middleware
each controller may have own middleware.
360 361 362 |
# File 'lib/presto/http/config.rb', line 360 def middleware @middleware end |
- (Object) path_rules(rules = nil)
# default rules
- "__" (2 underscores) => "-" (dash)
- "___" (3 underscores) => "/" (slash)
- "____" (4 underscores) => "." (period)
allow app to define its own rewriting rules for method names. each method are translated into its path representation.
return [Hash, nil]
135 136 137 138 139 140 |
# File 'lib/presto/http/config.rb', line 135 def path_rules rules = nil @path_rules = rules.freeze if rules && configurable? @setup[:path_rules] ||= @path_rules || (@controller.ctrl.slice.http.path_rules if @controller) || ::Presto.setup.http.path_rules end |
- (Object) rewrite_rules (readonly)
Returns the value of attribute rewrite_rules
15 16 17 |
# File 'lib/presto/http/config.rb', line 15 def rewrite_rules @rewrite_rules end |
Instance Method Details
- (Object) after(*actions, &proc)
hooks to be executed before/after given action(s) or before/after any action if no actions given.
46 47 48 49 50 51 52 |
# File 'lib/presto/http/config.rb', line 46 def after *actions, &proc if proc && configurable? actions = [:*] if actions.size == 0 actions.each { |a| @hooks_z[a] = proc } if proc end @hooks_z end |
- (Object) auth(*actions_and_or_opts, &proc)
making some actions, or all, to require authorization.
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/presto/http/config.rb', line 256 def auth *actions_and_or_opts, &proc if proc && configurable? # prohibit updates after controller mounted actions, opts = [], {} actions_and_or_opts.each { |arg| arg.is_a?(Hash) ? opts.update(arg) : actions << arg } if opted_type = opts[:type] AUTHORIZATION_TYPES.include?(opted_type.to_s) || raise('%s is an unknown auth type, please use one of :%s' % [opted_type.inspect, AUTHORIZATION_TYPES.join(', :')]) end opts[:proc] = proc actions = [:*] if actions.size == 0 actions.each { |a| @restrictions[a] = opts.merge(action: a) } end action = actions_and_or_opts.first @setup['%s::%s' % [:auth, action]] ||= @restrictions[action] || @restrictions[:*] || (@controller.ctrl.slice.http.auth(action) if @controller) end |
- (Object) basic_auth(*args, &proc)
278 279 280 |
# File 'lib/presto/http/config.rb', line 278 def basic_auth *args, &proc auth *args, type: :Basic, &proc end |
- (Object) before(*actions, &proc)
hooks to be executed before/after given action(s) or before/after any action if no actions given.
37 38 39 40 41 42 43 |
# File 'lib/presto/http/config.rb', line 37 def before *actions, &proc if proc && configurable? actions = [:*] if actions.size == 0 actions.each { |a| @hooks_a[a] = proc } if proc end @hooks_a end |
- (Object) cache(*actions, &proc)
if no actions given, all actions will use same setup.
block is required, cause it is a bad idea to leave cache out of control.
http hooks WILL BE EVER executed regardless cache.
setting some actions(or all) to use cache.
given block will be used to decide which requests will use cache and which will not.
-
if block returns any positive value, cache will be returned
-
if block returns :update [Symbol], cache will be updated then returned
otherwise a fresh version of action will be returned.
323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/presto/http/config.rb', line 323 def cache *actions, &proc if proc && configurable? # prohibit updates after controller mounted actions = [:*] if actions.size == 0 actions.each { |a| @cache[a] = proc } end action = actions.first @setup['%s::%s' % [:cache, action]] ||= @cache[action] || @cache[:*] || (@controller.ctrl.slice.http.cache(action) if @controller) end |
- (Object) digest_auth(*args, &proc)
282 283 284 |
# File 'lib/presto/http/config.rb', line 282 def digest_auth *args, &proc auth *args, type: :Digest, &proc end |
- (Object) encoding(*actions, &proc) Also known as: charset
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/presto/http/config.rb', line 99 def encoding *actions, &proc if proc && configurable? actions = [:*] if actions.size == 0 actions.each { |a| @encoding[a] = proc } end action = actions.first @setup['encoding::%s' % action] ||= @encoding[action] || @encoding[:*] || (@controller.ctrl.slice.http.encoding(action) if @controller) end |
- (Object) error(code, &proc)
define callbacks to be executed on HTTP errors.
64 65 66 67 68 69 70 |
# File 'lib/presto/http/config.rb', line 64 def error code, &proc if proc && configurable? # prohibit updates after controller mounted @error_procs[code] = proc end @setup['%s::%s' % [:error_procs, code]] ||= @error_procs[code] || (@controller.ctrl.slice.http.error(code) if @controller) end |
- (Object) html_auth(*args, &proc)
286 287 288 |
# File 'lib/presto/http/config.rb', line 286 def html_auth *args, &proc auth *args, type: :HTML, &proc end |
- (Config) initialize(*args)
A new instance of Config
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/presto/http/config.rb', line 17 def initialize *args @middleware = [] @rewrite_rules = {} @cache = {} @restrictions = {} @content_type, @encoding = {}, {} @error_procs = {} @hooks_a, @hooks_z = {}, {} @setup = {} end |
- (Object) rewrite(rule, &proc)
174 175 176 177 178 |
# File 'lib/presto/http/config.rb', line 174 def rewrite rule, &proc if proc && configurable? (@rewrite_rules[@slice && @slice.root] ||= []) << [rule, proc] end end |
- (Object) use(ware, *args, &proc)
each controller may have own middleware.
360 361 362 |
# File 'lib/presto/http/config.rb', line 360 def use ware, *args, &proc @middleware << {ware: ware, args: args, block: proc} if configurable? end |