Class: Jsus::Middleware
Overview
Jsus rack middleware.
Usage
use Jsus::Middleware
in your rack application and all the requests
to /javascripts/jsus/* will be redirected to the middleware.
Example requests
GET /javascripts/jsus/require/Mootools.Core+Mootools.More
GET /javascripts/jsus/require/Mootools.More~Mootools.Core
GET /javascripts/jsus/require/Mootools.Core:Class+Mootools.More:Fx
GET /javascripts/jsus/include/Mootools.Core
Constant Summary collapse
- DEFAULT_SETTINGS =
Default settings for Middleware
{ :packages_dir => ".", :cache => false, :cache_path => nil, :prefix => "jsus", :cache_pool => true, :includes_root => ".", :compression_method => :yui, # :yui, :frontcompiler, :uglifier, :closure :log_method => nil, # [:alert, :html, :console] :postproc => [] # ["mooltie8", "moocompat12"] }.freeze
Class Method Summary collapse
-
.cache ⇒ Jsus::Util::FileCache
File cache to store results of requests.
-
.cache? ⇒ Boolean
Whether caching is enabled.
-
.pool ⇒ Jsus::Pool
Generates and caches a pool of source files and packages.
-
.settings ⇒ Hash
Middleware current settings.
-
.settings=(new_settings) ⇒ Object
Merges given new settings into current settings.
Instance Method Summary collapse
-
#_call(env) ⇒ #each
Since rack apps are used as singletons and we store some state during request handling, we need to separate state between different calls.
-
#call(env) ⇒ #each
Rack calling point.
-
#initialize(app) ⇒ Middleware
constructor
Default rack initialization routine.
Constructor Details
#initialize(app) ⇒ Middleware
Default rack initialization routine
102 103 104 |
# File 'lib/jsus/middleware.rb', line 102 def initialize(app) @app = app end |
Class Method Details
.cache ⇒ Jsus::Util::FileCache
Returns file cache to store results of requests.
94 95 96 |
# File 'lib/jsus/middleware.rb', line 94 def cache @@cache ||= cache? ? Util::FileCache.new(settings[:cache_path]) : nil end |
.cache? ⇒ Boolean
Returns whether caching is enabled.
88 89 90 |
# File 'lib/jsus/middleware.rb', line 88 def cache? settings[:cache] end |
.pool ⇒ Jsus::Pool
Generates and caches a pool of source files and packages.
82 83 84 |
# File 'lib/jsus/middleware.rb', line 82 def pool @@pool ||= Jsus::Pool.new(settings[:packages_dir]) end |
.settings ⇒ Hash
Returns Middleware current settings.
52 53 54 |
# File 'lib/jsus/middleware.rb', line 52 def settings @@settings ||= DEFAULT_SETTINGS.dup end |
.settings=(new_settings) ⇒ Object
Merges given new settings into current settings.
74 75 76 |
# File 'lib/jsus/middleware.rb', line 74 def settings=(new_settings) settings.merge!(new_settings) end |
Instance Method Details
#_call(env) ⇒ #each
Since rack apps are used as singletons and we store some state during request handling, we need to separate state between different calls.
Jsus::Middleware#call method dups current rack app and executes Jsus::Middleware#_call on it.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/jsus/middleware.rb', line 115 def _call(env) Jsus.logger.buffer.clear path = Utils.unescape(env["PATH_INFO"]) return @app.call(env) unless handled_by_jsus?(path) path.sub!(path_prefix_regex, "") components = path.split("/") return @app.call(env) unless components.size >= 2 [:path] = path if components[0] == "require" generate_requires(components[1]) elsif components[0] == "compressed" [:compress] = true generate_requires(components[1]) elsif components[0] == "include" generate_includes(components[1]) else not_found! end end |
#call(env) ⇒ #each
Rack calling point
141 142 143 |
# File 'lib/jsus/middleware.rb', line 141 def call(env) dup._call(env) end |