fewer 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,12 +2,35 @@
2
2
 
3
3
  Rack middleware to bundle assets and help you make fewer HTTP requests.
4
4
 
5
- ## How to use in Rails (3)
5
+ ## How to use as a Rack app (config.ru example)
6
6
 
7
- config.middleware.insert 0, Fewer::App, {
8
- :engine => Fewer::Engines::Css,
7
+ app = Rack::Builder.new do
8
+ map '/stylesheets' do
9
+ run Fewer::App,
10
+ :root => File.dirname(__FILE__)+'/less_css',
11
+ :engine => Fewer::Engines::Less
12
+ end
13
+
14
+ map '/' do
15
+ run MyApp
16
+ end
17
+ end
18
+
19
+ run app
20
+
21
+ ## How to use in Rails 3 router
22
+
23
+ match '/stylesheets/:data.css', :to => Fewer::App.new(
24
+ :engine => Fewer::Engines::Less,
25
+ :root => Rails.root.join('app', 'stylesheets')
26
+ )
27
+
28
+ ## How to use in Rails as middleware
29
+
30
+ config.middleware.use Fewer::MiddleWare, {
31
+ :engine => Fewer::Engines::Less,
9
32
  :mount => '/stylesheets',
10
- :root => Rails.root.join('public', 'stylesheets')
33
+ :root => Rails.root.join('app', 'stylesheets')
11
34
  }
12
35
 
13
36
  ## Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
data/fewer.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fewer}
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Pickles"]
@@ -32,9 +32,11 @@ Gem::Specification.new do |s|
32
32
  "lib/fewer/engines/js_min.rb",
33
33
  "lib/fewer/engines/less.rb",
34
34
  "lib/fewer/errors.rb",
35
+ "lib/fewer/middleware.rb",
35
36
  "lib/fewer/serializer.rb",
36
37
  "test/app_test.rb",
37
38
  "test/engine_test.rb",
39
+ "test/middleware_test.rb",
38
40
  "test/test_helper.rb"
39
41
  ]
40
42
  s.homepage = %q{http://github.com/benpickles/fewer}
@@ -45,7 +47,7 @@ Gem::Specification.new do |s|
45
47
  s.test_files = [
46
48
  "test/app_test.rb",
47
49
  "test/engine_test.rb",
48
- "test/sass_engine_test.rb",
50
+ "test/middleware_test.rb",
49
51
  "test/test_helper.rb"
50
52
  ]
51
53
 
data/lib/fewer.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'fewer/app'
2
2
  require 'fewer/engines'
3
3
  require 'fewer/errors'
4
+ require 'fewer/middleware'
4
5
  require 'fewer/serializer'
data/lib/fewer/app.rb CHANGED
@@ -1,18 +1,17 @@
1
1
  module Fewer
2
2
  class App
3
- attr_reader :app, :engine_klass, :cache, :mount, :root
3
+ attr_reader :engine_klass, :cache, :root
4
4
 
5
- def initialize(app, options = {})
6
- @app = app
5
+ def initialize(options = {})
7
6
  @engine_klass = options[:engine]
8
7
  @mount = options[:mount]
9
8
  @root = options[:root]
10
9
  @cache = options[:cache] || 3600 * 24 * 365
10
+ raise 'You need to define an :engine class' unless @engine_klass
11
+ raise 'You need to define a :root path' unless @root
11
12
  end
12
13
 
13
14
  def call(env)
14
- return app.call(env) unless env['PATH_INFO'] =~ /^#{mount}/
15
-
16
15
  names = names_from_path(env['PATH_INFO'])
17
16
  engine = engine_klass.new(root, names)
18
17
  headers = {
@@ -29,7 +28,7 @@ module Fewer
29
28
 
30
29
  private
31
30
  def names_from_path(path)
32
- encoded = File.basename(path.sub(/^#{mount}\/?/, ''), '.*')
31
+ encoded = File.basename(path, '.*')
33
32
  Serializer.decode(encoded)
34
33
  end
35
34
  end
@@ -0,0 +1,18 @@
1
+ module Fewer
2
+ class MiddleWare
3
+ def initialize(app, options = {})
4
+ @options = options
5
+ @app = app
6
+ @mount = @options[:mount]
7
+ end
8
+
9
+ def call(env)
10
+ if env['PATH_INFO'] =~ /^#{@mount}/
11
+ App.new(@options).call(env)
12
+ else
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
data/test/app_test.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class AppTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
4
6
  def setup
5
7
  @app = stub
6
8
  @engine = stub(
@@ -9,9 +11,8 @@ class AppTest < Test::Unit::TestCase
9
11
  :read => 'content'
10
12
  )
11
13
  @engine_klass = stub(:new => @engine)
12
- @fewer_app = Fewer::App.new(@app, {
14
+ @fewer_app = Fewer::App.new({
13
15
  :engine => @engine_klass,
14
- :mount => '/path',
15
16
  :root => 'root'
16
17
  })
17
18
  @browser = Rack::Test::Session.new(Rack::MockSession.new(@fewer_app))
@@ -61,13 +62,4 @@ class AppTest < Test::Unit::TestCase
61
62
  @browser.get "/path/#{encode('file')}.css"
62
63
  assert_equal 500, @browser.last_response.status
63
64
  end
64
-
65
- private
66
- def decode(string)
67
- Fewer::Serializer.decode(string)
68
- end
69
-
70
- def encode(obj)
71
- Fewer::Serializer.encode(obj)
72
- end
73
65
  end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class MiddlewareTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def setup
7
+ @app = stub
8
+ @engine = stub(
9
+ :check_request_extension => true,
10
+ :content_type => 'text/css',
11
+ :read => 'content'
12
+ )
13
+ Fewer::Engines::Css.stubs(:new).returns(@engine)
14
+
15
+ @app = Rack::Builder.new do
16
+ map '/' do
17
+ use Fewer::MiddleWare,
18
+ :root => '/some/root/path',
19
+ :engine => Fewer::Engines::Css,
20
+ :mount => '/css'
21
+ run lambda{|env| [200, {'Content-Type'=>'text/html'},'Hello World']}
22
+ end
23
+ end
24
+
25
+ @browser = Rack::Test::Session.new(Rack::MockSession.new(@app))
26
+ end
27
+
28
+ def test_middleware_intercepts_path
29
+ @browser.get "/css/#{encode('file')}.css"
30
+ assert_equal 'content', @browser.last_response.body
31
+ assert_equal 'text/css', @browser.last_response.content_type
32
+ end
33
+
34
+ def test_middleware_delegates_to_app
35
+ @browser.get "/blah/foobar.css"
36
+ assert_equal 'Hello World', @browser.last_response.body
37
+ assert_equal 'text/html', @browser.last_response.content_type
38
+ end
39
+ end
data/test/test_helper.rb CHANGED
@@ -11,3 +11,14 @@ rescue LoadError
11
11
  end
12
12
 
13
13
  require 'fewer'
14
+
15
+ module TestHelper
16
+ private
17
+ def decode(string)
18
+ Fewer::Serializer.decode(string)
19
+ end
20
+
21
+ def encode(obj)
22
+ Fewer::Serializer.encode(obj)
23
+ end
24
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fewer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Pickles
@@ -57,11 +57,12 @@ files:
57
57
  - lib/fewer/engines/js_min.rb
58
58
  - lib/fewer/engines/less.rb
59
59
  - lib/fewer/errors.rb
60
+ - lib/fewer/middleware.rb
60
61
  - lib/fewer/serializer.rb
61
62
  - test/app_test.rb
62
63
  - test/engine_test.rb
64
+ - test/middleware_test.rb
63
65
  - test/test_helper.rb
64
- - test/sass_engine_test.rb
65
66
  has_rdoc: true
66
67
  homepage: http://github.com/benpickles/fewer
67
68
  licenses: []
@@ -99,5 +100,5 @@ summary: Rack middleware to bundle assets and help you make fewer HTTP requests.
99
100
  test_files:
100
101
  - test/app_test.rb
101
102
  - test/engine_test.rb
102
- - test/sass_engine_test.rb
103
+ - test/middleware_test.rb
103
104
  - test/test_helper.rb
@@ -1,22 +0,0 @@
1
- require 'test_helper'
2
-
3
- class EnginesTest < Test::Unit::TestCase
4
- def test_sass_engine
5
- engine = Fewer::Engines::Sass.new(template_root, 'sass')
6
- assert_equal sass, engine.read
7
- end
8
-
9
- private
10
- def sass
11
- '#navbar li {
12
- border-top-radius: 10px;
13
- -moz-border-radius-top: 10px;
14
- -webkit-border-top-radius: 10px; }
15
-
16
- #footer {
17
- border-top-radius: 10px;
18
- -moz-border-radius-top: 10px;
19
- -webkit-border-top-radius: 10px; }
20
- '
21
- end
22
- end