nyny 2.0.0 → 2.1.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG +18 -3
- data/Performance.md +34 -63
- data/README.md +130 -76
- data/benchmark.rb +95 -0
- data/lib/nyny.rb +17 -1
- data/lib/nyny/app.rb +7 -3
- data/lib/nyny/core-ext/runner.rb +2 -1
- data/lib/nyny/{route_signature.rb → route.rb} +13 -8
- data/lib/nyny/router.rb +10 -18
- data/lib/nyny/version.rb +1 -1
- data/nyny.gemspec +1 -1
- data/spec/app_spec.rb +28 -3
- data/spec/nyny_spec.rb +11 -0
- data/spec/runner_spec.rb +6 -0
- data/spec/spec_helper.rb +1 -0
- metadata +6 -36
- data/benchmarks/filters/nyny.rb +0 -18
- data/benchmarks/filters/sinatra.rb +0 -17
- data/benchmarks/helpers/nyny.rb +0 -19
- data/benchmarks/helpers/sinatra.rb +0 -18
- data/benchmarks/simple/nyny.rb +0 -10
- data/benchmarks/simple/sinatra.rb +0 -9
- data/benchmarks/url_pattern/nyny.rb +0 -10
- data/benchmarks/url_pattern/sinatra.rb +0 -8
- data/examples/active_record/.gitignore +0 -1
- data/examples/active_record/Gemfile +0 -15
- data/examples/active_record/Rakefile +0 -51
- data/examples/active_record/config/database.yml +0 -0
- data/examples/active_record/database.rb +0 -13
- data/examples/active_record/db/migrate/20130606133756_add_shouts.rb +0 -12
- data/examples/active_record/models/shout.rb +0 -3
- data/examples/active_record/server.rb +0 -43
- data/examples/data_mapper/Gemfile +0 -7
- data/examples/data_mapper/database.rb +0 -7
- data/examples/data_mapper/db/.gitignore +0 -1
- data/examples/data_mapper/models/shout.rb +0 -7
- data/examples/data_mapper/server.rb +0 -41
- data/examples/json_api.rb +0 -21
- data/examples/templates/server.rb +0 -28
- data/examples/templates/views/index.haml +0 -1
- data/examples/web_sockets/Gemfile +0 -4
- data/examples/web_sockets/public/FABridge.js +0 -604
- data/examples/web_sockets/public/WebSocketMain.swf +0 -0
- data/examples/web_sockets/public/index.html +0 -76
- data/examples/web_sockets/public/swfobject.js +0 -4
- data/examples/web_sockets/public/web_socket.js +0 -388
- data/examples/web_sockets/server.rb +0 -60
- data/spec/route_signature_spec.rb +0 -9
data/lib/nyny.rb
CHANGED
@@ -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/
|
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
|
data/lib/nyny/app.rb
CHANGED
@@ -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
|
-
|
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 ||=
|
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
|
data/lib/nyny/core-ext/runner.rb
CHANGED
@@ -11,7 +11,8 @@ module NYNY
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def run! port=9292
|
14
|
-
middlewares.unshift Rack::
|
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
|
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
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
data/lib/nyny/router.rb
CHANGED
@@ -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
|
-
|
24
|
-
|
14
|
+
env['PATH_INFO'] = '/' if env['PATH_INFO'].empty?
|
15
|
+
route = routes.find {|route| route.match? env }
|
25
16
|
|
26
|
-
if
|
27
|
-
|
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
|
35
|
-
request.
|
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
|
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
|
data/lib/nyny/version.rb
CHANGED
data/nyny.gemspec
CHANGED
@@ -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"]
|
data/spec/app_spec.rb
CHANGED
@@ -132,17 +132,28 @@ describe App do
|
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'should be able to set cookies' do
|
135
|
-
|
135
|
+
app = mock_app do
|
136
136
|
post '/write' do
|
137
137
|
cookies.merge! params
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
|
-
|
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
|
data/spec/nyny_spec.rb
ADDED
data/spec/runner_spec.rb
CHANGED
@@ -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
|
data/spec/spec_helper.rb
CHANGED
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.
|
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-
|
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
|
-
-
|
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/
|
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
|
data/benchmarks/filters/nyny.rb
DELETED
data/benchmarks/helpers/nyny.rb
DELETED
data/benchmarks/simple/nyny.rb
DELETED