Class: Rack::Backbone
- Inherits:
-
Object
- Object
- Rack::Backbone
- Includes:
- JQuery::Helpers
- Defined in:
- lib/rack/backbone.rb,
lib/rack/backbone/version.rb
Overview
Backbone CDN script tags and fallback in one neat package.
Defined Under Namespace
Modules: CDN
Constant Summary collapse
- BACKBONE_FILE_NAME =
Current file name of fallback.
"backbone-min.js"
- BACKBONE_SOURCE_MAP =
Fallback source map file name without version. Because the main script doesn’t call a versioned file.
"backbone-min.map"
- DEFAULT_OPTIONS =
Default options hash for the middleware.
{ :http_path => "/js" }
- FALLBACK_TOP =
This javascript checks if the Backbone object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified Backbone. It’s the top half, it gets pieced together elsewhere.
<<STR <script type="text/javascript"> if (typeof Backbone == 'undefined') { document.write(unescape("%3Cscript src=' STR
- FALLBACK_BOTTOM =
Bottom half of the fallback script.
<<STR ' type='text/javascript'%3E%3C/script%3E")) }; </script> STR
- VERSION =
the version of this library
"1.0.0"
- BACKBONE_VERSION =
the version of Backbone it supports.
"1.1.0"
- BACKBONE_VERSION_DATE =
TODO:
remember to change Last-Modified with each release!
This is the release date of the Backbone file, it makes an easy “Last-Modified” date for setting the headers around caching.
"Thu, 10 Oct 2013 17:57:30 -0700"
Class Method Summary collapse
-
.cdn(env, options = {}) ⇒ 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 = {}) ⇒ Backbone
constructor
A new instance of Backbone.
Constructor Details
#initialize(app, options = {}) ⇒ Backbone
Returns a new instance of Backbone.
102 103 104 105 106 107 |
# File 'lib/rack/backbone.rb', line 102 def initialize( app, ={} ) @app, @options = app, DEFAULT_OPTIONS.merge() @http_path_to_backbone = ::File.join @options[:http_path], BACKBONE_FILE_NAME @http_path_to_source_map = ::File.join @options[:http_path], BACKBONE_SOURCE_MAP @organisation = .fetch :organisation, :media_temple end |
Class Method Details
.cdn(env, options = {}) ⇒ String
Returns The HTML script tags to get the CDN.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rack/backbone.rb', line 57 def self.cdn( env, ={} ) if env.nil? || env.has_key?(:organisation) fail ArgumentError, "The Rack::Backbone.cdn method needs the Rack environment passed to it, or at the very least, an empty hash." end debug = .fetch :debug, false organisation = [:organisation] if organisation.nil? organisation = env["rack.backbone.organisation"] || :media_temple end unless organisation == false script_src = case organisation when :cloudflare CDN::CLOUDFLARE when :jsdelivr CDN::JSDELIVR else CDN::CLOUDFLARE end script_src = "#{script_src[0..-8]}.js" if debug %Q!<script src='#{script_src}'></script>\n#{FALLBACK_TOP.chomp}#{env["rack.backbone.http_path"]}#{FALLBACK_BOTTOM}! else "<script src='#{env["rack.backbone.http_path"]}'></script>" end end |
Instance Method Details
#_call(env) ⇒ Object
For thread safety
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rack/backbone.rb', line 118 def _call( env ) request = Rack::Request.new(env.dup) env.merge! "rack.backbone.organisation" => @organisation env.merge! "rack.backbone.http_path" => @http_path_to_backbone if request.path_info == @http_path_to_backbone response = Rack::Response.new # for caching response.headers.merge! caching_headers( BACKBONE_FILE_NAME, BACKBONE_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/libs/backbone/#{BACKBONE_VERSION}/#{BACKBONE_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. "../../../vendor/assets/javascripts/libs/backbone/#{BACKBONE_VERSION}/#{BACKBONE_SOURCE_MAP}", __FILE__) response.finish else @app.call(env) end end |
#call(env) ⇒ Object
111 112 113 |
# File 'lib/rack/backbone.rb', line 111 def call( env ) dup._call env end |