Class: Rack::Backbone

Inherits:
Object
  • Object
show all
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-#{BACKBONE_VERSION}-min.js"
BACKBONE_SOURCE_MAP =

Fallback source map file name.

"backbone-#{BACKBONE_VERSION}-min.map"
FALLBACK =

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.

"<script type=\"text/javascript\">\n  if (typeof Backbone == 'undefined') {\n    document.write(unescape(\"%3Cscript src='/js/\#{BACKBONE_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

"0.0.2"
BACKBONE_VERSION =

the version of Backbone it supports.

"1.0.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.

"Wed, 20 Mar 2013 12:13:55 +0000"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Backbone.

Examples:

# The default:
use Rack::Backbone

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

# With a default organisation:
use Rack::Backbone, :organisation => :cloudflare

Parameters:

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

Options Hash (options):

  • :http_path (String)

    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”.

  • :organisation (Symbol)

    see cdn



81
82
83
84
85
86
# File 'lib/rack/backbone.rb', line 81

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

Class Method Details

.cdn(env, options = {}) ⇒ String

Returns The HTML script tags to get the CDN.

Parameters:

  • env (Hash)

    The rack env hash.

  • organisation (Symbol)

    Choose which CDN to use, either :jsdelivr, or :cloudflare (the default). This will override anything set via the use statement.

Returns:

  • (String)

    The HTML script tags to get the CDN.



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

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

  organisation =  options[:organisation] ||
                    env["rack.backbone.organisation"] ||
                    :media_temple

  script = case organisation
    when :cloudflare
      CDN::CLOUDFLARE
    when :jsdelivr
      CDN::JSDELIVR
    else
      CDN::CLOUDFLARE
  end
  "<script src='#{script}'></script>\n#{FALLBACK}"
end

Instance Method Details

#_call(env) ⇒ Object

For thread safety

Parameters:

  • env (Hash)

    Rack request environment hash.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rack/backbone.rb', line 97

def _call( env )
  request = Rack::Request.new(env.dup)
  env.merge! "rack.backbone.organisation" => @organisation
  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/#{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/#{BACKBONE_SOURCE_MAP}", __FILE__)
    response.finish
  else
    @app.call(env)
  end
end

#call(env) ⇒ Object

Parameters:

  • env (Hash)

    Rack request environment hash.



90
91
92
# File 'lib/rack/backbone.rb', line 90

def call( env )
  dup._call env
end