Class: Rack::JQuery

Inherits:
Object
  • Object
show all
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 =
"jquery-#{JQUERY_VERSION}.min.js"
JQUERY_SOURCE_MAP =
"jquery-#{JQUERY_VERSION}.min.map"
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.

"<script type=\"text/javascript\">\n  if (typeof jQuery == 'undefined') {\n    document.write(unescape(\"%3Cscript src='/js/\#{JQUERY_FILE_NAME}' type='text/javascript'%3E%3C/script%3E\"))\n  };\n</script>\n"
DEFAULT_OPTIONS =

Default options hash for the middleware.

{
  :http_path => "/js"
}
VERSION =

the version of this library

"1.5.1"
JQUERY_VERSION =

the version of jQuery it supports.

"2.0.3"
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 +0100"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of JQuery.

Examples:

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

Parameters:

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

Options Hash (options):

  • :http_path (String)

    If you wish the jQuery fallback route to be “/js/jquery-1.9.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/jquery-1.9.1.min.js” then pass in ‘:http_path => “/assets/javascripts”.



73
74
75
76
77
# File 'lib/rack/jquery.rb', line 73

def initialize( app, options={} )
  @app, @options  = app, DEFAULT_OPTIONS.merge(options)
  @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_FILE_NAME
  @http_path_to_source_map = ::File.join @options[:http_path], JQUERY_SOURCE_MAP
end

Class Method Details

.cdn(organisation = :media_temple) ⇒ String

Returns The HTML script tags to get the CDN.

Parameters:

  • organisation (Symbol) (defaults to: :media_temple)

    Choose which CDN to use, either :google, :microsoft or :media_temple, or :cloudflare

Returns:

  • (String)

    The HTML script tags to get the CDN.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rack/jquery.rb', line 42

def self.cdn( organisation=:media_temple  )
  script = case organisation
    when :media_temple
      CDN::MEDIA_TEMPLE
    when :microsoft
      CDN::MICROSOFT
    when :cloudflare
      CDN::CLOUDFLARE
    when :google
      CDN::GOOGLE
    else
      CDN::MEDIA_TEMPLE
  end
  "<script src='#{script}'></script>\n#{FALLBACK}"
end

Instance Method Details

#_call(env) ⇒ Object

For thread safety

Parameters:

  • env (Hash)

    Rack request environment hash.



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
# File 'lib/rack/jquery.rb', line 88

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.expand_path "../../../vendor/assets/javascripts/#{JQUERY_FILE_NAME}", __FILE__)
    end
    response.finish
  elsif request.path_info == @http_path_to_source_map
    response = Rack::Response.new
    # No need for caching with the source map
    response.status = 200
    response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascripts/#{JQUERY_SOURCE_MAP}", __FILE__)
    response.finish
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object

Parameters:

  • env (Hash)

    Rack request environment hash.



81
82
83
# File 'lib/rack/jquery.rb', line 81

def call( env )
  dup._call env
end