Class: Rack::Lodash

Inherits:
Object
  • Object
show all
Includes:
JQuery::Helpers
Defined in:
lib/rack/lodash.rb,
lib/rack/lodash/version.rb

Defined Under Namespace

Modules: CDN

Constant Summary collapse

LODASH_FILE_NAME =

The file name to use for the fallback route.

"lodash-#{LODASH_VERSION}.min.js"
FALLBACK =

This javascript checks if the Lodash object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified Lodash.

<<STR
<script type="text/javascript">
  if (typeof _ == 'undefined') {
    document.write(unescape("%3Cscript src='/js/#{LODASH_FILE_NAME}' type='text/javascript'%3E%3C/script%3E"))
  };
</script>
STR
DEFAULT_ORGANISATION =

The default CDN to use.

:cloudflare
DEFAULT_OPTIONS =

Default options hash for the middleware.

{
  :http_path => "/js",
  :organisation => DEFAULT_ORGANISATION
}
VERSION =

The version of this library.

"1.0.2"
LODASH_VERSION =

The version of Lo-dash it supports

"1.3.1"
LODASH_VERSION_DATE =
TODO:

remember to change Last-Modified with each release!

This is the release date of the Lo-dash file, it makes an easy “Last-Modified” date for setting the headers around caching.

"Wed, 12 Jun 2013 15:54:39 +0100"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Lodash

Returns a new instance of Lodash.

Examples:

# The default:
use Rack::Lodash

# With a different route to the fallback:
use Rack::Lodash, :http_path => "/assets/js"

# With the CDN specified via the use statement
use Rack::Lodash, :organisation => :jsdelivr

Parameters:

  • app (#call)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :http_path (String)

    If you wish the Lodash fallback route to be “/js/lodash-1.3.1.min.js” (or whichever version this is at) then do nothing, that’s the default. If you want the path to be “/assets/javascripts/lodash-1.3.1.min.js” then pass in ‘:http_path => “/assets/javascripts”.

  • :organisation (Symbol)

    Choose which CDN to use, either :cloudflare or :jsdelivr. The default is :cloudflare.



75
76
77
78
79
# File 'lib/rack/lodash.rb', line 75

def initialize( app, options={} )
  @app, @options  = app, DEFAULT_OPTIONS.merge(options)
  @http_path_to_lodash = ::File.join @options[:http_path], LODASH_FILE_NAME
  @organisation = @options[:organisation]
end

Class Method Details

.cdn(env, opts = {}) ⇒ String

Returns The HTML script tags to get the CDN.

Parameters:

  • env (Hash)

    The rack env.

  • opts (Hash) (defaults to: {})

    Extra options.

Returns:

  • (String)

    The HTML script tags to get the CDN.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/lodash.rb', line 37

def self.cdn( env, opts={} )
  organisation =  opts[:organisation] ||
                    (env["rack.lodash"] && env["rack.lodash"]["organisation"]) ||
                    Rack::Lodash::DEFAULT_ORGANISATION

  script = case organisation
    when :cloudflare then CDN::CLOUDFLARE
    when :jsdelivr then CDN::JSDELIVR
    else CDN::CLOUDFLARE
  end
  "<script src='#{script}'></script>\n#{FALLBACK}"
end

Instance Method Details

#_call(env) ⇒ Object

For thread safety

Parameters:

  • env (Hash)

    Rack request environment hash.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rack/lodash.rb', line 90

def _call( env )
  env.merge! "rack.lodash" => {"organisation" => @organisation}

  request = Rack::Request.new(env.dup)
  if request.path_info == @http_path_to_lodash
    response = Rack::Response.new
    # for caching
    response.headers.merge! caching_headers( LODASH_FILE_NAME, LODASH_VERSION_DATE)

    # There's no need to test if the IF_MODIFIED_SINCE against the release date because the header will only be passed if the file was previously accessed by the requester, and the file is never updated. If it is updated then it is accessed by a different path.
    if request.env['HTTP_IF_MODIFIED_SINCE']
      response.status = 304
    else
      response.status = 200
      response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascript/libs/lodash/#{LODASH_VERSION}/lodash.min.js", __FILE__)
    end
    response.finish
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object

Parameters:

  • env (Hash)

    Rack request environment hash.



83
84
85
# File 'lib/rack/lodash.rb', line 83

def call( env )
  dup._call env
end