Class: Rack::Polymer

Inherits:
Object
  • Object
show all
Includes:
JQuery::Helpers
Defined in:
lib/rack/polymer.rb,
lib/rack/polymer/version.rb

Defined Under Namespace

Modules: CDN

Constant Summary collapse

POLYMER_FILE_NAME =

The file name to use for the fallback route.

"polymer-#{POLYMER_VERSION}.min.js"
POLYMER_SOURCE_MAP =

The file name of the source map.

"polymer.min.js.map"
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.

<<STR
<script type="text/javascript">
  if (typeof Polymer == 'undefined') {
    document.write(unescape("%3Cscript src='/js/#{POLYMER_FILE_NAME}' type='text/javascript'%3E%3C/script%3E"))
  };
</script>
STR
DEFAULT_ORGANISATION =

The default CDN to use.

:cloudflare
DEFAULT_OPTIONS =

Default options hash for the middleware.

{
  :http_path => "/js",
  :organisation => DEFAULT_ORGANISATION
}
VERSION =

The version of this library.

"0.3.0"
POLYMER_VERSION =

The version of Polymer it supports

"0.0.20130816"
POLYMER_VERSION_DATE =
TODO:

remember to change Last-Modified with each release!

This is the release date of the Polymer file, it makes an easy “Last-Modified” date for setting the headers around caching.

"Fri, 16 Aug 2013 15:09:13 +0100"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Polymer.

Examples:

# 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

Parameters:

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

Options Hash (options):

  • :http_path (String)

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

  • :organisation (Symbol)

    Choose which CDN to use. The default is :cloudflare.



87
88
89
90
91
92
93
94
# 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

Class Method Details

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

Returns The HTML script tags to get the CDN.

Examples:

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 )

Parameters:

  • env (Hash)

    The rack env.

  • opts (Hash) (defaults to: {})

    Extra options.

Returns:

  • (String)

    The HTML script tags to get the CDN.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

Instance Method Details

#_call(env) ⇒ Object

For thread safety

Parameters:

  • env (Hash)

    Rack request environment hash.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

Parameters:

  • env (Hash)

    Rack request environment hash.



98
99
100
# File 'lib/rack/polymer.rb', line 98

def call( env )
  dup._call env
end

#serve_static_file(file) ⇒ Rack::Response

Helper method for serving static files.

Parameters:

  • file (String)

    The short file name.

Returns:

  • (Rack::Response)


133
134
135
136
137
138
# 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