Class: Rack::Lodash
- Inherits:
-
Object
- Object
- Rack::Lodash
- 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
-
.cdn(env, opts = {}) ⇒ String
The HTML script tags to get the CDN.
Instance Method Summary collapse
-
#_call(env) ⇒ Object
For thread safety.
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ Lodash
constructor
A new instance of Lodash.
Constructor Details
#initialize(app, options = {}) ⇒ Lodash
Returns a new instance of Lodash.
75 76 77 78 79 |
# File 'lib/rack/lodash.rb', line 75 def initialize( app, ={} ) @app, @options = app, DEFAULT_OPTIONS.merge() @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.
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
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. "../../../vendor/assets/javascript/libs/lodash/#{LODASH_VERSION}/lodash.min.js", __FILE__) end response.finish else @app.call(env) end end |
#call(env) ⇒ Object
83 84 85 |
# File 'lib/rack/lodash.rb', line 83 def call( env ) dup._call env end |