rack-jquery 1.5.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # CH CH CH CHANGES #
2
2
 
3
+ ## Saturday the 7th of September 2013, v2.0.0 ##
4
+
5
+ * Added ability to pass `use` a default organisation.
6
+ * Added a failure message for those who don't notice the API changes.
7
+
8
+ ----
9
+
3
10
  ## Friday the 6th of September 2013, v1.5.1 ##
4
11
 
5
12
  * Added the source map and fallback routes for it.
data/README.md CHANGED
@@ -18,11 +18,11 @@ Have a look in the examples directory, but here's a snippet.
18
18
  * Install it (see below)
19
19
  * `require 'rack/jquery'`.
20
20
  * If you want fallback then add this to your middleware stack: `use Rack::JQuery`
21
- * Put this in the head of your layout (the example is Haml but you can use whatever you like)
21
+ * Put this in the head of your layout (the example is Haml but you can use whatever you like) and pass it the Rack env:
22
22
 
23
23
  <pre><code>
24
24
  %head
25
- = Rack::JQuery.cdn
25
+ = Rack::JQuery.cdn( env )
26
26
  </code></pre>
27
27
 
28
28
  Now you have the script tags to Media Temple's CDN in the head (you can also use Google or Microsoft, or Cloudflare, see the docs).
data/examples/config.rb CHANGED
@@ -47,36 +47,82 @@ STR
47
47
  end
48
48
  end
49
49
 
50
+
51
+ class AppWithDefaults < Sinatra::Base
52
+
53
+ enable :inline_templates
54
+ use Rack::JQuery, :organisation => :cloudflare
55
+
56
+ get "/" do
57
+ output = <<STR
58
+ !!!
59
+ %body
60
+ %ul
61
+ %li
62
+ %a{ href: "/google-cdn"} google-cdn
63
+ %li
64
+ %a{ href: "/media-temple-cdn"} media-temple-cdn
65
+ %li
66
+ %a{ href: "/microsoft-cdn"} microsoft-cdn
67
+ %li
68
+ %a{ href: "/cloudflare-cdn"} cloudflare-cdn
69
+ %li
70
+ %a{ href: "/unspecified-cdn"} unspecified-cdn
71
+ STR
72
+ haml output
73
+ end
74
+
75
+ get "/google-cdn" do
76
+ haml :index, :layout => :google
77
+ end
78
+
79
+ get "/media-temple-cdn" do
80
+ haml :index, :layout => :mediatemple
81
+ end
82
+
83
+ get "/microsoft-cdn" do
84
+ haml :index, :layout => :microsoft
85
+ end
86
+
87
+ get "/cloudflare-cdn" do
88
+ haml :index, :layout => :cloudflare
89
+ end
90
+
91
+ get "/unspecified-cdn" do
92
+ haml :index, :layout => :unspecified
93
+ end
94
+ end
95
+
50
96
  __END__
51
97
 
52
98
  @@google
53
99
  %html
54
100
  %head
55
- = Rack::JQuery.cdn( :google )
101
+ = Rack::JQuery.cdn(env, :organisation => :google )
56
102
  = yield
57
103
 
58
104
  @@microsoft
59
105
  %html
60
106
  %head
61
- = Rack::JQuery.cdn( :microsoft )
107
+ = Rack::JQuery.cdn( env, :organisation => :microsoft )
62
108
  = yield
63
109
 
64
110
  @@mediatemple
65
111
  %html
66
112
  %head
67
- = Rack::JQuery.cdn( :media_temple )
113
+ = Rack::JQuery.cdn( env, :organisation => :media_temple )
68
114
  = yield
69
115
 
70
116
  @@cloudflare
71
117
  %html
72
118
  %head
73
- = Rack::JQuery.cdn( :cloudflare )
119
+ = Rack::JQuery.cdn( env, :organisation => :cloudflare )
74
120
  = yield
75
121
 
76
122
  @@unspecified
77
123
  %html
78
124
  %head
79
- = Rack::JQuery.cdn()
125
+ = Rack::JQuery.cdn(env)
80
126
  = yield
81
127
 
82
128
  @@index
data/examples/config.ru CHANGED
@@ -3,4 +3,9 @@ require "bundler"
3
3
  Bundler.setup(:examples)
4
4
  require File.expand_path( '../config.rb', __FILE__)
5
5
 
6
- run App
6
+
7
+ map "/defaults" do
8
+ run AppWithDefaults
9
+ end
10
+
11
+ run App
data/lib/rack/jquery.rb CHANGED
@@ -1,13 +1,17 @@
1
1
  require "rack/jquery/version"
2
2
  require "rack/jquery/helpers"
3
3
 
4
+ # @see http://rack.github.io/
4
5
  module Rack
5
6
 
6
7
  # jQuery CDN script tags and fallback in one neat package.
7
8
  class JQuery
8
9
  include Helpers
9
10
 
11
+ # Current file name of fallback.
10
12
  JQUERY_FILE_NAME = "jquery-#{JQUERY_VERSION}.min.js"
13
+
14
+ # Fallback source map file name.
11
15
  JQUERY_SOURCE_MAP = "jquery-#{JQUERY_VERSION}.min.map"
12
16
 
13
17
 
@@ -37,9 +41,18 @@ module Rack
37
41
  </script>
38
42
  STR
39
43
 
40
- # @param [Symbol] organisation Choose which CDN to use, either :google, :microsoft or :media_temple, or :cloudflare
44
+ # @param [Hash] env The rack env hash.
45
+ # @param [Symbol] organisation Choose which CDN to use, either :google, :microsoft or :media_temple, or :cloudflare. This will override anything set via the `use` statement.
41
46
  # @return [String] The HTML script tags to get the CDN.
42
- def self.cdn( organisation=:media_temple )
47
+ def self.cdn( env, options={} )
48
+ if env.nil? || env.has_key?(:organisation)
49
+ fail ArgumentError, "The Rack::JQuery.cdn method needs the Rack environment passed to it, or at the very least, an empty hash."
50
+ end
51
+
52
+ organisation = options[:organisation] ||
53
+ env["rack.jquery.organisation"] ||
54
+ :media_temple
55
+
43
56
  script = case organisation
44
57
  when :media_temple
45
58
  CDN::MEDIA_TEMPLE
@@ -65,15 +78,21 @@ STR
65
78
  # @param [#call] app
66
79
  # @param [Hash] options
67
80
  # @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".
81
+ # @option options [Symbol] :organisation see {Rack::JQuery.cdn}
68
82
  # @example
69
83
  # # The default:
70
84
  # use Rack::JQuery
85
+ #
71
86
  # # With a different route to the fallback:
72
87
  # use Rack::JQuery, :http_path => "/assets/js"
88
+ #
89
+ # # With a default organisation:
90
+ # use Rack::JQuery, :organisation => :cloudflare
73
91
  def initialize( app, options={} )
74
92
  @app, @options = app, DEFAULT_OPTIONS.merge(options)
75
93
  @http_path_to_jquery = ::File.join @options[:http_path], JQUERY_FILE_NAME
76
94
  @http_path_to_source_map = ::File.join @options[:http_path], JQUERY_SOURCE_MAP
95
+ @organisation = options.fetch :organisation, :media_temple
77
96
  end
78
97
 
79
98
 
@@ -87,6 +106,7 @@ STR
87
106
  # @param (see #call)
88
107
  def _call( env )
89
108
  request = Rack::Request.new(env.dup)
109
+ env.merge! "rack.jquery.organisation" => @organisation
90
110
  if request.path_info == @http_path_to_jquery
91
111
  response = Rack::Response.new
92
112
  # for caching
@@ -2,7 +2,7 @@ module Rack
2
2
  class JQuery
3
3
 
4
4
  # the version of this library
5
- VERSION = "1.5.1"
5
+ VERSION = "2.0.0"
6
6
 
7
7
  # the version of jQuery it supports.
8
8
  JQUERY_VERSION = "2.0.3"
@@ -4,8 +4,10 @@ require 'spec_helper'
4
4
  require_relative "../lib/rack/jquery.rb"
5
5
 
6
6
  describe "The class methods" do
7
- subject { Rack::JQuery.cdn organisation }
8
- context "Given an argument" do
7
+ let(:env) { {} }
8
+ subject { Rack::JQuery.cdn env, :organisation => organisation }
9
+
10
+ context "Given the organisation option" do
9
11
  context "of nil (the default)" do
10
12
  let(:organisation) { nil }
11
13
  it { should == "<script src='#{Rack::JQuery::CDN::MEDIA_TEMPLE}'></script>\n#{Rack::JQuery::FALLBACK}" }
@@ -27,60 +29,134 @@ describe "The class methods" do
27
29
  it { should == "<script src='#{Rack::JQuery::CDN::CLOUDFLARE}'></script>\n#{Rack::JQuery::FALLBACK}" }
28
30
  end
29
31
  end
32
+
33
+ context "Given no Rack env argument" do
34
+ it "should fail and give a message" do
35
+ expect{ Rack::JQuery.cdn nil }.to raise_error(ArgumentError)
36
+ end
37
+
38
+ context "and an organisation option" do
39
+ it "should fail and give a message" do
40
+ expect{ Rack::JQuery.cdn nil, {:organisation => :microsoft} }.to raise_error(ArgumentError)
41
+ end
42
+ end
43
+ end
30
44
  end
31
45
 
32
46
  describe "Inserting the CDN" do
33
- include_context "All routes"
34
- context "Check the examples run at all" do
35
- before do
36
- get "/"
47
+
48
+ # These check the default is overriden
49
+ # when `cdn` is given a value
50
+ # but when not, the default is used.
51
+ context "When given a default" do
52
+ include_context "All routes" do
53
+ let(:app){ AppWithDefaults }
37
54
  end
38
- it_should_behave_like "Any route"
39
- end
40
- context "Google CDN" do
41
- before do
42
- get "/google-cdn"
55
+ context "Check the examples run at all" do
56
+ before do
57
+ get "/"
58
+ end
59
+ it_should_behave_like "Any route"
43
60
  end
44
- it_should_behave_like "Any route"
45
- subject { last_response.body }
46
- let(:expected) { Rack::JQuery::CDN::GOOGLE }
47
- it { should include expected }
48
- end
49
- context "Microsoft CDN" do
50
- before do
51
- get "/microsoft-cdn"
61
+ context "Google CDN" do
62
+ before do
63
+ get "/google-cdn"
64
+ end
65
+ it_should_behave_like "Any route"
66
+ subject { last_response.body }
67
+ let(:expected) { Rack::JQuery::CDN::GOOGLE }
68
+ it { should include expected }
52
69
  end
53
- it_should_behave_like "Any route"
54
- subject { last_response.body }
55
- let(:expected) { Rack::JQuery::CDN::MICROSOFT }
56
- it { should include expected }
57
- end
58
- context "Media_temple CDN" do
59
- before do
60
- get "/media-temple-cdn"
70
+ context "Microsoft CDN" do
71
+ before do
72
+ get "/microsoft-cdn"
73
+ end
74
+ it_should_behave_like "Any route"
75
+ subject { last_response.body }
76
+ let(:expected) { Rack::JQuery::CDN::MICROSOFT }
77
+ it { should include expected }
61
78
  end
62
- it_should_behave_like "Any route"
63
- subject { last_response.body }
64
- let(:expected) { Rack::JQuery::CDN::MEDIA_TEMPLE }
65
- it { should include expected }
66
- end
67
- context "Unspecified CDN" do
68
- before do
69
- get "/unspecified-cdn"
79
+ context "Media_temple CDN" do
80
+ before do
81
+ get "/media-temple-cdn"
82
+ end
83
+ it_should_behave_like "Any route"
84
+ subject { last_response.body }
85
+ let(:expected) { Rack::JQuery::CDN::MEDIA_TEMPLE }
86
+ it { should include expected }
87
+ end
88
+ context "Unspecified CDN" do
89
+ before do
90
+ get "/unspecified-cdn"
91
+ end
92
+ it_should_behave_like "Any route"
93
+ subject { last_response.body }
94
+ let(:expected) { Rack::JQuery::CDN::CLOUDFLARE }
95
+ it { should include expected }
96
+ end
97
+ context "Cloudflare CDN" do
98
+ before do
99
+ get "/cloudflare-cdn"
100
+ end
101
+ it_should_behave_like "Any route"
102
+ subject { last_response.body }
103
+ let(:expected) { Rack::JQuery::CDN::CLOUDFLARE }
104
+ it { should include expected }
70
105
  end
71
- it_should_behave_like "Any route"
72
- subject { last_response.body }
73
- let(:expected) { Rack::JQuery::CDN::MEDIA_TEMPLE }
74
- it { should include expected }
75
106
  end
76
- context "Cloudflare CDN" do
77
- before do
78
- get "/cloudflare-cdn"
107
+ context "When not given a default" do
108
+ include_context "All routes"
109
+ context "Check the examples run at all" do
110
+ before do
111
+ get "/"
112
+ end
113
+ it_should_behave_like "Any route"
114
+ end
115
+ context "Google CDN" do
116
+ before do
117
+ get "/google-cdn"
118
+ end
119
+ it_should_behave_like "Any route"
120
+ subject { last_response.body }
121
+ let(:expected) { Rack::JQuery::CDN::GOOGLE }
122
+ it { should include expected }
123
+ end
124
+ context "Microsoft CDN" do
125
+ before do
126
+ get "/microsoft-cdn"
127
+ end
128
+ it_should_behave_like "Any route"
129
+ subject { last_response.body }
130
+ let(:expected) { Rack::JQuery::CDN::MICROSOFT }
131
+ it { should include expected }
132
+ end
133
+ context "Media_temple CDN" do
134
+ before do
135
+ get "/media-temple-cdn"
136
+ end
137
+ it_should_behave_like "Any route"
138
+ subject { last_response.body }
139
+ let(:expected) { Rack::JQuery::CDN::MEDIA_TEMPLE }
140
+ it { should include expected }
141
+ end
142
+ context "Unspecified CDN" do
143
+ before do
144
+ get "/unspecified-cdn"
145
+ end
146
+ it_should_behave_like "Any route"
147
+ subject { last_response.body }
148
+ let(:expected) { Rack::JQuery::CDN::MEDIA_TEMPLE }
149
+ it { should include expected }
150
+ end
151
+ context "Cloudflare CDN" do
152
+ before do
153
+ get "/cloudflare-cdn"
154
+ end
155
+ it_should_behave_like "Any route"
156
+ subject { last_response.body }
157
+ let(:expected) { Rack::JQuery::CDN::CLOUDFLARE }
158
+ it { should include expected }
79
159
  end
80
- it_should_behave_like "Any route"
81
- subject { last_response.body }
82
- let(:expected) { Rack::JQuery::CDN::CLOUDFLARE }
83
- it { should include expected }
84
160
  end
85
161
  end
86
162
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-jquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-06 00:00:00.000000000 Z
12
+ date: 2013-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler