class Rack::Polymer

Constants

DEFAULT_OPTIONS

Default options hash for the middleware.

DEFAULT_ORGANISATION

The default CDN to use.

FALLBACK

This javascript checks if the Polymer object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified Polymer.

POLYMER_FILE_NAME

The file name to use for the fallback route.

POLYMER_SOURCE_MAP

The file name of the source map.

POLYMER_VERSION

The version of Polymer it supports

POLYMER_VERSION_DATE

This is the release date of the Polymer file, it makes an easy “Last-Modified” date for setting the headers around caching. @todo remember to change Last-Modified with each release!

VERSION

The version of this library.

Public Class Methods

cdn( env, opts={} ) click to toggle source

@param [Hash] env The rack env. @param [Hash] opts Extra options. @options opts [Symbol] :organisation Choose which CDN to use. The default is :cloudflare. If an organisation was set via the Rack env this will override it. @options opts [FalseClass] :cdn Mark as false if you *don’t* want to use the CDN and only want to use the fallback script. This option is primarily here so I can use the latest script without relying on the CDN, but use it however you wish. @return [String] The HTML script tags to get the CDN. @example

Rack::Polymer.cdn( env )

# Choose an organisation
Rack::Polymer.cdn( env, organisation: :cloudflare )

# Don't use a CDN, just use the fallback
Rack::Polymer.cdn( env, cdn: false )
# File lib/rack/polymer.rb, line 47
def self.cdn( env, opts={} )
  organisation =  opts[:organisation] ||
                    (env["rack.polymer"] && env["rack.polymer"]["organisation"]) ||
                    Rack::Polymer::DEFAULT_ORGANISATION

  script = case organisation
    when :cloudflare then CDN::CLOUDFLARE
    else CDN::CLOUDFLARE
  end

  opts[:cdn] == false ?
    FALLBACK :
    "<script src='#{script}'></script>\n#{FALLBACK}"
end
new( app, options={} ) click to toggle source

@param [#call] app @param [Hash] options @option options [String] :http_path If you wish the Polymer fallback route to be “/js/polymer-0.0.20130711.min.js” (or whichever version this is at) then do nothing, that’s the default. If you want the path to be “/assets/javascripts/polymer-0.0.20130711.min.js” then pass in ‘:http_path => “/assets/javascripts”. @option options [Symbol] :organisation Choose which CDN to use. The default is :cloudflare. @example

# The default:
use Rack::Polymer

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

# With the CDN specified via the use statement
use Rack::Polymer, :organisation => :cloudflare
# File lib/rack/polymer.rb, line 87
def initialize( app, options={} )
  @app, @options  = app, DEFAULT_OPTIONS.merge(options)
  @http_path_to_polymer, @http_path_to_polymer_source_map =
    [POLYMER_FILE_NAME,POLYMER_SOURCE_MAP].map{|file_name|
      ::File.join @options[:http_path], file_name
    }
  @organisation = @options[:organisation]
end

Public Instance Methods

_call( env ) click to toggle source

For thread safety @param (see call)

# File lib/rack/polymer.rb, line 105
def _call( env )
  env.merge! "rack.polymer" => {"organisation" => @organisation}

  request = Rack::Request.new(env.dup)
  if request.path_info == @http_path_to_polymer
    @response = Rack::Response.new
    # for caching
    @response.headers.merge! caching_headers( POLYMER_FILE_NAME, POLYMER_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
      serve_static_file "polymer.min.js"
    end
    @response.finish
  elsif request.path_info == @http_path_to_polymer_source_map
    # The source map isn't cached
    serve_static_file( "polymer.min.js.map" ).finish
  else
    @app.call(env)
  end
end
call( env ) click to toggle source

@param [Hash] env Rack request environment hash.

# File lib/rack/polymer.rb, line 98
def call( env )
  dup._call env
end
serve_static_file( file ) click to toggle source

Helper method for serving static files. @param [String] file The short file name. @return [Rack::Response]

# File lib/rack/polymer.rb, line 133
def serve_static_file( file )
  @response ||= Rack::Response.new
  @response.status = 200
  @response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascript/libs/polymer/#{POLYMER_VERSION}/#{file}", __FILE__)
  @response
end