class Rack::Backbone
Constants
- BACKBONE_FILE_NAME
Current file name of fallback.
- BACKBONE_SOURCE_MAP
Fallback source map file name without version. Because the main script doesn’t call a versioned file.
- BACKBONE_VERSION
the version of
Backbone
it supports.- BACKBONE_VERSION_DATE
This is the release date of the
Backbone
file, it makes an easy “Last-Modified” date for setting the headers around caching. @todo remember to change Last-Modified with each release!- DEFAULT_OPTIONS
Default options hash for the middleware.
- FALLBACK_BOTTOM
Bottom half of the fallback script.
- FALLBACK_TOP
This javascript checks if the
Backbone
object has loaded. If not, that most likely means theCDN
is unreachable, so it uses the local minifiedBackbone
. It’s the top half, it gets pieced together elsewhere.- VERSION
the version of this library
Public Class Methods
@param [Hash] env The rack env hash. @option options [Symbol] organisation Choose which CDN
to use, either :jsdelivr, or :cloudflare (the default). This will override anything set via the ‘use` statement. Pass in `false` to force use of the local Backbonejs script. `nil` will force choosing the default CDN
. @option options [TrueClass] :debug Pass `true` to get the unminified version of the script from the CDN
. @return [String] The HTML script tags to get the CDN
.
# File lib/rack/backbone.rb, line 57 def self.cdn( env, options={} ) 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 = options.fetch :debug, false organisation = options[: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
@param [#call] app @param [Hash] options @option options [String] :http_path If you wish the Backbone
fallback route to be “/js/backbone-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/backbone-1.9.1.min.js” then pass in ‘:http_path => “/assets/javascripts”. @option options [Symbol] :organisation see {Rack::Backbone.cdn} @example
# The default: use Rack::Backbone # With a default organisation: use Rack::Backbone, :organisation => :cloudflare
# File lib/rack/backbone.rb, line 102 def initialize( app, options={} ) @app, @options = app, DEFAULT_OPTIONS.merge(options) @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 = options.fetch :organisation, :media_temple end
Public Instance Methods
For thread safety @param (see call
)
# 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.expand_path "../../../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.expand_path "../../../vendor/assets/javascripts/libs/backbone/#{BACKBONE_VERSION}/#{BACKBONE_SOURCE_MAP}", __FILE__) response.finish else @app.call(env) end end
@param [Hash] env Rack
request environment hash.
# File lib/rack/backbone.rb, line 111 def call( env ) dup._call env end