rack-polymer 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +11 -0
- data/CHANGES.md +32 -0
- data/Gemfile +21 -0
- data/LICENCE.txt +22 -0
- data/Polymer-LICENSE +27 -0
- data/README.md +81 -0
- data/Rakefile +73 -0
- data/examples/config.rb +90 -0
- data/examples/config.ru +10 -0
- data/examples/public/x-foo.html +23 -0
- data/lib/rack/polymer.rb +141 -0
- data/lib/rack/polymer/version.rb +14 -0
- data/rack-polymer.gemspec +25 -0
- data/spec/rack_lodash_spec.rb +94 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/shared/all_pages.rb +14 -0
- data/vendor/assets/javascript/libs/polymer/0.0.20130816/polymer.min.js +37 -0
- data/vendor/assets/javascript/libs/polymer/0.0.20130816/polymer.min.js.map +1 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 50cf117879336a91ecc1d51591f36b0c7b5cccd4
|
4
|
+
data.tar.gz: d245ed9f1cd9f3f852bc80de4fe9ee06c3cb3f18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ddb8a8482b22baf25575b20c9254b36d7da553f678d548a5052e10a5e56d360600a2986791bf39b39b78aed54490a37a58705a77f8f0ebc87c50c6e817db7a8
|
7
|
+
data.tar.gz: 1c52746e4cd8b1aa20972f483b566839e1f0a09ef95ccd684c08acf4b00d9ab4eb1df3c46df50ddd07921486d7cec7a99a462bf73a6e12ebb11e302ad977e6aa
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGES.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# CH CH CH CH CHANGES! #
|
2
|
+
|
3
|
+
|
4
|
+
## Thursday the 29th of August 2013 ##
|
5
|
+
|
6
|
+
### v0.3.0 ###
|
7
|
+
|
8
|
+
* Updated the version of Polymer to 0.0.20130816.
|
9
|
+
|
10
|
+
|
11
|
+
### v0.2.0 ###
|
12
|
+
|
13
|
+
* Added route for the javascript source map.
|
14
|
+
|
15
|
+
----
|
16
|
+
|
17
|
+
|
18
|
+
## Wednesday the 28th of August 2013 ##
|
19
|
+
|
20
|
+
### v0.1.1 ###
|
21
|
+
|
22
|
+
* Was using a newer version of the source map that I should've been. Fixed.
|
23
|
+
|
24
|
+
### v0.1.0 ###
|
25
|
+
|
26
|
+
* Added :cdn option to opt out of using the CDN, mainly so I can use the newer scripts without waiting for the CDNs to catch up.
|
27
|
+
|
28
|
+
### v0.0.1 ###
|
29
|
+
|
30
|
+
* First version.
|
31
|
+
|
32
|
+
----
|
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in rack-polymer.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem "wirble"
|
8
|
+
gem "yard"
|
9
|
+
end
|
10
|
+
|
11
|
+
group :examples do
|
12
|
+
gem "sinatra"
|
13
|
+
gem "haml"
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "rspec"
|
18
|
+
gem "rack-test"
|
19
|
+
gem "simplecov"
|
20
|
+
gem "timecop"
|
21
|
+
end
|
data/LICENCE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Iain Barnett
|
2
|
+
|
3
|
+
MIT Licence
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Polymer-LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
// Copyright (c) 2012 The Polymer Authors. All rights reserved.
|
2
|
+
//
|
3
|
+
// Redistribution and use in source and binary forms, with or without
|
4
|
+
// modification, are permitted provided that the following conditions are
|
5
|
+
// met:
|
6
|
+
//
|
7
|
+
// * Redistributions of source code must retain the above copyright
|
8
|
+
// notice, this list of conditions and the following disclaimer.
|
9
|
+
// * Redistributions in binary form must reproduce the above
|
10
|
+
// copyright notice, this list of conditions and the following disclaimer
|
11
|
+
// in the documentation and/or other materials provided with the
|
12
|
+
// distribution.
|
13
|
+
// * Neither the name of Google Inc. nor the names of its
|
14
|
+
// contributors may be used to endorse or promote products derived from
|
15
|
+
// this software without specific prior written permission.
|
16
|
+
//
|
17
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
18
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
19
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
20
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
21
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
24
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
25
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
26
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Rack::Polymer #
|
2
|
+
|
3
|
+
[Polymer](http://www.polymer-project.org/) CDN script tags and fallback in one neat package. Current version is for Polymer v#{Rack::Polymer::POLYMER_VERSION} (or if you're reading this in the source and that number doesn't display, look in the lib/rack/polymer/version.rb file).
|
4
|
+
|
5
|
+
### Build status ###
|
6
|
+
|
7
|
+
Master branch:
|
8
|
+
[![Build Status](https://travis-ci.org/yb66/rack-polymer.png?branch=master)](https://travis-ci.org/yb66/rack-polymer)
|
9
|
+
|
10
|
+
|
11
|
+
## Why? ##
|
12
|
+
|
13
|
+
Because then I don't have to worry about versioning or fallback.
|
14
|
+
|
15
|
+
## Installation ##
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'rack-polymer'
|
20
|
+
|
21
|
+
And then, from a command-line, execute:
|
22
|
+
|
23
|
+
bundle install
|
24
|
+
|
25
|
+
Or even better:
|
26
|
+
|
27
|
+
bundle install --binstubs --path vendor
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
gem install rack-polymer -r
|
32
|
+
|
33
|
+
## Usage ##
|
34
|
+
|
35
|
+
In your rackup file or Sinatra class (or wherever you like to set up Rack middleware…)
|
36
|
+
|
37
|
+
use Rack::Polymer
|
38
|
+
|
39
|
+
Then wherever you need the script loaded (like in a layout file):
|
40
|
+
|
41
|
+
Rack::Polymer.cdn( env )
|
42
|
+
|
43
|
+
That's it. There are more options, check the docs.
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
If you're going to change the code:
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
54
|
+
|
55
|
+
You can also help by informing me of CDNs that are hosting it and/or releases of the Polymer library. Just add an issue.
|
56
|
+
|
57
|
+
|
58
|
+
### Version numbers ###
|
59
|
+
|
60
|
+
This library uses [semver](http://semver.org/) to version the **library**. That means the library version is ***not*** an indicator of quality but a way to manage changes. The version of Polymer can be found in the lib/rack/polymer/version.rb file, or via the {Rack::Polymer::POLYMER_VERSION} constant.
|
61
|
+
|
62
|
+
On top of that, version numbers will also change when new releases of Polymer are supported.
|
63
|
+
|
64
|
+
* If Polymer makes a major version jump, then this library will make a ***minor*** jump. That is because the API for the library has not really changed, but it is *possibly* a change that will break things.
|
65
|
+
* If Polymer makes a minor version jump, then so will this library, for the same reason as above.
|
66
|
+
* I doubt point releases will be followed, but if so, it will also precipitate a minor jump in this library's version number. That's because even though Polymer feel it's a point release, I'm not them, my responsibility is to users of this library and I'll take the cautious approach of making it a minor version number change.
|
67
|
+
|
68
|
+
As an example, if the current library version was 1.0.0 and Polymer was at 2.0.0 and I made a change that I felt was major and breaking (to the Ruby library), I'd bump Rack::Polymer's version to 2.0.0. That the version numbers match between Rack::Polymer and the Polymer script is of no significance, it's just coincidental.
|
69
|
+
If then Polymer went to v2.1.0 and I decided to support that, I'd make the changes and bump Rack::Polymer's version to 2.1.0. That the version numbers match between Rack::Polymer and the Polymer script is of no significance, it's just coincidental.
|
70
|
+
If then I made a minor change to the library's API that could be breaking I'd bump it to 2.2.0.
|
71
|
+
If I then added some more instructions I'd bump Rack::Polymer's version to 2.2.1.
|
72
|
+
If then Polymer released version 3.0.0, I'd add it to the library, and bump Rack::Polymer's version to 2.3.0.
|
73
|
+
|
74
|
+
Only one version of Polymer will be supported at a time. This is because the fallback script is shipped with the gem and I'd like to keep it as light as possible. It's also a headache to have more than one.
|
75
|
+
|
76
|
+
So basically, if you want to use a specific version of Polymer, look for the library version that supports it via the {Rack::Polymer::POLYMER_VERSION} constant. Don't rely on the version numbers of *this* library to tell you anything other than compatibility between versions of this library.
|
77
|
+
|
78
|
+
|
79
|
+
## Licences ##
|
80
|
+
|
81
|
+
The licence for this library can be found in the LICENCE.txt file. The licence for parts taken from the Polymer library (like the fallback script, bits for examples and specs etc) is to be found in the Polymer-LICENSE file.
|
data/Rakefile
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
|
4
|
+
desc "(Re-) generate documentation and place it in the docs/ dir. Open the index.html file in there to read it."
|
5
|
+
task :docs => [:"docs:environment", :"docs:yard"]
|
6
|
+
namespace :docs do
|
7
|
+
|
8
|
+
task :environment do
|
9
|
+
ENV["RACK_ENV"] = "documentation"
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'yard'
|
13
|
+
|
14
|
+
YARD::Rake::YardocTask.new :yard do |t|
|
15
|
+
t.files = ['lib/**/*.rb', 'app/*.rb', 'spec/**/*.rb']
|
16
|
+
t.options = ['-odocs/'] # optional
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => "spec"
|
22
|
+
|
23
|
+
task :spec => :"spec:run"
|
24
|
+
task :rspec => :spec
|
25
|
+
namespace :spec do
|
26
|
+
task :environment do
|
27
|
+
ENV["RACK_ENV"] = "test"
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Run specs"
|
31
|
+
task :run, [:any_args] => :"spec:environment" do |t,args|
|
32
|
+
warn "Entering spec task."
|
33
|
+
any_args = args[:any_args] || ""
|
34
|
+
cmd = "bin/rspec #{any_args}"
|
35
|
+
warn cmd
|
36
|
+
system cmd
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
namespace :examples do
|
42
|
+
|
43
|
+
desc "Run the examples."
|
44
|
+
task :run do
|
45
|
+
exec "bundle exec rackup examples/config.ru"
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
namespace :cdn do
|
52
|
+
require 'open3'
|
53
|
+
desc "An availability check, for sanity"
|
54
|
+
task :check do
|
55
|
+
require_relative './lib/rack/polymer.rb'
|
56
|
+
Rack::Polymer::CDN.constants.each do |const|
|
57
|
+
url = "#{Rack::Polymer::CDN.const_get(const)}"
|
58
|
+
url = "http:#{url}" unless url.start_with? "http"
|
59
|
+
cmd = "curl -I #{url}"
|
60
|
+
puts cmd
|
61
|
+
puts catch(:status) {
|
62
|
+
Open3.popen3(cmd) do |_,stdout,_|
|
63
|
+
line = stdout.gets
|
64
|
+
throw :status, "Nothing for #{const}" if line.nil?
|
65
|
+
puts line.match("HTTP/1.1 404 Not Found") ?
|
66
|
+
"FAILED: #{const}" :
|
67
|
+
"PASSED: #{const}"
|
68
|
+
end
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
data/examples/config.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'haml'
|
3
|
+
require 'rack/polymer'
|
4
|
+
|
5
|
+
class App < Sinatra::Base
|
6
|
+
|
7
|
+
enable :inline_templates
|
8
|
+
use Rack::Polymer
|
9
|
+
|
10
|
+
get "/" do
|
11
|
+
output = <<STR
|
12
|
+
!!!
|
13
|
+
%body
|
14
|
+
%ul
|
15
|
+
%li
|
16
|
+
%a{ href: "/cloudflare-cdn"} cloudflare-cdn
|
17
|
+
%li
|
18
|
+
%a{ href: "/unspecified-cdn"} unspecified-cdn
|
19
|
+
%li
|
20
|
+
%a{ href: "/specified-via-use"} specified-via-use
|
21
|
+
%li
|
22
|
+
%a{ href: "/no-cdn"} no-cdn
|
23
|
+
STR
|
24
|
+
haml output
|
25
|
+
end
|
26
|
+
|
27
|
+
get "/cloudflare-cdn" do
|
28
|
+
haml :index, :layout => :cloudflare
|
29
|
+
end
|
30
|
+
|
31
|
+
get "/unspecified-cdn" do
|
32
|
+
haml :index, :layout => :unspecified
|
33
|
+
end
|
34
|
+
|
35
|
+
get "/no-cdn" do
|
36
|
+
haml :index, :layout => :no_cdn
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# This is probably the one I'd use.
|
42
|
+
class App2 < Sinatra::Base
|
43
|
+
|
44
|
+
enable :inline_templates
|
45
|
+
use Rack::Polymer, :organisation => :cloudflare
|
46
|
+
|
47
|
+
get "/" do
|
48
|
+
haml :index, :layout => :specified_via_use
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
__END__
|
53
|
+
|
54
|
+
|
55
|
+
@@ cloudflare
|
56
|
+
!!!
|
57
|
+
%head
|
58
|
+
= Rack::Polymer.cdn( env, :organisation => :cloudflare )
|
59
|
+
%link{ rel: "import", href: "x-foo.html" }
|
60
|
+
%body
|
61
|
+
= yield
|
62
|
+
|
63
|
+
@@ unspecified
|
64
|
+
!!!
|
65
|
+
%head
|
66
|
+
= Rack::Polymer.cdn(env)
|
67
|
+
%link{ rel: "import", href: "x-foo.html" }
|
68
|
+
%body
|
69
|
+
= yield
|
70
|
+
|
71
|
+
@@ specified_via_use
|
72
|
+
!!!
|
73
|
+
%head
|
74
|
+
= Rack::Polymer.cdn(env)
|
75
|
+
%link{ rel: "import", href: "x-foo.html" }
|
76
|
+
%body
|
77
|
+
= yield
|
78
|
+
|
79
|
+
|
80
|
+
@@ no_cdn
|
81
|
+
!!!
|
82
|
+
%head
|
83
|
+
= Rack::Polymer.cdn(env, cdn: false)
|
84
|
+
%link{ rel: "import", href: "x-foo.html" }
|
85
|
+
%body
|
86
|
+
= yield
|
87
|
+
|
88
|
+
@@index
|
89
|
+
%p The polymer-all/polymer/workbench/oldSmoke/x-foo.html should be imported below.
|
90
|
+
<x-foo></x-foo>
|
data/examples/config.ru
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
<!--
|
2
|
+
Copyright 2013 The Polymer Authors. All rights reserved.
|
3
|
+
Use of this source code is governed by a BSD-style
|
4
|
+
license that can be found in the LICENSE file.
|
5
|
+
-->
|
6
|
+
<polymer-element name="x-foo">
|
7
|
+
<template>
|
8
|
+
It LIVES <content></content>
|
9
|
+
<x-bar label="{{label}}"></x-bar>
|
10
|
+
</template>
|
11
|
+
<script>
|
12
|
+
Polymer('x-foo', {
|
13
|
+
label: '',
|
14
|
+
ready: function(root) {
|
15
|
+
this.textContent = 'cordon bleu';
|
16
|
+
this.blueate();
|
17
|
+
},
|
18
|
+
blueate: function() {
|
19
|
+
this.style.backgroundColor = 'lightblue';
|
20
|
+
}
|
21
|
+
});
|
22
|
+
</script>
|
23
|
+
</polymer-element>
|
data/lib/rack/polymer.rb
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
require "rack/polymer/version"
|
2
|
+
require "rack/jquery/helpers"
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
class Polymer
|
6
|
+
include Rack::JQuery::Helpers # for caching
|
7
|
+
|
8
|
+
# Namespaced CDNs for convenience.
|
9
|
+
module CDN
|
10
|
+
|
11
|
+
# Script tags for the Media Temple CDN
|
12
|
+
CLOUDFLARE = "//cdnjs.cloudflare.com/ajax/libs/polymer/#{POLYMER_VERSION}/polymer.min.js"
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# The file name to use for the fallback route.
|
18
|
+
POLYMER_FILE_NAME = "polymer-#{POLYMER_VERSION}.min.js"
|
19
|
+
|
20
|
+
# The file name of the source map.
|
21
|
+
POLYMER_SOURCE_MAP = "polymer.min.js.map"
|
22
|
+
|
23
|
+
|
24
|
+
# 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.
|
25
|
+
FALLBACK = <<STR
|
26
|
+
<script type="text/javascript">
|
27
|
+
if (typeof Polymer == 'undefined') {
|
28
|
+
document.write(unescape("%3Cscript src='/js/#{POLYMER_FILE_NAME}' type='text/javascript'%3E%3C/script%3E"))
|
29
|
+
};
|
30
|
+
</script>
|
31
|
+
STR
|
32
|
+
|
33
|
+
|
34
|
+
# @param [Hash] env The rack env.
|
35
|
+
# @param [Hash] opts Extra options.
|
36
|
+
# @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.
|
37
|
+
# @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.
|
38
|
+
# @return [String] The HTML script tags to get the CDN.
|
39
|
+
# @example
|
40
|
+
# Rack::Polymer.cdn( env )
|
41
|
+
#
|
42
|
+
# # Choose an organisation
|
43
|
+
# Rack::Polymer.cdn( env, organisation: :cloudflare )
|
44
|
+
#
|
45
|
+
# # Don't use a CDN, just use the fallback
|
46
|
+
# Rack::Polymer.cdn( env, cdn: false )
|
47
|
+
def self.cdn( env, opts={} )
|
48
|
+
organisation = opts[:organisation] ||
|
49
|
+
(env["rack.polymer"] && env["rack.polymer"]["organisation"]) ||
|
50
|
+
Rack::Polymer::DEFAULT_ORGANISATION
|
51
|
+
|
52
|
+
script = case organisation
|
53
|
+
when :cloudflare then CDN::CLOUDFLARE
|
54
|
+
else CDN::CLOUDFLARE
|
55
|
+
end
|
56
|
+
|
57
|
+
opts[:cdn] == false ?
|
58
|
+
FALLBACK :
|
59
|
+
"<script src='#{script}'></script>\n#{FALLBACK}"
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# The default CDN to use.
|
64
|
+
DEFAULT_ORGANISATION = :cloudflare
|
65
|
+
|
66
|
+
|
67
|
+
# Default options hash for the middleware.
|
68
|
+
DEFAULT_OPTIONS = {
|
69
|
+
:http_path => "/js",
|
70
|
+
:organisation => DEFAULT_ORGANISATION
|
71
|
+
}
|
72
|
+
|
73
|
+
|
74
|
+
# @param [#call] app
|
75
|
+
# @param [Hash] options
|
76
|
+
# @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".
|
77
|
+
# @option options [Symbol] :organisation Choose which CDN to use. The default is :cloudflare.
|
78
|
+
# @example
|
79
|
+
# # The default:
|
80
|
+
# use Rack::Polymer
|
81
|
+
#
|
82
|
+
# # With a different route to the fallback:
|
83
|
+
# use Rack::Polymer, :http_path => "/assets/js"
|
84
|
+
#
|
85
|
+
# # With the CDN specified via the use statement
|
86
|
+
# use Rack::Polymer, :organisation => :cloudflare
|
87
|
+
def initialize( app, options={} )
|
88
|
+
@app, @options = app, DEFAULT_OPTIONS.merge(options)
|
89
|
+
@http_path_to_polymer, @http_path_to_polymer_source_map =
|
90
|
+
[POLYMER_FILE_NAME,POLYMER_SOURCE_MAP].map{|file_name|
|
91
|
+
::File.join @options[:http_path], file_name
|
92
|
+
}
|
93
|
+
@organisation = @options[:organisation]
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
# @param [Hash] env Rack request environment hash.
|
98
|
+
def call( env )
|
99
|
+
dup._call env
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
# For thread safety
|
104
|
+
# @param (see #call)
|
105
|
+
def _call( env )
|
106
|
+
env.merge! "rack.polymer" => {"organisation" => @organisation}
|
107
|
+
|
108
|
+
request = Rack::Request.new(env.dup)
|
109
|
+
if request.path_info == @http_path_to_polymer
|
110
|
+
@response = Rack::Response.new
|
111
|
+
# for caching
|
112
|
+
@response.headers.merge! caching_headers( POLYMER_FILE_NAME, POLYMER_VERSION_DATE)
|
113
|
+
|
114
|
+
# 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.
|
115
|
+
if request.env['HTTP_IF_MODIFIED_SINCE']
|
116
|
+
@response.status = 304
|
117
|
+
else
|
118
|
+
serve_static_file "polymer.min.js"
|
119
|
+
end
|
120
|
+
@response.finish
|
121
|
+
elsif request.path_info == @http_path_to_polymer_source_map
|
122
|
+
# The source map isn't cached
|
123
|
+
serve_static_file( "polymer.min.js.map" ).finish
|
124
|
+
else
|
125
|
+
@app.call(env)
|
126
|
+
end
|
127
|
+
end # call
|
128
|
+
|
129
|
+
|
130
|
+
# Helper method for serving static files.
|
131
|
+
# @param [String] file The short file name.
|
132
|
+
# @return [Rack::Response]
|
133
|
+
def serve_static_file( file )
|
134
|
+
@response ||= Rack::Response.new
|
135
|
+
@response.status = 200
|
136
|
+
@response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascript/libs/polymer/#{POLYMER_VERSION}/#{file}", __FILE__)
|
137
|
+
@response
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
end
|