rack-backbone 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # CH CH CH CHANGES #
2
2
 
3
+
4
+ ## Thursday the 10th of October 2013 ##
5
+
6
+ ### v0.0.5 ###
7
+
8
+ * Can pass `false` to the `cdn` method to just get the fallback with no CDN, useful for working locally.
9
+ * Fixed problem with the :http_path option for the fallback route.
10
+
11
+ ----
12
+
13
+
3
14
  ## Thursday the 19th of September 2013 ##
4
15
 
5
16
  ### v0.0.4 ###
@@ -8,6 +19,7 @@
8
19
 
9
20
  ----
10
21
 
22
+
11
23
  ### v0.0.3 ###
12
24
 
13
25
  * The vendored scripts weren't in the gem because of a cheeky line in the .gitignore file. Maybe DHH got hold of it ;) Fixed now.
data/Rakefile CHANGED
@@ -12,7 +12,7 @@ namespace :docs do
12
12
  require 'yard'
13
13
 
14
14
  YARD::Rake::YardocTask.new :yard do |t|
15
- t.files = ['lib/**/*.rb', 'app/*.rb', 'spec/**/*.rb']
15
+ t.files = ['lib/**/*.rb', 'app/*.rb']
16
16
  t.options = ['-odocs/'] # optional
17
17
  end
18
18
 
data/lib/rack/backbone.rb CHANGED
@@ -31,43 +31,60 @@ module Rack
31
31
 
32
32
  end
33
33
 
34
+
35
+ # Default options hash for the middleware.
36
+ DEFAULT_OPTIONS = {
37
+ :http_path => "/js"
38
+ }
39
+
40
+
34
41
  # This javascript checks if the Backbone object has loaded. If not, that most likely means the CDN is unreachable, so it uses the local minified Backbone.
35
- FALLBACK = <<STR
42
+ # It's the top half, it gets pieced together elsewhere.
43
+ FALLBACK_TOP = <<STR
36
44
  <script type="text/javascript">
37
45
  if (typeof Backbone == 'undefined') {
38
- document.write(unescape("%3Cscript src='/js/#{BACKBONE_FILE_NAME}' type='text/javascript'%3E%3C/script%3E"))
46
+ document.write(unescape("%3Cscript src='
47
+ STR
48
+
49
+ # Bottom half of the fallback script.
50
+ FALLBACK_BOTTOM = <<STR
51
+ ' type='text/javascript'%3E%3C/script%3E"))
39
52
  };
40
53
  </script>
41
54
  STR
42
55
 
43
56
  # @param [Hash] env The rack env hash.
44
- # @param [Symbol] organisation Choose which CDN to use, either :jsdelivr, or :cloudflare (the default). This will override anything set via the `use` statement.
57
+ # @option options [Symbol] organisation Choose which CDN to use, either :jsdelivr, or :cloudflare (the default). This will override anything set via the `use` statement. Pass in `false` to force use of the local Backbonejs script. `nil` will force choosing the default CDN.
45
58
  # @return [String] The HTML script tags to get the CDN.
46
59
  def self.cdn( env, options={} )
47
60
  if env.nil? || env.has_key?(:organisation)
48
61
  fail ArgumentError, "The Rack::Backbone.cdn method needs the Rack environment passed to it, or at the very least, an empty hash."
49
62
  end
50
63
 
51
- organisation = options[:organisation] ||
52
- env["rack.backbone.organisation"] ||
53
- :media_temple
64
+ organisation = options[:organisation]
65
+ if organisation.nil?
66
+ organisation =
67
+ env["rack.backbone.organisation"] ||
68
+ :media_temple
69
+ end
54
70
 
55
- script = case organisation
56
- when :cloudflare
57
- CDN::CLOUDFLARE
58
- when :jsdelivr
59
- CDN::JSDELIVR
60
- else
61
- CDN::CLOUDFLARE
71
+ warn "organisation = #{organisation.inspect}"
72
+ unless organisation == false
73
+ script_src = case organisation
74
+ when :cloudflare
75
+ CDN::CLOUDFLARE
76
+ when :jsdelivr
77
+ CDN::JSDELIVR
78
+ else
79
+ CDN::CLOUDFLARE
80
+ end
81
+ %Q!<script src='#{script_src}'></script>\n#{FALLBACK_TOP}#{env["rack.backbone.http_path"]}#{FALLBACK_BOTTOM}!
82
+ else
83
+ "<script src='#{env["rack.backbone.http_path"]}'></script>"
62
84
  end
63
- "<script src='#{script}'></script>\n#{FALLBACK}"
64
- end
65
85
 
86
+ end
66
87
 
67
- # Default options hash for the middleware.
68
- DEFAULT_OPTIONS = {
69
- :http_path => "/js"
70
- }
71
88
 
72
89
 
73
90
  # @param [#call] app
@@ -78,9 +95,6 @@ STR
78
95
  # # The default:
79
96
  # use Rack::Backbone
80
97
  #
81
- # # With a different route to the fallback:
82
- # use Rack::Backbone, :http_path => "/assets/js"
83
- #
84
98
  # # With a default organisation:
85
99
  # use Rack::Backbone, :organisation => :cloudflare
86
100
  def initialize( app, options={} )
@@ -102,6 +116,7 @@ STR
102
116
  def _call( env )
103
117
  request = Rack::Request.new(env.dup)
104
118
  env.merge! "rack.backbone.organisation" => @organisation
119
+ env.merge! "rack.backbone.http_path" => @http_path_to_backbone
105
120
  if request.path_info == @http_path_to_backbone
106
121
  response = Rack::Response.new
107
122
  # for caching
@@ -2,7 +2,7 @@ module Rack
2
2
  class Backbone
3
3
 
4
4
  # the version of this library
5
- VERSION = "0.0.4"
5
+ VERSION = "0.0.5"
6
6
 
7
7
  # the version of Backbone it supports.
8
8
  BACKBONE_VERSION = "1.0.0"
@@ -3,22 +3,30 @@
3
3
  require 'spec_helper'
4
4
  require_relative "../lib/rack/backbone.rb"
5
5
 
6
+ class Rack::Backbone # for clarity!
7
+
6
8
  describe "The class methods" do
7
- let(:env) { {} }
8
- subject { Rack::Backbone.cdn env, :organisation => organisation }
9
+ let(:path) {::File.join(DEFAULT_OPTIONS[:http_path],BACKBONE_FILE_NAME)}
10
+ let(:env) { {"rack.backbone.http_path" => path} }
11
+ let(:default_options) { {} }
12
+ subject(:cdn) { Rack::Backbone.cdn env, default_options.merge(options) }
9
13
 
10
14
  context "Given the organisation option" do
11
15
  context "of nil (the default)" do
12
- let(:organisation) { nil }
13
- it { should == "<script src='#{Rack::Backbone::CDN::CLOUDFLARE}'></script>\n#{Rack::Backbone::FALLBACK}" }
16
+ let(:options) { {:organisation => nil } }
17
+ it { should == "<script src='#{CDN::CLOUDFLARE}'></script>\n#{FALLBACK_TOP}#{path}#{FALLBACK_BOTTOM}" }
14
18
  end
15
19
  context "of :jsdelivr" do
16
- let(:organisation) { :jsdelivr }
17
- it { should == "<script src='#{Rack::Backbone::CDN::JSDELIVR}'></script>\n#{Rack::Backbone::FALLBACK}" }
20
+ let(:options) { {:organisation => :jsdelivr } }
21
+ it { should == "<script src='#{CDN::JSDELIVR}'></script>\n#{FALLBACK_TOP}#{path}#{FALLBACK_BOTTOM}" }
18
22
  end
19
23
  context "of :cloudflare" do
20
- let(:organisation) { :cloudflare }
21
- it { should == "<script src='#{Rack::Backbone::CDN::CLOUDFLARE}'></script>\n#{Rack::Backbone::FALLBACK}" }
24
+ let(:options) { {:organisation => :cloudflare } }
25
+ it { should == "<script src='#{CDN::CLOUDFLARE}'></script>\n#{FALLBACK_TOP}#{path}#{FALLBACK_BOTTOM}" }
26
+ end
27
+ context "of false, to get the fallback script only" do
28
+ let(:options) { {:organisation => false } }
29
+ it { should == "<script src='#{path}'></script>" }
22
30
  end
23
31
  end
24
32
 
@@ -56,7 +64,7 @@ describe "Inserting the CDN" do
56
64
  end
57
65
  it_should_behave_like "Any route"
58
66
  subject { last_response.body }
59
- let(:expected) { Rack::Backbone::CDN::JSDELIVR }
67
+ let(:expected) { CDN::JSDELIVR }
60
68
  it { should include expected }
61
69
  end
62
70
  context "Unspecified CDN" do
@@ -65,7 +73,7 @@ describe "Inserting the CDN" do
65
73
  end
66
74
  it_should_behave_like "Any route"
67
75
  subject { last_response.body }
68
- let(:expected) { Rack::Backbone::CDN::CLOUDFLARE }
76
+ let(:expected) { CDN::CLOUDFLARE }
69
77
  it { should include expected }
70
78
  end
71
79
  context "Cloudflare CDN" do
@@ -74,7 +82,7 @@ describe "Inserting the CDN" do
74
82
  end
75
83
  it_should_behave_like "Any route"
76
84
  subject { last_response.body }
77
- let(:expected) { Rack::Backbone::CDN::CLOUDFLARE }
85
+ let(:expected) { CDN::CLOUDFLARE }
78
86
  it { should include expected }
79
87
  end
80
88
  end
@@ -92,7 +100,7 @@ describe "Inserting the CDN" do
92
100
  end
93
101
  it_should_behave_like "Any route"
94
102
  subject { last_response.body }
95
- let(:expected) { Rack::Backbone::CDN::JSDELIVR }
103
+ let(:expected) { CDN::JSDELIVR }
96
104
  it { should include expected }
97
105
  end
98
106
  context "Unspecified CDN" do
@@ -101,7 +109,7 @@ describe "Inserting the CDN" do
101
109
  end
102
110
  it_should_behave_like "Any route"
103
111
  subject { last_response.body }
104
- let(:expected) { Rack::Backbone::CDN::CLOUDFLARE }
112
+ let(:expected) { CDN::CLOUDFLARE }
105
113
  it { should include expected }
106
114
  end
107
115
  context "Cloudflare CDN" do
@@ -110,7 +118,7 @@ describe "Inserting the CDN" do
110
118
  end
111
119
  it_should_behave_like "Any route"
112
120
  subject { last_response.body }
113
- let(:expected) { Rack::Backbone::CDN::CLOUDFLARE }
121
+ let(:expected) { CDN::CLOUDFLARE }
114
122
  it { should include expected }
115
123
  end
116
124
  end
@@ -121,24 +129,53 @@ require 'timecop'
121
129
  require 'time'
122
130
 
123
131
  describe "Serving the fallback backbone" do
124
- include_context "All routes"
125
132
  before do
126
- get "/js/backbone-#{Rack::Backbone::BACKBONE_VERSION}-min.js"
133
+ get path
127
134
  end
128
- it_should_behave_like "Any route"
129
135
  subject { last_response.body }
130
- it { should start_with "(function(){var t=this;var e=t.Backbone;" }
136
+ let(:path){ ::File.join(http_path, BACKBONE_FILE_NAME) }
131
137
 
132
- context "Re requests" do
133
- before do
134
- at_start = Time.parse(Rack::Backbone::BACKBONE_VERSION_DATE) + 60 * 60 * 24 * 180
135
- Timecop.freeze at_start
136
- get "/js/backbone-#{Rack::Backbone::BACKBONE_VERSION}-min.js"
137
- Timecop.travel Time.now + 86400 # add a day
138
- get "/js/backbone-#{Rack::Backbone::BACKBONE_VERSION}-min.js", {}, {"HTTP_IF_MODIFIED_SINCE" => Rack::Utils.rfc2109(at_start) }
139
- end
140
- subject { last_response }
141
- its(:status) { should == 304 }
142
-
138
+ context "With the default :http_path (none given)" do
139
+ include_context "All routes"
140
+ let(:http_path) { DEFAULT_OPTIONS[:http_path] }
141
+
142
+ it_should_behave_like "Any route"
143
+ it { should start_with "(function(){var t=this;var e=t.Backbone;" }
144
+
145
+ context "Re requests" do
146
+ before do
147
+ at_start = Time.parse(BACKBONE_VERSION_DATE) + 60 * 60 * 24 * 180
148
+ Timecop.freeze at_start
149
+ get path
150
+ Timecop.travel Time.now + 86400 # add a day
151
+ get path, {}, {"HTTP_IF_MODIFIED_SINCE" => Rack::Utils.rfc2109(at_start) }
152
+ end
153
+ subject { last_response }
154
+ its(:status) { should == 304 }
155
+ end
143
156
  end
157
+ context "Given a different http_path via the options" do
158
+ include_context "All routes" do
159
+ let(:app) {
160
+ Sinatra.new do
161
+ use Rack::JQuery
162
+ use Rack::Lodash
163
+ use Rack::Backbone, :http_path => "/assets/javascripts"
164
+ end
165
+ }
166
+ end
167
+ context "That is valid" do
168
+ let(:http_path) { "/assets/javascripts" }
169
+
170
+ it_should_behave_like "Any route"
171
+ it { should start_with "(function(){var t=this;var e=t.Backbone;" }
172
+ end
173
+ context "That is not valid" do
174
+ let(:http_path) { "/this/is/not/the/path/it/was/setup/with" }
175
+ subject { last_response }
176
+ it { should_not be_ok }
177
+ end
178
+ end
179
+ end
180
+
144
181
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-backbone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-19 00:00:00.000000000 Z
12
+ date: 2013-10-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler