rack-jquery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbx/
19
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - ruby-head
7
+ - jruby-19mode
8
+ - rbx-19mode
9
+
10
+ # whitelist
11
+ branches:
12
+ only:
13
+ - master
14
+ - develop
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-jquery.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ gem 'rack-test'
10
+ gem "timecop"
11
+ end
12
+
13
+ group :examples do
14
+ gem "sinatra"
15
+ gem "haml"
16
+ end
17
+
18
+ group :development do
19
+ gem "wirble"
20
+ gem "pry"
21
+ gem "pry-nav"
22
+ 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/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Rack::JQuery
2
+
3
+ [jQuery](http://jquery.com/download/) CDN script tags and fallback in one neat package.
4
+
5
+ ### Build status ###
6
+
7
+ Master branch:
8
+ [![Build Status](https://secure.travis-ci.org/yb66/rack-jquery.png?branch=master)](http://travis-ci.org/yb66/rack-jquery)
9
+
10
+ ## Why? ##
11
+
12
+ I get tired of copy and pasting and downloading and moving… jQuery files and script tags etc. This does it for me, and keeps version management nice 'n' easy.
13
+
14
+ ## Usage ##
15
+
16
+ Have a look in the examples directory, but here's a snippet.
17
+
18
+ * Install it (see below)
19
+ * Require `'rack/jquery`.
20
+ * Put this in the head of your layout (the example is Haml but you can use whatever you like)
21
+
22
+ <pre><code>
23
+ %head
24
+ = Rack::JQuery.cdn
25
+ </code></pre>
26
+
27
+ Now you have the script tags to Google's CDN in the head (you can also use Media Temple or Microsoft, see the docs).
28
+
29
+ It also adds in a bit of javascript that will load in a locally kept version of jQuery, just incase the CDN is unreachable. The script will use the "/js/jquery-#{VERSION}.min.js" path. You can change the "/js" bit if you like (see the docs).
30
+
31
+ That was easy.
32
+
33
+ ## Version numbers ##
34
+
35
+ 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 jQuery can be found in the lib/rack/jquery/version.rb file, or via the Rack::JQuery::JQUERY_VERSION constant.
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'rack-jquery'
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install rack-jquery
50
+
51
+ ## Contributing
52
+
53
+ 1. Fork it
54
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
55
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
56
+ 4. Push to the branch (`git push origin my-new-feature`)
57
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,59 @@
1
+ require 'sinatra/base'
2
+ require 'haml'
3
+ require 'rack/jquery'
4
+
5
+ class App < Sinatra::Base
6
+
7
+ enable :inline_templates
8
+ use Rack::JQuery
9
+
10
+ get "/" do
11
+ "RUNNING"
12
+ end
13
+
14
+ get "/google-cdn" do
15
+ haml :index, :layout => :google
16
+ end
17
+
18
+ get "/media-temple-cdn" do
19
+ haml :index, :layout => :mediatemple
20
+ end
21
+
22
+ get "/microsoft-cdn" do
23
+ haml :index, :layout => :microsoft
24
+ end
25
+
26
+ get "/unspecified-cdn" do
27
+ haml :index, :layout => :unspecified
28
+ end
29
+ end
30
+
31
+ __END__
32
+
33
+ @@google
34
+ %html
35
+ %head
36
+ = Rack::JQuery.cdn( :google )
37
+ = yield
38
+
39
+ @@microsoft
40
+ %html
41
+ %head
42
+ = Rack::JQuery.cdn( :microsoft )
43
+ = yield
44
+
45
+ @@mediatemple
46
+ %html
47
+ %head
48
+ = Rack::JQuery.cdn( :media_temple )
49
+ = yield
50
+
51
+ @@unspecified
52
+ %html
53
+ %head
54
+ = Rack::JQuery.cdn()
55
+ = yield
56
+
57
+ @@index
58
+ %p
59
+ "NOTHING TO SEE HERE, MOVE ALONG, MOVE ALONG"
@@ -0,0 +1,6 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup(:examples)
4
+ require File.expand_path( '../config.rb', __FILE__)
5
+
6
+ run App
@@ -0,0 +1,98 @@
1
+ require "rack/jquery/version"
2
+
3
+ module Rack
4
+
5
+ # jQuery CDN script tags and fallback in one neat package.
6
+ class JQuery
7
+
8
+ JQUERY_FILE_NAME = "jquery-#{JQUERY_VERSION}.min.js"
9
+
10
+ # Script tags for the Media Temple CDN
11
+ MEDIA_TEMPLE = "<script src='http://code.jquery.com/#{JQUERY_FILE_NAME}'></script>"
12
+
13
+ # Script tags for the Google CDN
14
+ GOOGLE = "<script src='//ajax.googleapis.com/ajax/libs/jquery/#{JQUERY_VERSION}/jquery.min.js'></script>"
15
+
16
+ # Script tags for the Microsoft CDN
17
+ MICROSOFT = "<script src='http://ajax.aspnetcdn.com/ajax/jQuery/#{JQUERY_FILE_NAME}'></script>"
18
+
19
+ # This javascript checks if the jQuery object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified jQuery.
20
+ FALLBACK = <<STR
21
+ <script type="text/javascript">
22
+ if (typeof jQuery == 'undefined') {
23
+ document.write(unescape("%3Cscript src='/js/#{JQUERY_FILE_NAME}' type='text/javascript'%3E))
24
+ };
25
+ </script>
26
+ STR
27
+
28
+ # HTTP date format.
29
+ # @see http://www.ietf.org/rfc/rfc1123.txt
30
+ HTTP_DATE = "%a, %d %b %Y %T GMT"
31
+
32
+ # Ten years in seconds.
33
+ TEN_YEARS = 60 * 60 * 24 * 365 * 10
34
+
35
+ # @param [Symbol] organisation Choose which CDN to use, either :google, :microsoft or :media_temple
36
+ # @return [String] The HTML script tags to get the CDN.
37
+ def self.cdn( organisation=:google )
38
+ script = case organisation
39
+ when :media_temple
40
+ MEDIA_TEMPLE
41
+ when :microsoft
42
+ MICROSOFT
43
+ else
44
+ GOOGLE
45
+ end
46
+ "#{script}\n#{FALLBACK}"
47
+ end
48
+
49
+
50
+ # Default options hash for the middleware.
51
+ DEFAULT_OPTIONS = {
52
+ :http_path => "/js"
53
+ }
54
+
55
+
56
+ # @param [#call] app
57
+ # @param [Hash] options
58
+ # @option options [String] :http_path If you wish the jQuery fallback route to be "/js/jquery-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/jquery-1.9.1.min.js" then pass in `:http_path => "/assets/javascripts".
59
+ # @example
60
+ # # The default:
61
+ # use Rack::JQuery
62
+ # # With a different route to the fallback:
63
+ # use Rack::JQuery, :http_path => "/assets/js"
64
+ def initialize( app, options={} )
65
+ @app, @options = app, DEFAULT_OPTIONS.merge(options)
66
+ @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_FILE_NAME
67
+ end
68
+
69
+
70
+ # @param [Hash] env Rack request environment hash.
71
+ def call( env )
72
+ request = Rack::Request.new(env.dup)
73
+ if request.path_info == @http_path_to_jquery
74
+ response = Rack::Response.new
75
+ # for caching
76
+ response.headers.merge!( {
77
+ "Last-Modified" => JQUERY_VERSION_DATE,
78
+ "Expires" => (Time.now + TEN_YEARS).strftime(HTTP_DATE),
79
+ "Cache-Control" => "max-age=#{TEN_YEARS},public",
80
+ "Etag" => "#{JQUERY_FILE_NAME}",
81
+ 'Content-Type' =>'application/javascript; charset=utf-8'
82
+ })
83
+
84
+ # 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.
85
+ if request.env['HTTP_IF_MODIFIED_SINCE']
86
+ response.status = 304
87
+ else
88
+ response.status = 200
89
+ response.write ::File.read( ::File.expand_path "../../../vendor/assets/javascripts/#{JQUERY_FILE_NAME}", __FILE__)
90
+ end
91
+ response.finish
92
+ else
93
+ @app.call(env)
94
+ end
95
+ end # call
96
+
97
+ end
98
+ end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+ class JQuery
3
+ VERSION = "0.0.1"
4
+ JQUERY_VERSION = "1.9.1"
5
+
6
+ # TODO remember to change Last-Modified with each release!
7
+ JQUERY_VERSION_DATE = "Mon, 04 Feb 2013 00:00:00 GMT"
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/jquery/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-jquery"
8
+ spec.version = Rack::JQuery::VERSION
9
+ spec.authors = ["Iain Barnett"]
10
+ spec.email = ["iainspeed@gmail.com"]
11
+ spec.description = %q{jQuery CDN script tags and fallback in one neat package.}
12
+ spec.summary = %q{The description says it all.}
13
+ spec.homepage = "https://github.com/yb66/rack-jquery"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency("rack")
24
+ end
@@ -0,0 +1,93 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+ require_relative "../lib/rack/jquery.rb"
5
+
6
+ describe "The class methods" do
7
+ subject { Rack::JQuery.cdn organisation }
8
+ context "Given an argument" do
9
+ context "of nil (the default)" do
10
+ let(:organisation) { nil }
11
+ it { should == "#{Rack::JQuery::GOOGLE}\n#{Rack::JQuery::FALLBACK}" }
12
+ end
13
+ context "of :google" do
14
+ let(:organisation) { :google }
15
+ it { should == "#{Rack::JQuery::GOOGLE}\n#{Rack::JQuery::FALLBACK}" }
16
+ end
17
+ context "of :microsoft" do
18
+ let(:organisation) { :microsoft }
19
+ it { should == "#{Rack::JQuery::MICROSOFT}\n#{Rack::JQuery::FALLBACK}" }
20
+ end
21
+ context "of :media_temple" do
22
+ let(:organisation) { :media_temple }
23
+ it { should == "#{Rack::JQuery::MEDIA_TEMPLE}\n#{Rack::JQuery::FALLBACK}" }
24
+ end
25
+ end
26
+ end
27
+
28
+ describe "Inserting the CDN" do
29
+ include_context "All routes"
30
+ context "Google CDN" do
31
+ before do
32
+ get "/google-cdn"
33
+ end
34
+ it_should_behave_like "Any route"
35
+ subject { last_response.body }
36
+ let(:expected) { Rack::JQuery::GOOGLE }
37
+ it { should include expected }
38
+ end
39
+ context "Microsoft CDN" do
40
+ before do
41
+ get "/microsoft-cdn"
42
+ end
43
+ it_should_behave_like "Any route"
44
+ subject { last_response.body }
45
+ let(:expected) { Rack::JQuery::MICROSOFT }
46
+ it { should include expected }
47
+ end
48
+ context "Media_temple CDN" do
49
+ before do
50
+ get "/media-temple-cdn"
51
+ end
52
+ it_should_behave_like "Any route"
53
+ subject { last_response.body }
54
+ let(:expected) { Rack::JQuery::MEDIA_TEMPLE }
55
+ it { should include expected }
56
+ end
57
+ context "Unspecified CDN" do
58
+ before do
59
+ get "/unspecified-cdn"
60
+ end
61
+ it_should_behave_like "Any route"
62
+ subject { last_response.body }
63
+ let(:expected) { Rack::JQuery::GOOGLE }
64
+ it { should include expected }
65
+ end
66
+ end
67
+
68
+
69
+ require 'timecop'
70
+ require 'time'
71
+
72
+ describe "Serving the fallback jquery" do
73
+ include_context "All routes"
74
+ before do
75
+ get "/js/jquery-#{Rack::JQuery::JQUERY_VERSION}.min.js"
76
+ end
77
+ it_should_behave_like "Any route"
78
+ subject { last_response.body }
79
+ it { should start_with "/*! jQuery v1.9.1" }
80
+
81
+ context "Re requests" do
82
+ before do
83
+ at_start = Time.parse(Rack::JQuery::JQUERY_VERSION_DATE) + 60 * 60 * 24 * 180
84
+ Timecop.freeze at_start
85
+ get "/js/jquery-#{Rack::JQuery::JQUERY_VERSION}.min.js"
86
+ Timecop.travel Time.now + 86400 # add a day
87
+ get "/js/jquery-#{Rack::JQuery::JQUERY_VERSION}.min.js", {}, {"HTTP_IF_MODIFIED_SINCE" => at_start.strftime(Rack::JQuery::HTTP_DATE) }
88
+ end
89
+ subject { last_response }
90
+ its(:status) { should == 304 }
91
+
92
+ end
93
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ Spec_dir = File.expand_path( File.dirname __FILE__ )
5
+
6
+ unless Kernel.respond_to?(:require_relative)
7
+ module Kernel
8
+ def require_relative(path)
9
+ require File.join(File.dirname(caller[0]), path.to_str)
10
+ end
11
+ end
12
+ end
13
+
14
+ # code coverage
15
+ require 'simplecov'
16
+ SimpleCov.start do
17
+ add_filter "/vendor/"
18
+ add_filter "/bin/"
19
+ end
20
+
21
+ require "rack/test"
22
+ ENV['RACK_ENV'] ||= 'test'
23
+ ENV["EXPECT_WITH"] ||= "racktest"
24
+
25
+
26
+ Dir[ File.join( Spec_dir, "/support/**/*.rb")].each do |f|
27
+ require f
28
+ end
29
+
30
+ RSpec.configure do |config|
31
+ config.treat_symbols_as_metadata_keys_with_true_values = true
32
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ config_path = File.expand_path '../../../examples/config.rb', File.dirname(__FILE__)
4
+ require_relative config_path
5
+
6
+ shared_context "All routes" do
7
+ include Rack::Test::Methods
8
+ let(:app){ App }
9
+ end
10
+
11
+ shared_examples_for "Any route" do
12
+ subject { last_response }
13
+ it { should be_ok }
14
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-jquery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Iain Barnett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rack
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: jQuery CDN script tags and fallback in one neat package.
63
+ email:
64
+ - iainspeed@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENCE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - examples/config.rb
76
+ - examples/config.ru
77
+ - lib/rack/jquery.rb
78
+ - lib/rack/jquery/version.rb
79
+ - rack-jquery.gemspec
80
+ - spec/rack_jquery_spec.rb
81
+ - spec/spec_helper.rb
82
+ - spec/support/shared/all_pages.rb
83
+ homepage: https://github.com/yb66/rack-jquery
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: The description says it all.
108
+ test_files:
109
+ - spec/rack_jquery_spec.rb
110
+ - spec/spec_helper.rb
111
+ - spec/support/shared/all_pages.rb