Class: Jsus::Middleware

Inherits:
Object
  • Object
show all
Includes:
Rack
Defined in:
lib/jsus/middleware.rb

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

merges packages named Mootools.Core and Mootools.More with all the dependencies and outputs the result.

GET /javascripts/jsus/require/Mootools.More~Mootools.Core

returns package Mootools.More with all the dependencies MINUS any of Mootools.Core dependencies.

GET /javascripts/jsus/require/Mootools.Core:Class+Mootools.More:Fx

same thing but for source files providing Mootools.Core/Class and Mootools.More/Fx

GET /javascripts/jsus/include/Mootools.Core

generates js file with remote javascript fetching via ajax

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Default rack initialization routine

Parameters:

  • app (#call)

    rack chain caller



102
103
104
# File 'lib/jsus/middleware.rb', line 102

def initialize(app)
  @app = app
end

Class Method Details

.cacheJsus::Util::FileCache

Returns file cache to store results of requests.

Returns:



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.

Returns:

  • (Boolean)

    whether caching is enabled



88
89
90
# File 'lib/jsus/middleware.rb', line 88

def cache?
  settings[:cache]
end

.poolJsus::Pool

Generates and caches a pool of source files and packages.

Returns:



82
83
84
# File 'lib/jsus/middleware.rb', line 82

def pool
  @@pool ||= Jsus::Pool.new(settings[:packages_dir])
end

.settingsHash

Returns Middleware current settings.

Returns:

  • (Hash)

    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.

Parameters:

  • new_settings (Hash)

Options Hash (new_settings):

  • :packages_dir (String, Array)

    directory (or array of directories) containing source files.

  • :cache (Boolean)

    enable file caching (every request is written into corresponding file). Note, that it's write-only cache, you will have to configure your webserver to serve these files.

  • :cache_path (String)

    path to cache directory

  • :prefix (String, nil)

    path prefix to use for request. You can change default "jsus" to anything else or disable it altogether.

  • :cache_pool (Boolean)

    whether to cache pool between requests. Cached pool means that updates to your source files will not be visible until you restart webserver.

  • :includes_root (String)

    when generating "includes" lists, this is the point in filesystem used as relative root.



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.

Parameters:

  • env (Hash)

    rack env

Returns:

  • (#each)

    rack response



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

  request_options[:path] = path
  if components[0] == "require"
    generate_requires(components[1])
  elsif components[0] == "compressed"
    request_options[: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

Parameters:

  • env (Hash)

    rack env

Returns:

  • (#each)

    rack response



141
142
143
# File 'lib/jsus/middleware.rb', line 141

def call(env)
  dup._call(env)
end