Class: Rack::Polymer
- Inherits:
-
Object
- Object
- Rack::Polymer
- 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
-
.cdn(env, opts = {}) ⇒ 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 = {}) ⇒ Polymer
constructor
A new instance of Polymer.
-
#serve_static_file(file) ⇒ Rack::Response
Helper method for serving static files.
Constructor Details
#initialize(app, options = {}) ⇒ Polymer
Returns a new instance of Polymer.
87 88 89 90 91 92 93 94 |
# File 'lib/rack/polymer.rb', line 87 def initialize( app, ={} ) @app, @options = app, DEFAULT_OPTIONS.merge() @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.
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
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
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.
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. "../../../vendor/assets/javascript/libs/polymer/#{POLYMER_VERSION}/#{file}", __FILE__) @response end |