nyny 3.3.0 → 3.3.1
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 +6 -0
- data/README.md +4 -5
- data/lib/nyny/app.rb +12 -88
- data/lib/nyny/base.rb +85 -0
- data/lib/nyny/primitives.rb +0 -8
- data/lib/nyny/request_scope.rb +5 -6
- data/lib/nyny/router.rb +1 -1
- data/lib/nyny/version.rb +1 -1
- data/spec/app_spec.rb +9 -3
- data/spec/request_scope_spec.rb +4 -12
- metadata +3 -4
- data/spec/primitives_spec.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6db6a9ca35676823e643db65bc2b458c44bc793f
|
4
|
+
data.tar.gz: 582642543e721156d818975ced8ad08849c5c2eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b4aeb7be4451c4d4663cecf2ce4bb0056bf9a2170d2afd0935399f0d114c37a32fb5f3101029509a839dee046d809fbf3dfd48a52faaa5b996c18d006cc3dfc
|
7
|
+
data.tar.gz: fc5e89993a3d8356c21fab47ca10b132ca1b0eeb1f5e46b7365f648353b35b8bfbfd873e2ec30a4a19717d191c63adfda8404c01fcc70708b203977b4ff53d23
|
data/CHANGELOG
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
3.3.1
|
2
|
+
- if the response of the route is a enumerable, NYNY will try to
|
3
|
+
send it in chunks (given that both the client and the server support it)
|
4
|
+
- NYNY::Base for those who love the barebone (lacks config, namespaces,
|
5
|
+
templates, and run!)
|
6
|
+
|
1
7
|
3.3.0
|
2
8
|
- App.config
|
3
9
|
- before_initialize hooks
|
data/README.md
CHANGED
@@ -4,7 +4,6 @@
|
|
4
4
|
[](https://travis-ci.org/alisnic/nyny)
|
5
5
|
[](https://coveralls.io/r/alisnic/nyny)
|
6
6
|
[](https://codeclimate.com/repos/521b7ee513d637348712864a/feed)
|
7
|
-
[](https://gemnasium.com/alisnic/nyny)
|
8
7
|
[](http://badge.fury.io/rb/nyny)
|
9
8
|
|
10
9
|
```ruby
|
@@ -37,7 +36,7 @@ Open the browser at [http://localhost:9292](http://localhost:9292)
|
|
37
36
|
- [TOP](#new-york-new-york)
|
38
37
|
- [Motivation](#motivation)
|
39
38
|
- [Philosophy](#philosophy)
|
40
|
-
- [Why use NYNY
|
39
|
+
- [Why use NYNY](#why-use-nyny)
|
41
40
|
- [Usage](#usage)
|
42
41
|
- [Environment](#environment)
|
43
42
|
- [Configuration](#configuration)
|
@@ -58,8 +57,8 @@ NYNY is unassuming, it has all the core stuff to get running, but nothing else.
|
|
58
57
|
Your app is the framework. However, it's trivial to extend NYNY via its
|
59
58
|
[extension interface](#extensions).
|
60
59
|
|
61
|
-
# Why use NYNY
|
62
|
-
- It's __very__ small (
|
60
|
+
# Why use NYNY
|
61
|
+
- It's __very__ small (~300 LOC), which is just a little overhead on top of Rack.
|
63
62
|
- You want to dig into the source code and change to your needs (NYNY's source code is more welcoming)
|
64
63
|
- Each NYNY app is a Rack middleware, so it can be used inside of Sinatra, Rails, or any other Rack-based app.
|
65
64
|
- __It uses Journey for routing (Rails' router)__, which makes its routing logic
|
@@ -124,7 +123,7 @@ class App < NYNY::App
|
|
124
123
|
config.always = true
|
125
124
|
end
|
126
125
|
|
127
|
-
|
126
|
+
configure :production do
|
128
127
|
config.prod = true
|
129
128
|
end
|
130
129
|
|
data/lib/nyny/app.rb
CHANGED
@@ -1,61 +1,18 @@
|
|
1
|
-
require '
|
1
|
+
require 'nyny/base'
|
2
|
+
require 'nyny/templates'
|
2
3
|
require 'better_errors'
|
3
4
|
require 'ostruct'
|
4
|
-
require 'nyny/primitives'
|
5
|
-
require 'nyny/request_scope'
|
6
|
-
require 'nyny/router'
|
7
|
-
require 'nyny/templates'
|
8
5
|
|
9
6
|
module NYNY
|
10
|
-
class App
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
inheritable :builder, Rack::Builder.new
|
15
|
-
inheritable :scope_class, Class.new(RequestScope)
|
16
|
-
inheritable :config, OpenStruct.new
|
17
|
-
inheritable :route_defs, []
|
18
|
-
inheritable :before_hooks, []
|
19
|
-
inheritable :after_hooks, []
|
20
|
-
inheritable :before_init_hooks, []
|
21
|
-
inheritable :after_init_hooks, []
|
22
|
-
|
23
|
-
def initialize app=nil
|
24
|
-
self.class.builder.run Router.new({
|
25
|
-
:scope_class => self.class.scope_class,
|
26
|
-
:route_defs => self.class.route_defs,
|
27
|
-
:before_hooks => self.class.before_hooks,
|
28
|
-
:after_hooks => self.class.after_hooks,
|
29
|
-
:fallback => app
|
30
|
-
})
|
31
|
-
|
32
|
-
self.class.before_init_hooks.each {|h| h.call(self)}
|
33
|
-
@app = self.class.builder.to_app
|
34
|
-
self.class.after_init_hooks.each {|h| h.call(self, @app)}
|
35
|
-
end
|
36
|
-
|
37
|
-
def call env
|
38
|
-
@app.call env
|
39
|
-
end
|
7
|
+
class App < Base
|
8
|
+
inheritable :config, OpenStruct.new
|
9
|
+
register NYNY::Templates
|
10
|
+
use Rack::Chunked
|
40
11
|
|
41
|
-
#class methods
|
42
12
|
class << self
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
options[:constraints].merge!(:request_method => method.to_s.upcase)
|
47
|
-
define_route path, options, &block
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def define_route path, options, &block
|
52
|
-
self.route_defs << [path, options, Proc.new(&block)]
|
53
|
-
end
|
54
|
-
|
55
|
-
def register *extensions
|
56
|
-
extensions.each do |ext|
|
57
|
-
extend ext
|
58
|
-
ext.registered(self) if ext.respond_to?(:registered)
|
13
|
+
def configure *envs, &block
|
14
|
+
if envs.map(&:to_sym).include?(NYNY.env.to_sym) or envs.empty?
|
15
|
+
instance_eval(&block)
|
59
16
|
end
|
60
17
|
end
|
61
18
|
|
@@ -70,44 +27,11 @@ module NYNY
|
|
70
27
|
builder.map (url) { use klass }
|
71
28
|
end
|
72
29
|
|
73
|
-
def before &blk
|
74
|
-
before_hooks << Proc.new(&blk)
|
75
|
-
end
|
76
|
-
|
77
|
-
def after &blk
|
78
|
-
after_hooks << Proc.new(&blk)
|
79
|
-
end
|
80
|
-
|
81
|
-
def before_initialize &blk
|
82
|
-
before_init_hooks << Proc.new(&blk)
|
83
|
-
end
|
84
|
-
|
85
|
-
def after_initialize &blk
|
86
|
-
after_init_hooks << Proc.new(&blk)
|
87
|
-
end
|
88
|
-
|
89
|
-
def configure *envs, &block
|
90
|
-
if envs.map(&:to_sym).include?(NYNY.env.to_sym) or envs.empty?
|
91
|
-
instance_eval(&block)
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def use middleware, *args, &block
|
96
|
-
builder.use middleware, *args, &block
|
97
|
-
end
|
98
|
-
|
99
|
-
def helpers *args, &block
|
100
|
-
args << Module.new(&block) if block_given?
|
101
|
-
args.each {|m| scope_class.send :include, m }
|
102
|
-
end
|
103
|
-
|
104
30
|
def run! port=9292
|
105
31
|
use Rack::CommonLogger
|
106
32
|
use BetterErrors::Middleware unless NYNY.env.production?
|
107
|
-
Rack::Handler.pick(['thin', 'webrick']).run new, :Port => port
|
33
|
+
Rack::Handler.pick(['puma', 'thin', 'webrick']).run new, :Port => port
|
108
34
|
end
|
109
|
-
end
|
110
|
-
|
111
|
-
register NYNY::Templates
|
35
|
+
end
|
112
36
|
end
|
113
|
-
end
|
37
|
+
end
|
data/lib/nyny/base.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'rack'
|
2
|
+
require 'nyny/primitives'
|
3
|
+
require 'nyny/request_scope'
|
4
|
+
require 'nyny/router'
|
5
|
+
|
6
|
+
module NYNY
|
7
|
+
class Base
|
8
|
+
include NYNY::Inheritable
|
9
|
+
HTTP_VERBS = [:delete, :get, :head, :options, :patch, :post, :put, :trace]
|
10
|
+
|
11
|
+
inheritable :builder, Rack::Builder.new
|
12
|
+
inheritable :scope_class, Class.new(RequestScope)
|
13
|
+
inheritable :route_defs, []
|
14
|
+
inheritable :before_hooks, []
|
15
|
+
inheritable :after_hooks, []
|
16
|
+
inheritable :before_init_hooks, []
|
17
|
+
inheritable :after_init_hooks, []
|
18
|
+
|
19
|
+
def initialize app=nil
|
20
|
+
self.class.before_init_hooks.each {|h| h.call(self)}
|
21
|
+
|
22
|
+
self.class.builder.run Router.new({
|
23
|
+
:scope_class => self.class.scope_class,
|
24
|
+
:route_defs => self.class.route_defs,
|
25
|
+
:before_hooks => self.class.before_hooks,
|
26
|
+
:after_hooks => self.class.after_hooks,
|
27
|
+
:fallback => app
|
28
|
+
})
|
29
|
+
|
30
|
+
@app = self.class.builder.to_app
|
31
|
+
self.class.after_init_hooks.each {|h| h.call(self, @app)}
|
32
|
+
end
|
33
|
+
|
34
|
+
def call env
|
35
|
+
@app.call env
|
36
|
+
end
|
37
|
+
|
38
|
+
#class methods
|
39
|
+
class << self
|
40
|
+
HTTP_VERBS.each do |method|
|
41
|
+
define_method method do |path, options={}, &block|
|
42
|
+
options[:constraints] ||= {}
|
43
|
+
options[:constraints].merge!(:request_method => method.to_s.upcase)
|
44
|
+
define_route path, options, &block
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def define_route path, options, &block
|
49
|
+
self.route_defs << [path, options, Proc.new(&block)]
|
50
|
+
end
|
51
|
+
|
52
|
+
def before &blk
|
53
|
+
before_hooks << Proc.new(&blk)
|
54
|
+
end
|
55
|
+
|
56
|
+
def after &blk
|
57
|
+
after_hooks << Proc.new(&blk)
|
58
|
+
end
|
59
|
+
|
60
|
+
def before_initialize &blk
|
61
|
+
before_init_hooks << Proc.new(&blk)
|
62
|
+
end
|
63
|
+
|
64
|
+
def after_initialize &blk
|
65
|
+
after_init_hooks << Proc.new(&blk)
|
66
|
+
end
|
67
|
+
|
68
|
+
def use middleware, *args, &block
|
69
|
+
builder.use middleware, *args, &block
|
70
|
+
end
|
71
|
+
|
72
|
+
def register *extensions
|
73
|
+
extensions.each do |ext|
|
74
|
+
extend ext
|
75
|
+
ext.registered(self) if ext.respond_to?(:registered)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def helpers *args, &block
|
80
|
+
args << Module.new(&block) if block_given?
|
81
|
+
args.each {|m| scope_class.send :include, m }
|
82
|
+
end
|
83
|
+
end #class methods
|
84
|
+
end
|
85
|
+
end
|
data/lib/nyny/primitives.rb
CHANGED
@@ -38,14 +38,6 @@ module NYNY
|
|
38
38
|
end
|
39
39
|
|
40
40
|
class Response < Rack::Response
|
41
|
-
def rewrite str
|
42
|
-
@body = []
|
43
|
-
@length = 0
|
44
|
-
header.delete "Content-Type"
|
45
|
-
header.delete "Content-Length"
|
46
|
-
write str
|
47
|
-
end
|
48
|
-
alias_method :body=, :rewrite
|
49
41
|
end
|
50
42
|
|
51
43
|
def self.root
|
data/lib/nyny/request_scope.rb
CHANGED
@@ -24,11 +24,9 @@ module NYNY
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def halt status, headers={}, body=''
|
27
|
-
response
|
28
|
-
response.headers.merge! headers
|
29
|
-
response.rewrite body
|
27
|
+
@response = Response.new body, status, headers
|
30
28
|
cookies.finish!(response) if @cookies
|
31
|
-
throw :halt, response
|
29
|
+
throw :halt, response
|
32
30
|
end
|
33
31
|
|
34
32
|
def redirect_to uri, status=302
|
@@ -37,9 +35,10 @@ module NYNY
|
|
37
35
|
alias_method :redirect, :redirect_to
|
38
36
|
|
39
37
|
def apply_to &handler
|
40
|
-
|
38
|
+
data = instance_eval(&handler)
|
39
|
+
data.respond_to?(:each) ? response.body = data : response.write(data)
|
41
40
|
cookies.finish!(response) if @cookies
|
42
|
-
response
|
41
|
+
response
|
43
42
|
end
|
44
43
|
end
|
45
44
|
end
|
data/lib/nyny/router.rb
CHANGED
data/lib/nyny/version.rb
CHANGED
data/spec/app_spec.rb
CHANGED
@@ -1,11 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe App do
|
4
|
+
let (:klass) { mock_app_class {} }
|
4
5
|
let (:app) { mock_app {} }
|
5
6
|
|
6
7
|
it 'should return a rack response on call' do
|
7
|
-
|
8
|
-
|
8
|
+
env = Rack::MockRequest.env_for '/'
|
9
|
+
res = klass.new.call(env)
|
10
|
+
res.should be_a(Array)
|
11
|
+
res.size.should == 3
|
12
|
+
res[0].should == 404
|
13
|
+
res[1].should be_a(Hash)
|
14
|
+
res[2].should respond_to(:each)
|
9
15
|
end
|
10
16
|
|
11
17
|
it 'should return 404 for non-matched routes' do
|
@@ -144,7 +150,7 @@ describe App do
|
|
144
150
|
|
145
151
|
it 'acts well as a middleware' do
|
146
152
|
app = lambda do |env|
|
147
|
-
[210, {}, ['Hello from downstream']]
|
153
|
+
[210, {'Content-Length' => 21}, ['Hello from downstream']]
|
148
154
|
end
|
149
155
|
|
150
156
|
app_class = mock_app_class do
|
data/spec/request_scope_spec.rb
CHANGED
@@ -76,13 +76,13 @@ describe RequestScope do
|
|
76
76
|
it '#headers should set the header values' do
|
77
77
|
subject.headers['Head'] = 'Tail'
|
78
78
|
response = subject.apply_to &handler
|
79
|
-
response[
|
79
|
+
response.headers['Head'].should == 'Tail'
|
80
80
|
end
|
81
81
|
|
82
82
|
it '#status should set the response status' do
|
83
83
|
forbid = Proc.new { status 403 }
|
84
84
|
response = subject.apply_to &forbid
|
85
|
-
response
|
85
|
+
response.status.should == 403
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'params should have insensitive keys' do
|
@@ -138,16 +138,8 @@ describe RequestScope do
|
|
138
138
|
it '#redirect_to should redirect' do
|
139
139
|
redir = Proc.new { redirect_to 'http://foo.bar' }
|
140
140
|
response = catch(:halt) { subject.apply_to &redir }
|
141
|
-
response
|
142
|
-
response[
|
143
|
-
end
|
144
|
-
|
145
|
-
it '#apply_to should return a Rack response' do
|
146
|
-
response = subject.apply_to &handler
|
147
|
-
response.length.should == 3
|
148
|
-
response[0].should == 200
|
149
|
-
response[1].should == subject.headers
|
150
|
-
response[2].should be_a(Rack::BodyProxy)
|
141
|
+
response.status == 302
|
142
|
+
response.headers['Location'].should == 'http://foo.bar'
|
151
143
|
end
|
152
144
|
end
|
153
145
|
end
|
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: 3.3.
|
4
|
+
version: 3.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrei Lisnic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-contrib
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- Rakefile
|
127
127
|
- lib/nyny.rb
|
128
128
|
- lib/nyny/app.rb
|
129
|
+
- lib/nyny/base.rb
|
129
130
|
- lib/nyny/primitives.rb
|
130
131
|
- lib/nyny/request_scope.rb
|
131
132
|
- lib/nyny/router.rb
|
@@ -137,7 +138,6 @@ files:
|
|
137
138
|
- spec/inheritance_spec.rb
|
138
139
|
- spec/initialize_hooks_spec.rb
|
139
140
|
- spec/nyny_spec.rb
|
140
|
-
- spec/primitives_spec.rb
|
141
141
|
- spec/request_scope_spec.rb
|
142
142
|
- spec/runner_spec.rb
|
143
143
|
- spec/spec_helper.rb
|
@@ -176,7 +176,6 @@ test_files:
|
|
176
176
|
- spec/inheritance_spec.rb
|
177
177
|
- spec/initialize_hooks_spec.rb
|
178
178
|
- spec/nyny_spec.rb
|
179
|
-
- spec/primitives_spec.rb
|
180
179
|
- spec/request_scope_spec.rb
|
181
180
|
- spec/runner_spec.rb
|
182
181
|
- spec/spec_helper.rb
|
data/spec/primitives_spec.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'NYNY primitives' do
|
4
|
-
describe NYNY::Response do
|
5
|
-
it 'allows to rewrite the response' do
|
6
|
-
resp = NYNY::Response.new
|
7
|
-
resp.write 'foo'
|
8
|
-
resp.rewrite 'banana'
|
9
|
-
resp.headers['Content-Length'].should == "6"
|
10
|
-
resp.body.first.should == 'banana'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|