Class: Rack::JQuery
- Inherits:
-
Object
- Object
- Rack::JQuery
- Includes:
- Helpers
- Defined in:
- lib/rack/jquery.rb,
lib/rack/jquery/version.rb
Overview
jQuery CDN script tags and fallback in one neat package.
Constant Summary collapse
- JQUERY_FILE_NAME =
"jquery-#{JQUERY_VERSION}.min.js"- MEDIA_TEMPLE =
Script tags for the Media Temple CDN
"<script src='http://code.jquery.com/#{JQUERY_FILE_NAME}'></script>"- GOOGLE =
Script tags for the Google CDN
"<script src='//ajax.googleapis.com/ajax/libs/jquery/#{JQUERY_VERSION}/jquery.min.js'></script>"- MICROSOFT =
Script tags for the Microsoft CDN
"<script src='//ajax.aspnetcdn.com/ajax/jQuery/#{JQUERY_FILE_NAME}'></script>"- CLOUDFLARE =
Script tags for the Cloudflare CDN
"<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/#{JQUERY_VERSION}/jquery.min.js'></script>"- FALLBACK =
This javascript checks if the jQuery object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified jQuery.
<<STR <script type="text/javascript"> if (typeof jQuery == 'undefined') { document.write(unescape("%3Cscript src='/js/#{JQUERY_FILE_NAME}' type='text/javascript'%3E%3C/script%3E")) }; </script> STR
- DEFAULT_OPTIONS =
Default options hash for the middleware.
{ :http_path => "/js" }
- VERSION =
the version of this library
"1.4.0"- JQUERY_VERSION =
the version of jQuery it supports.
"2.0.0"- JQUERY_VERSION_DATE =
TODO:
remember to change Last-Modified with each release!
This is the release date of the jQuery file, it makes an easy “Last-Modified” date for setting the headers around caching.
"Thu, 18 Apr 2013 00:00:00 GMT"
Class Method Summary collapse
-
.cdn(organisation = :media_temple) ⇒ 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 = {}) ⇒ JQuery
constructor
A new instance of JQuery.
Constructor Details
#initialize(app, options = {}) ⇒ JQuery
Returns a new instance of JQuery.
66 67 68 69 |
# File 'lib/rack/jquery.rb', line 66 def initialize( app, ={} ) @app, @options = app, DEFAULT_OPTIONS.merge() @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_FILE_NAME end |
Class Method Details
.cdn(organisation = :media_temple) ⇒ String
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rack/jquery.rb', line 35 def self.cdn( organisation=:media_temple ) script = case organisation when :media_temple MEDIA_TEMPLE when :microsoft MICROSOFT when :cloudflare CLOUDFLARE when :google GOOGLE else MEDIA_TEMPLE end "#{script}\n#{FALLBACK}" end |
Instance Method Details
#_call(env) ⇒ Object
For thread safety
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rack/jquery.rb', line 80 def _call( env ) request = Rack::Request.new(env.dup) if request.path_info == @http_path_to_jquery response = Rack::Response.new # for caching response.headers.merge! caching_headers( JQUERY_FILE_NAME, JQUERY_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/javascripts/#{JQUERY_FILE_NAME}", __FILE__) end response.finish else @app.call(env) end end |
#call(env) ⇒ Object
73 74 75 |
# File 'lib/rack/jquery.rb', line 73 def call( env ) dup._call env end |