nancy 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -6,7 +6,7 @@ _Sinatra's little daughter_
6
6
 
7
7
  ## Description
8
8
 
9
- Minimal Ruby microframework for web development inspired in [Sinatra](http://www.sinatrarb.com/)
9
+ Minimal Ruby microframework for web development inspired in [Sinatra](http://www.sinatrarb.com/) and [Cuba](https://github.com/soveran/cuba)
10
10
 
11
11
 
12
12
  ## Installation
@@ -66,6 +66,14 @@ class Hello < Nancy::Base
66
66
  halt 404 unless @user
67
67
  UserSerializer.new(@user).to_json
68
68
  end
69
+
70
+ map "/resque" do
71
+ run Resque::Server
72
+ end
73
+
74
+ map "/nancy" do
75
+ run AnotherNancyApp
76
+ end
69
77
  end
70
78
  ```
71
79
 
@@ -75,7 +83,7 @@ To run it, you can create a `config.ru` file:
75
83
  # config.ru
76
84
  require "./hello"
77
85
 
78
- run App.new
86
+ run Hello
79
87
  ```
80
88
 
81
89
  You can now run `rackup` and enjoy what you have just created.
@@ -85,9 +93,10 @@ Check examples folder for a detailed example.
85
93
 
86
94
  ## Features
87
95
 
88
- * "Sinatra-like" routes: support for get, post, put, patch, delete
96
+ * "Sinatra-like" routes: support for get, post, put, patch, delete, options, head
89
97
  * Template rendering and caching through Tilt
90
98
  * Include middlewares with the use method
99
+ * Mount rack apps with the map method
91
100
  * Sessions through Rack::Session
92
101
  * Halt execution at any point using Ruby's throw/catch mechanism
93
102
  * Thread-safe
@@ -95,7 +104,15 @@ Check examples folder for a detailed example.
95
104
 
96
105
  ## Version history
97
106
 
98
- ### 0.1.0
107
+ ### 0.2.0 (April 12, 2012)
108
+
109
+ * Set PATH INFO to '/' when is blank
110
+ * Fixed session method: Raise error when is used but Rack::Session isn't present
111
+ * Added support for HEAD and OPTIONS HTTP verbs
112
+ * Refactored Base.use to use a Rack::Builder internally
113
+ * Added Base.map to redirect requests to Rack sub-apps
114
+
115
+ ### 0.1.0 (April 4, 2012)
99
116
 
100
117
  * Created a new [Github Page](http://guilleiguaran.github.com/nancy) for the project
101
118
  * Added env accessor, this add support for [Shield](https://github.com/cyx/shield)
@@ -104,7 +121,7 @@ Check examples folder for a detailed example.
104
121
  * Refactored Nancy::Base to evaluate code blocks at instance level
105
122
  * Fixed passing of render options to Tilt (thanks to [lporras](https://github.com/lporras))
106
123
 
107
- ### 0.0.1
124
+ ### 0.0.1 (March 20, 2012)
108
125
 
109
126
  * Initial Release
110
127
 
data/Rakefile CHANGED
@@ -1,7 +1,15 @@
1
1
  #!/usr/bin/env rake
2
+ require 'rake/testtask'
2
3
 
3
4
  task :test do
4
- Dir.chdir('test'){ ruby('base_test.rb') }
5
+ Dir.chdir('test')
6
+ end
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << '../lib'
10
+ t.libs << '../test'
11
+ t.test_files = FileList['*_test.rb']
12
+ t.verbose = false
5
13
  end
6
14
 
7
15
  task :default => :test
@@ -3,10 +3,15 @@ $:.push File.expand_path("../../lib", __FILE__)
3
3
  require "nancy/base"
4
4
  require "nancy/render"
5
5
 
6
+ class MiniApp < Nancy::Base
7
+ get("/") { "Hello from MiniApp" }
8
+ get("/:something") { params['something'] }
9
+ end
10
+
6
11
  class App < Nancy::Base
7
12
  use Rack::Runtime
8
13
  use Rack::Session::Cookie
9
- use Rack::Static, :urls => ["/javascripts"], :root => "public"
14
+ use Rack::Static, urls: ["/js"], root: "public"
10
15
  include Nancy::Render
11
16
 
12
17
  get "/" do
@@ -58,6 +63,14 @@ class App < Nancy::Base
58
63
  halt 500, "Error fatal"
59
64
  end
60
65
 
66
+ map "/rack" do
67
+ run lambda{|env| [200, {"Content-Type" => "text/html"}, ["PATH_INFO: #{env["PATH_INFO"]}"]]}
68
+ end
69
+
70
+ map "/nancy" do
71
+ run MiniApp
72
+ end
73
+
61
74
  # Helper method
62
75
  def render(*args)
63
76
  args[0] = "views/#{args[0]}"
@@ -1,3 +1,2 @@
1
1
  require "./app"
2
-
3
- run App.new
2
+ run App
@@ -3,7 +3,7 @@ require 'rack'
3
3
  module Nancy
4
4
  class Base
5
5
  class << self
6
- %w(GET POST PATCH PUT DELETE).each do |verb|
6
+ %w(GET POST PATCH PUT DELETE HEAD OPTIONS).each do |verb|
7
7
  define_method(verb.downcase) do |pattern, &block|
8
8
  route_set[verb] << [compile(pattern), block]
9
9
  end
@@ -28,32 +28,32 @@ module Nancy
28
28
  end
29
29
 
30
30
  def session
31
- request.env["rack.session"]
31
+ request.env["rack.session"] || raise("Rack::Session handler is missing")
32
32
  end
33
33
 
34
34
  def redirect(uri)
35
35
  halt 302, {"Location" => uri}
36
36
  end
37
37
 
38
- def self.use(middleware, *args, &block)
39
- middlewares << [middleware, *args, block]
38
+ def self.use(*args, &block)
39
+ @builder.use(*args, &block)
40
40
  end
41
41
 
42
- def self.middlewares
43
- @middlewares ||= []
42
+ def self.map(*args, &block)
43
+ @builder.map(*args, &block)
44
44
  end
45
45
 
46
- def initialize
47
- klass = self.class
48
- @app = Rack::Builder.new do
49
- klass.middlewares.each do |middleware|
50
- middleware, *args, block = middleware
51
- use(middleware, *args, &block)
52
- end
53
- run self
46
+ def self.inherited(child)
47
+ child.instance_eval do
48
+ @builder = Rack::Builder.new
49
+ @builder.run(child.new)
54
50
  end
55
51
  end
56
52
 
53
+ def self.call(env)
54
+ @builder.dup.call(env)
55
+ end
56
+
57
57
  def call(env)
58
58
  Thread.current[:request] = Rack::Request.new(env)
59
59
  Thread.current[:response] = Rack::Response.new
@@ -65,6 +65,7 @@ module Nancy
65
65
  end
66
66
 
67
67
  def route_eval(request_method, path_info)
68
+ path_info = "/" if path_info == ""
68
69
  self.class.route_set[request_method].each do |matcher, block|
69
70
  if match = path_info.match(matcher[0])
70
71
  if (captures = match.captures) && !captures.empty?
@@ -1,3 +1,3 @@
1
1
  module Nancy
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,11 +1,10 @@
1
1
  require File.expand_path('../test_helper', __FILE__)
2
- require "minitest/autorun"
3
2
 
4
3
  class BaseTest < MiniTest::Unit::TestCase
5
4
  include Rack::Test::Methods
6
5
 
7
6
  class TestApp < Nancy::Base
8
- include Nancy::Render
7
+ use Rack::Session::Cookie
9
8
 
10
9
  get "/" do
11
10
  "Hello World"
@@ -36,32 +35,19 @@ class BaseTest < MiniTest::Unit::TestCase
36
35
  "not reached code"
37
36
  end
38
37
 
39
- get "/view" do
40
- @message = "Hello from view"
41
- render("#{view_path}/view.erb")
42
- end
43
-
44
- get "/layout" do
45
- @message = "Hello from view"
46
- render("#{view_path}/layout.erb") { render("#{view_path}/view.erb") }
47
- end
48
-
49
- get "/view_with_option_trim" do
50
- render("#{view_path}/view_with_trim.erb", {}, :trim => "%")
51
- end
52
-
53
- def view_path
54
- File.expand_path("fixtures", Dir.pwd)
38
+ get "/session" do
39
+ session['test'] = "test"
40
+ "session['test'] content is: #{session['test']}"
55
41
  end
56
42
  end
57
43
 
58
44
  def app
59
- TestApp.new
45
+ TestApp
60
46
  end
61
47
 
62
48
  def test_app_respond_with_call
63
49
  assert TestApp.new.respond_to?(:call)
64
- request = Rack::MockRequest.new(TestApp.new)
50
+ request = Rack::MockRequest.new(TestApp)
65
51
  response = request.get('/')
66
52
  assert_equal 200, response.status
67
53
  assert_equal 'Hello World', response.body
@@ -94,20 +80,8 @@ class BaseTest < MiniTest::Unit::TestCase
94
80
  assert_equal 'Internal Error', last_response.body
95
81
  end
96
82
 
97
- def test_render
98
- get '/view'
99
- assert !last_response.body.include?("<html>")
100
- assert last_response.body.include?("Hello from view")
101
- end
102
-
103
- def test_render_with_layout
104
- get '/layout'
105
- assert last_response.body.include?("<html>")
106
- assert last_response.body.include?("Hello from view")
107
- end
108
-
109
- def test_send_tilt_options_to_render
110
- get '/view_with_option_trim'
111
- assert_equal "\nhello\n", last_response.body
83
+ def test_session
84
+ get '/session'
85
+ assert_equal "session['test'] content is: test", last_response.body
112
86
  end
113
87
  end
@@ -1,4 +1,3 @@
1
-
2
- % if true
1
+ <% if true %>
3
2
  hello
4
- %end
3
+ <% end %>
@@ -0,0 +1,57 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class RackApp
4
+ def call(env)
5
+ [200, {'Content-Type' => 'text/html'}, ["Hello from RackApp"]]
6
+ end
7
+ end
8
+
9
+ class NancyApp < Nancy::Base
10
+ get "/" do
11
+ "Hello from NancyApp"
12
+ end
13
+ end
14
+
15
+ class MainApp < Nancy::Base
16
+ get "/" do
17
+ "Hello from MainApp"
18
+ end
19
+
20
+ map "/rack_app" do
21
+ run RackApp.new
22
+ end
23
+
24
+ map "/nancy_app" do
25
+ run NancyApp
26
+ end
27
+
28
+ map "/lambda_app" do
29
+ run lambda{|env| [200, {'Content-Type' => 'text/html'}, ["Hello from LambdaApp"]]}
30
+ end
31
+ end
32
+
33
+ class UseTest < MiniTest::Unit::TestCase
34
+ def setup
35
+ @request = Rack::MockRequest.new(MainApp)
36
+ end
37
+
38
+ def test_main_app
39
+ response = @request.get('/')
40
+ assert_equal "Hello from MainApp", response.body
41
+ end
42
+
43
+ def test_map_with_rack_app
44
+ response = @request.get('/rack_app')
45
+ assert_equal "Hello from RackApp", response.body
46
+ end
47
+
48
+ def test_map_with_nancy_app
49
+ response = @request.get('/nancy_app')
50
+ assert_equal "Hello from NancyApp", response.body
51
+ end
52
+
53
+ def test_map_with_lambda
54
+ response = @request.get('/lambda_app')
55
+ assert_equal "Hello from LambdaApp", response.body
56
+ end
57
+ end
@@ -0,0 +1,48 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class BaseTest < MiniTest::Unit::TestCase
4
+ include Rack::Test::Methods
5
+
6
+ class TestApp < Nancy::Base
7
+ include Nancy::Render
8
+
9
+ get "/view" do
10
+ @message = "Hello from view"
11
+ render("#{view_path}/view.erb")
12
+ end
13
+
14
+ get "/layout" do
15
+ @message = "Hello from view"
16
+ render("#{view_path}/layout.erb") { render("#{view_path}/view.erb") }
17
+ end
18
+
19
+ get "/view_with_option_trim" do
20
+ render("#{view_path}/view_with_trim.erb", {}, :trim => true)
21
+ end
22
+
23
+ def view_path
24
+ File.expand_path("fixtures", Dir.pwd)
25
+ end
26
+ end
27
+
28
+ def app
29
+ TestApp
30
+ end
31
+
32
+ def test_render
33
+ get '/view'
34
+ assert !last_response.body.include?("<html>")
35
+ assert last_response.body.include?("Hello from view")
36
+ end
37
+
38
+ def test_render_with_layout
39
+ get '/layout'
40
+ assert last_response.body.include?("<html>")
41
+ assert last_response.body.include?("Hello from view")
42
+ end
43
+
44
+ def test_send_tilt_options_to_render
45
+ get '/view_with_option_trim'
46
+ assert_equal "hello\n", last_response.body
47
+ end
48
+ end
@@ -7,4 +7,5 @@ $LOAD_PATH.unshift libdir unless $LOAD_PATH.include?(libdir)
7
7
  require "rubygems"
8
8
  require "nancy"
9
9
  require "minitest/unit"
10
+ require "minitest/autorun"
10
11
  require "rack/test"
@@ -0,0 +1,30 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class Reverser
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, response = @app.call(env)
10
+ body = response.body
11
+ [status, headers, [body.first.reverse]]
12
+ end
13
+ end
14
+
15
+ class HelloApp < Nancy::Base
16
+ use Reverser
17
+
18
+ get "/" do
19
+ "Hello World"
20
+ end
21
+ end
22
+
23
+ class UseTest < MiniTest::Unit::TestCase
24
+
25
+ def test_use
26
+ request = Rack::MockRequest.new(HelloApp)
27
+ response = request.get('/')
28
+ assert_equal "dlroW olleH", response.body
29
+ end
30
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nancy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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: 2012-04-04 00:00:00.000000000 Z
12
+ date: 2012-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -101,7 +101,10 @@ files:
101
101
  - test/fixtures/layout.erb
102
102
  - test/fixtures/view.erb
103
103
  - test/fixtures/view_with_trim.erb
104
+ - test/map_test.rb
105
+ - test/render_test.rb
104
106
  - test/test_helper.rb
107
+ - test/use_test.rb
105
108
  homepage: http://guilleiguaran.github.com/nancy
106
109
  licenses: []
107
110
  post_install_message:
@@ -131,4 +134,7 @@ test_files:
131
134
  - test/fixtures/layout.erb
132
135
  - test/fixtures/view.erb
133
136
  - test/fixtures/view_with_trim.erb
137
+ - test/map_test.rb
138
+ - test/render_test.rb
134
139
  - test/test_helper.rb
140
+ - test/use_test.rb