nyny 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +18 -3
  3. data/Performance.md +34 -63
  4. data/README.md +130 -76
  5. data/benchmark.rb +95 -0
  6. data/lib/nyny.rb +17 -1
  7. data/lib/nyny/app.rb +7 -3
  8. data/lib/nyny/core-ext/runner.rb +2 -1
  9. data/lib/nyny/{route_signature.rb → route.rb} +13 -8
  10. data/lib/nyny/router.rb +10 -18
  11. data/lib/nyny/version.rb +1 -1
  12. data/nyny.gemspec +1 -1
  13. data/spec/app_spec.rb +28 -3
  14. data/spec/nyny_spec.rb +11 -0
  15. data/spec/runner_spec.rb +6 -0
  16. data/spec/spec_helper.rb +1 -0
  17. metadata +6 -36
  18. data/benchmarks/filters/nyny.rb +0 -18
  19. data/benchmarks/filters/sinatra.rb +0 -17
  20. data/benchmarks/helpers/nyny.rb +0 -19
  21. data/benchmarks/helpers/sinatra.rb +0 -18
  22. data/benchmarks/simple/nyny.rb +0 -10
  23. data/benchmarks/simple/sinatra.rb +0 -9
  24. data/benchmarks/url_pattern/nyny.rb +0 -10
  25. data/benchmarks/url_pattern/sinatra.rb +0 -8
  26. data/examples/active_record/.gitignore +0 -1
  27. data/examples/active_record/Gemfile +0 -15
  28. data/examples/active_record/Rakefile +0 -51
  29. data/examples/active_record/config/database.yml +0 -0
  30. data/examples/active_record/database.rb +0 -13
  31. data/examples/active_record/db/migrate/20130606133756_add_shouts.rb +0 -12
  32. data/examples/active_record/models/shout.rb +0 -3
  33. data/examples/active_record/server.rb +0 -43
  34. data/examples/data_mapper/Gemfile +0 -7
  35. data/examples/data_mapper/database.rb +0 -7
  36. data/examples/data_mapper/db/.gitignore +0 -1
  37. data/examples/data_mapper/models/shout.rb +0 -7
  38. data/examples/data_mapper/server.rb +0 -41
  39. data/examples/json_api.rb +0 -21
  40. data/examples/templates/server.rb +0 -28
  41. data/examples/templates/views/index.haml +0 -1
  42. data/examples/web_sockets/Gemfile +0 -4
  43. data/examples/web_sockets/public/FABridge.js +0 -604
  44. data/examples/web_sockets/public/WebSocketMain.swf +0 -0
  45. data/examples/web_sockets/public/index.html +0 -76
  46. data/examples/web_sockets/public/swfobject.js +0 -4
  47. data/examples/web_sockets/public/web_socket.js +0 -388
  48. data/examples/web_sockets/server.rb +0 -60
  49. data/spec/route_signature_spec.rb +0 -9
@@ -4,7 +4,7 @@ require 'rack'
4
4
  require 'nyny/version'
5
5
  require 'nyny/primitives'
6
6
  require 'nyny/request_scope'
7
- require 'nyny/route_signature'
7
+ require 'nyny/route'
8
8
  require 'nyny/middleware_chain'
9
9
  require 'nyny/app'
10
10
  require 'nyny/router'
@@ -15,4 +15,20 @@ require 'nyny/core-ext/runner'
15
15
 
16
16
  module NYNY
17
17
  App.register NYNY::Runner
18
+
19
+ class EnvString < String
20
+ [:production, :development, :test].each do |env|
21
+ define_method "#{env}?" do
22
+ self == env.to_s
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.root
28
+ Dir.pwd
29
+ end
30
+
31
+ def self.env
32
+ @env ||= EnvString.new(ENV['RACK_ENV'] || 'development')
33
+ end
18
34
  end
@@ -26,12 +26,12 @@ module NYNY
26
26
  class << self
27
27
  HTTP_VERBS.each do |method|
28
28
  define_method method do |str, &blk|
29
- (routes[method] ||= {})[RouteSignature.new(str)] = Proc.new &blk
29
+ routes << Route.new(method, str, &blk)
30
30
  end
31
31
  end
32
32
 
33
33
  def middlewares; @middlewares ||= [] end
34
- def routes; @routes ||= {} end
34
+ def routes; @routes ||= [] end
35
35
  def before_hooks; @before_hooks ||= [] end
36
36
  def after_hooks; @after_hooks ||= [] end
37
37
 
@@ -55,7 +55,11 @@ module NYNY
55
55
  middlewares << [middleware, args, block]
56
56
  end
57
57
 
58
- def helpers *args
58
+ def helpers *args, &block
59
+ if block_given?
60
+ args << Module.new(&block)
61
+ end
62
+
59
63
  args.each {|m| RequestScope.add_helper_module m }
60
64
  end
61
65
  end #class methods
@@ -11,7 +11,8 @@ module NYNY
11
11
  end
12
12
 
13
13
  def run! port=9292
14
- middlewares.unshift Rack::ShowExceptions, Rack::CommonLogger
14
+ middlewares.unshift Rack::CommonLogger
15
+ middlewares.unshift Rack::ShowExceptions unless NYNY.env.production?
15
16
  optimal_runner.run new, :Port => port
16
17
  end
17
18
  end
@@ -1,10 +1,12 @@
1
1
  module NYNY
2
- class RouteSignature
2
+ class Route
3
3
  NAME_PATTERN = /:(\S+)/
4
4
 
5
- attr_reader :pattern
6
- def initialize signature
5
+ attr_reader :pattern, :handler, :method
6
+ def initialize method, signature, &block
7
7
  @pattern = pattern_for signature
8
+ @handler = Proc.new(&block)
9
+ @method = method.to_s.upcase
8
10
  end
9
11
 
10
12
  def pattern_for signature
@@ -25,11 +27,14 @@ module NYNY
25
27
  %r(^\/#{groups}$)
26
28
  end
27
29
 
28
- def match path
29
- data = pattern.match path
30
- if data
31
- Hash[data.names.map {|n| [n.to_sym, URI.unescape(data[n])]}]
32
- end
30
+ def match? env
31
+ return false unless method == env['REQUEST_METHOD']
32
+ not pattern.match(env['PATH_INFO']).nil?
33
+ end
34
+
35
+ def url_params env
36
+ data = pattern.match(env['PATH_INFO'])
37
+ Hash[data.names.map {|n| [n.to_sym, URI.unescape(data[n])]}]
33
38
  end
34
39
  end
35
40
  end
@@ -10,34 +10,26 @@ module NYNY
10
10
  @after_hooks = options[:after_hooks]
11
11
  end
12
12
 
13
- def find_handler request
14
- routes.fetch(request.request_method.downcase.to_sym, []).each do |sig, h|
15
- params = sig.match request.path
16
- return [h, params] if params
17
- end
18
-
19
- [NullHandler, {}]
20
- end
21
-
22
13
  def call env
23
- req = Request.new(env)
24
- handler, params = find_handler req
14
+ env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
15
+ route = routes.find {|route| route.match? env }
25
16
 
26
- if handler != NullHandler
27
- prepare_params req, params
28
- process req, handler
17
+ if route
18
+ process route, env
29
19
  else
30
20
  fallback.call env
31
21
  end
32
22
  end
33
23
 
34
- def prepare_params request, url_params
35
- request.params.merge! url_params
24
+ def process route, env
25
+ request = Request.new(env)
26
+ request.params.merge! route.url_params(env)
36
27
  request.params.default_proc = proc {|h,k| h[k.to_s] || h[k.to_sym]}
28
+
29
+ eval_response RequestScope.new(request), route.handler
37
30
  end
38
31
 
39
- def process request, handler
40
- scope = RequestScope.new(request)
32
+ def eval_response scope, handler
41
33
  catch (:halt) do
42
34
  before_hooks.each {|h| scope.instance_eval &h }
43
35
  response = scope.apply_to &handler
@@ -1,3 +1,3 @@
1
1
  module NYNY
2
- VERSION = "2.0.0"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "http://alisnic.github.io/nyny/"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($/).select {|f| !f.start_with?('examples')}
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
@@ -132,17 +132,28 @@ describe App do
132
132
  end
133
133
 
134
134
  it 'should be able to set cookies' do
135
- app_class = Class.new(App) do
135
+ app = mock_app do
136
136
  post '/write' do
137
137
  cookies.merge! params
138
138
  end
139
139
  end
140
140
 
141
- req = Rack::MockRequest.env_for '/write?foo=bar', :method => :post
142
- res = app_class.new.call(req)
141
+ res = app.post '/write?foo=bar'
143
142
  res.headers['Set-Cookie'].should == 'foo=bar'
144
143
  end
145
144
 
145
+ it 'works with empty path' do
146
+ kls = mock_app_class do
147
+ get '/' do
148
+ 'Hello'
149
+ end
150
+ end
151
+
152
+ env = Rack::MockRequest.env_for '/'
153
+ env['PATH_INFO'] = ''
154
+ kls.new.call(env).body.first.should == 'Hello'
155
+ end
156
+
146
157
  describe 'Class level api' do
147
158
  let (:app_class) { Class.new(App) }
148
159
  describe 'middlewares' do
@@ -170,6 +181,20 @@ describe App do
170
181
  app_class.helpers NullHelper, NullHelper2
171
182
  RequestScope.ancestors.should include(NullHelper, NullHelper2)
172
183
  end
184
+
185
+ it 'should allow to define helpers with a block' do
186
+ app_class.helpers do
187
+ def foo
188
+ 'bar'
189
+ end
190
+ end
191
+
192
+ app_class.get '/' do
193
+ foo.should == 'bar'
194
+ end
195
+ req = Rack::MockRequest.env_for '/'
196
+ res = app_class.new.call(req)
197
+ end
173
198
  end
174
199
  end
175
200
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe NYNY do
4
+ it '.root points to pwd' do
5
+ NYNY.root.should == Dir.pwd
6
+ end
7
+
8
+ it 'has the correct env' do
9
+ NYNY.env.should be_test
10
+ end
11
+ end
@@ -13,4 +13,10 @@ describe Runner do
13
13
  kls.middlewares[1].should == Rack::CommonLogger
14
14
  end
15
15
 
16
+ it 'should not include show exceptions middleware in production' do
17
+ NYNY.env.stub :production? => true
18
+ kls.run!
19
+ kls.middlewares.should == [Rack::CommonLogger]
20
+ end
21
+
16
22
  end
@@ -1,5 +1,6 @@
1
1
  require 'rack'
2
2
  require 'securerandom'
3
+ ENV['RACK_ENV'] = 'test'
3
4
 
4
5
  if ENV['TRAVIS']
5
6
  require 'coveralls'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nyny
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Lisnic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-30 00:00:00.000000000 Z
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -83,51 +83,21 @@ files:
83
83
  - Performance.md
84
84
  - README.md
85
85
  - Rakefile
86
- - benchmarks/filters/nyny.rb
87
- - benchmarks/filters/sinatra.rb
88
- - benchmarks/helpers/nyny.rb
89
- - benchmarks/helpers/sinatra.rb
90
- - benchmarks/simple/nyny.rb
91
- - benchmarks/simple/sinatra.rb
92
- - benchmarks/url_pattern/nyny.rb
93
- - benchmarks/url_pattern/sinatra.rb
94
- - examples/active_record/.gitignore
95
- - examples/active_record/Gemfile
96
- - examples/active_record/Rakefile
97
- - examples/active_record/config/database.yml
98
- - examples/active_record/database.rb
99
- - examples/active_record/db/migrate/20130606133756_add_shouts.rb
100
- - examples/active_record/models/shout.rb
101
- - examples/active_record/server.rb
102
- - examples/data_mapper/Gemfile
103
- - examples/data_mapper/database.rb
104
- - examples/data_mapper/db/.gitignore
105
- - examples/data_mapper/models/shout.rb
106
- - examples/data_mapper/server.rb
107
- - examples/json_api.rb
108
- - examples/templates/server.rb
109
- - examples/templates/views/index.haml
110
- - examples/web_sockets/Gemfile
111
- - examples/web_sockets/public/FABridge.js
112
- - examples/web_sockets/public/WebSocketMain.swf
113
- - examples/web_sockets/public/index.html
114
- - examples/web_sockets/public/swfobject.js
115
- - examples/web_sockets/public/web_socket.js
116
- - examples/web_sockets/server.rb
86
+ - benchmark.rb
117
87
  - lib/nyny.rb
118
88
  - lib/nyny/app.rb
119
89
  - lib/nyny/core-ext/runner.rb
120
90
  - lib/nyny/middleware_chain.rb
121
91
  - lib/nyny/primitives.rb
122
92
  - lib/nyny/request_scope.rb
123
- - lib/nyny/route_signature.rb
93
+ - lib/nyny/route.rb
124
94
  - lib/nyny/router.rb
125
95
  - lib/nyny/version.rb
126
96
  - nyny.gemspec
127
97
  - spec/app_spec.rb
98
+ - spec/nyny_spec.rb
128
99
  - spec/primitives_spec.rb
129
100
  - spec/request_scope_spec.rb
130
- - spec/route_signature_spec.rb
131
101
  - spec/runner_spec.rb
132
102
  - spec/spec_helper.rb
133
103
  homepage: http://alisnic.github.io/nyny/
@@ -156,8 +126,8 @@ specification_version: 4
156
126
  summary: New York, New York.
157
127
  test_files:
158
128
  - spec/app_spec.rb
129
+ - spec/nyny_spec.rb
159
130
  - spec/primitives_spec.rb
160
131
  - spec/request_scope_spec.rb
161
- - spec/route_signature_spec.rb
162
132
  - spec/runner_spec.rb
163
133
  - spec/spec_helper.rb
@@ -1,18 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'nyny'
3
-
4
- class App < NYNY::App
5
- before do
6
- request
7
- end
8
-
9
- after do
10
- response
11
- end
12
-
13
- get '/' do
14
- 'Hello World!'
15
- end
16
- end
17
-
18
- App.run!
@@ -1,17 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'sinatra'
3
-
4
- class App < Sinatra::Base
5
- before do
6
- request
7
- end
8
-
9
- after do
10
- response
11
- end
12
-
13
- get '/' do
14
- 'Hello World!'
15
- end
16
- end
17
-
@@ -1,19 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'nyny'
3
-
4
- module Dude
5
- def da_request_man
6
- request
7
- end
8
- end
9
-
10
- class App < NYNY::App
11
- helpers Dude
12
-
13
- get '/' do
14
- da_request_man
15
- 'Hello World!'
16
- end
17
- end
18
-
19
- App.run!
@@ -1,18 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'sinatra'
3
-
4
- module Dude
5
- def da_request_man
6
- request
7
- end
8
- end
9
-
10
- class App < Sinatra::Base
11
- helpers Dude
12
-
13
- get '/' do
14
- da_request_man
15
- 'Hello World!'
16
- end
17
- end
18
-
@@ -1,10 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'nyny'
3
-
4
- class App < NYNY::App
5
- get '/' do
6
- 'Hello World!'
7
- end
8
- end
9
-
10
- App.run!
@@ -1,9 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'sinatra'
3
-
4
- class App < Sinatra::Base
5
- get '/' do
6
- 'Hello World!'
7
- end
8
- end
9
-
@@ -1,10 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'nyny'
3
-
4
- class App < NYNY::App
5
- get '/hello/:name' do
6
- "Hello #{params[:name]}!"
7
- end
8
- end
9
-
10
- App.run!
@@ -1,8 +0,0 @@
1
- #!ruby -I ../../lib -I lib
2
- require 'sinatra'
3
-
4
- class App < Sinatra::Base
5
- get '/hello/:name' do
6
- "Hello #{params[:name]}!"
7
- end
8
- end