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.
Defined Under Namespace
Modules: CDN
Constant Summary collapse
- JQUERY_FILE_NAME =
Current file name of fallback.
"jquery-#{JQUERY_VERSION}.min.js"
- FALLBACK_PATH =
path to the fallback script
"/js/#{JQUERY_FILE_NAME}"
- 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='#{FALLBACK_PATH}' type='text/javascript'%3E%3C/script%3E")) }; </script> STR
- WARNING =
For CDN’s that don’t support the current release.
"CDN does not hold #{JQUERY_VERSION} at the time of this gem's release. Please use Rack::JQuery's Rake file by running rake cdn:check to confirm this, or choose another CDN."
- DEFAULT_OPTIONS =
Default options hash for the middleware.
{ :http_path => "/js", :raise => false }
- VERSION =
the version of this library
"2.2.1"
- JQUERY_VERSION =
the version of jQuery it supports.
"2.1.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, 23 Jan 2014 21:11:05 +0000"
Class Method Summary collapse
-
.cdn(env, options = {}) ⇒ String
The HTML script tags to get the CDN.
-
.raiser?(env, options) ⇒ Boolean
Handles the logic for whether to raise or not.
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.
142 143 144 145 146 147 |
# File 'lib/rack/jquery.rb', line 142 def initialize( app, ={} ) @app, @options = app, DEFAULT_OPTIONS.merge() @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_FILE_NAME @raise = @options.fetch :raise, false @organisation = .fetch :organisation, :media_temple end |
Class Method Details
.cdn(env, options = {}) ⇒ String
Returns The HTML script tags to get the CDN.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rack/jquery.rb', line 76 def self.cdn( env, ={} ) if env.nil? || env.has_key?(:organisation) fail ArgumentError, "The Rack::JQuery.cdn method needs the Rack environment passed to it, or at the very least, an empty hash." end organisation = [:organisation] if organisation.nil? # because false is valid organisation = env["rack.jquery.organisation"].nil? ? :media_temple : env["rack.jquery.organisation"] end raise = raiser?( env, ) unless organisation == false script_src = case organisation when :media_temple CDN::MEDIA_TEMPLE when :microsoft CDN::MICROSOFT when :cloudflare CDN::CLOUDFLARE when :google # meth = raise ? :fail : :warn # send meth, "#{organisation.to_s.gsub('_', ' ').capitalize}'s #{WARNING}" CDN::GOOGLE else CDN::MEDIA_TEMPLE end debug = .fetch :debug, false script_src = "#{script_src[0..-7]}js" if debug "<script src='#{script_src}'></script>\n#{FALLBACK}" else "<script src='#{FALLBACK_PATH}'></script>" end end |
.raiser?(env, options) ⇒ Boolean
Used by the library, not for public use.
Handles the logic for whether to raise or not
49 50 51 52 53 |
# File 'lib/rack/jquery.rb', line 49 def self.raiser?( env, ) (opt = [:raise]).nil? ? (env["rack.jquery.raise"] || false) : opt end |
Instance Method Details
#_call(env) ⇒ Object
For thread safety
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rack/jquery.rb', line 158 def _call( env ) request = Rack::Request.new(env.dup) env.merge! "rack.jquery.organisation" => @organisation env.merge! "rack.jquery.raise" => @raise 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
151 152 153 |
# File 'lib/rack/jquery.rb', line 151 def call( env ) dup._call env end |