newark 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/benchmark/benchmark_router.rb +43 -10
- data/lib/newark/app.rb +33 -12
- data/lib/newark/request.rb +9 -1
- data/lib/newark/route.rb +2 -0
- data/lib/newark/version.rb +1 -1
- data/test/test_app.rb +29 -0
- data/test/test_request.rb +20 -0
- metadata +27 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fed1a9e5d5021c32f65e7813c10a5dbc1f409e83
|
4
|
+
data.tar.gz: aa12c7aba2fec27ac9bb7dcdb9e92202dcf1d03c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee6980bb5d7b94957b37ce3cb21bbdd19f0b65cd27d232aefbbaf2a563db3cdcbf81bae63edf36ecf73c4248209f70e01eda7f71900f0646092fc6298aebfc74
|
7
|
+
data.tar.gz: 6ff2442b777d6a93d3f2c4fc892729e1be9530ffef45e556f79bb7681420dc13cc15dffd9cb19b877ee8d5d99cd8368989f82eaf32f2efc761e3751a3e6f28de
|
@@ -17,6 +17,12 @@ class App
|
|
17
17
|
'hello'
|
18
18
|
end
|
19
19
|
|
20
|
+
get '/hello1' do
|
21
|
+
hello
|
22
|
+
end
|
23
|
+
|
24
|
+
get 'hello2', :hello
|
25
|
+
|
20
26
|
get(/\/regexp/) do
|
21
27
|
'regexp'
|
22
28
|
end
|
@@ -38,6 +44,16 @@ class App
|
|
38
44
|
get '/variables/:a/:b' do
|
39
45
|
"#{params[:a]}:#{params[:b]}"
|
40
46
|
end
|
47
|
+
|
48
|
+
get '/path/*globbing' do
|
49
|
+
params[:globbing]
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def hello
|
55
|
+
'hello'
|
56
|
+
end
|
41
57
|
end
|
42
58
|
|
43
59
|
include Rack::Test::Methods
|
@@ -46,26 +62,43 @@ def app
|
|
46
62
|
App.new
|
47
63
|
end
|
48
64
|
|
65
|
+
50_000.times do
|
66
|
+
get '/hello1'
|
67
|
+
get '/hello2'
|
68
|
+
end
|
69
|
+
|
49
70
|
Benchmark.ips do |x|
|
50
71
|
|
51
|
-
x.report('match /')
|
72
|
+
x.report('match /') do
|
52
73
|
get '/'
|
53
|
-
|
74
|
+
end
|
75
|
+
|
76
|
+
x.report('handler: block') do
|
77
|
+
get '/hello1'
|
78
|
+
end
|
54
79
|
|
55
|
-
x.report('
|
80
|
+
x.report('handler: method') do
|
81
|
+
get '/hello2'
|
82
|
+
end
|
83
|
+
|
84
|
+
x.report('match / with param') do
|
56
85
|
get '/', user: 'frank'
|
57
|
-
|
86
|
+
end
|
58
87
|
|
59
|
-
x.report('404')
|
88
|
+
x.report('404') do
|
60
89
|
get '/not_found'
|
61
|
-
|
90
|
+
end
|
62
91
|
|
63
|
-
x.report('post')
|
92
|
+
x.report('post') do
|
64
93
|
post '/create'
|
65
|
-
|
94
|
+
end
|
66
95
|
|
67
|
-
x.report('path params')
|
96
|
+
x.report('path params') do
|
68
97
|
get '/variables/fu/bar'
|
69
|
-
|
98
|
+
end
|
99
|
+
|
100
|
+
x.report('path globbing') do
|
101
|
+
get '/path/a/b/c/d'
|
102
|
+
end
|
70
103
|
|
71
104
|
end
|
data/lib/newark/app.rb
CHANGED
@@ -8,30 +8,38 @@ module Newark
|
|
8
8
|
:patch, :post, :put, :trace ].freeze
|
9
9
|
|
10
10
|
def self.included(klass)
|
11
|
-
klass.instance_variable_set :@routes,
|
12
|
-
klass.instance_variable_set :@before_hooks, []
|
13
|
-
klass.instance_variable_set :@after_hooks, []
|
11
|
+
klass.instance_variable_set :@routes, {}
|
14
12
|
klass.extend ClassMethods
|
15
13
|
end
|
16
14
|
|
17
15
|
module ClassMethods
|
18
16
|
|
19
17
|
HTTP_VERBS.each do |verb|
|
20
|
-
define_method verb do |path,
|
21
|
-
|
22
|
-
|
18
|
+
define_method verb do |path, *args, &block|
|
19
|
+
if block.is_a?(Proc)
|
20
|
+
handler = block
|
21
|
+
constraints = args[0] || {}
|
22
|
+
else
|
23
|
+
handler = args[0]
|
24
|
+
constraints = args[1] || {}
|
25
|
+
end
|
26
|
+
|
27
|
+
define_route(verb.to_s.upcase, path, constraints, handler)
|
23
28
|
end
|
24
29
|
end
|
25
30
|
|
26
|
-
def define_route(path,
|
27
|
-
@routes
|
31
|
+
def define_route(verb, path, constraints, handler)
|
32
|
+
@routes[verb] ||= []
|
33
|
+
@routes[verb] << Route.new(path, constraints, handler)
|
28
34
|
end
|
29
35
|
|
30
36
|
def before(&block)
|
37
|
+
@before_hooks ||= []
|
31
38
|
@before_hooks << block
|
32
39
|
end
|
33
40
|
|
34
41
|
def after(&block)
|
42
|
+
@after_hooks ||= []
|
35
43
|
@after_hooks << block
|
36
44
|
end
|
37
45
|
end
|
@@ -58,11 +66,11 @@ module Newark
|
|
58
66
|
end
|
59
67
|
|
60
68
|
def route
|
61
|
-
exec_before_hooks
|
62
69
|
route = match_route
|
63
70
|
if route
|
64
71
|
request.params.merge!(route.params)
|
65
|
-
|
72
|
+
exec_before_hooks
|
73
|
+
response.body = exec_handler(route.handler)
|
66
74
|
exec_after_hooks
|
67
75
|
response.finish
|
68
76
|
else
|
@@ -77,7 +85,19 @@ module Newark
|
|
77
85
|
end
|
78
86
|
|
79
87
|
def routes
|
80
|
-
self.class.instance_variable_get(:@routes)
|
88
|
+
self.class.instance_variable_get(:@routes)[@request.request_method]
|
89
|
+
end
|
90
|
+
|
91
|
+
def exec(action)
|
92
|
+
if action.respond_to? :to_sym
|
93
|
+
send(action)
|
94
|
+
else
|
95
|
+
instance_exec(&action)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def exec_handler(handler)
|
100
|
+
exec(handler)
|
81
101
|
end
|
82
102
|
|
83
103
|
def exec_before_hooks
|
@@ -89,8 +109,9 @@ module Newark
|
|
89
109
|
end
|
90
110
|
|
91
111
|
def exec_hooks(hooks)
|
112
|
+
return if hooks.nil?
|
92
113
|
hooks.each do |hook|
|
93
|
-
|
114
|
+
exec(hook)
|
94
115
|
end
|
95
116
|
end
|
96
117
|
end
|
data/lib/newark/request.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'active_support/hash_with_indifferent_access'
|
2
|
+
require 'securerandom'
|
2
3
|
|
3
4
|
module Newark
|
4
5
|
class Request < Rack::Request
|
@@ -7,6 +8,8 @@ module Newark
|
|
7
8
|
uri = "#{scheme}://#{host_with_port}#{path_info}"
|
8
9
|
uri << "?#{query_string}" unless query_string.empty?
|
9
10
|
URI(uri)
|
11
|
+
rescue URI::InvalidURIError
|
12
|
+
URI(URI.escape(uri))
|
10
13
|
end
|
11
14
|
|
12
15
|
def params
|
@@ -21,12 +24,17 @@ module Newark
|
|
21
24
|
@headers ||= original_headers
|
22
25
|
end
|
23
26
|
|
27
|
+
def request_id
|
28
|
+
@env['action_dispatch.request_id'] ||
|
29
|
+
(@env['rack.request_id'] ||= headers['X-Request-Id'] || SecureRandom.uuid)
|
30
|
+
end
|
31
|
+
|
24
32
|
protected
|
25
33
|
|
26
34
|
def original_headers
|
27
35
|
{}.tap do |headers|
|
28
36
|
env.select { |k, v| k.start_with?('HTTP_') }.each_pair do |k, v|
|
29
|
-
header = k.sub(/^HTTP_/, '').split('
|
37
|
+
header = k.sub(/^HTTP_/, '').gsub(/_/, '-').split('-').map(&:capitalize).join('-')
|
30
38
|
headers[header] = v
|
31
39
|
end
|
32
40
|
end
|
data/lib/newark/route.rb
CHANGED
@@ -9,6 +9,8 @@ module Newark
|
|
9
9
|
attr_reader :handler, :params
|
10
10
|
|
11
11
|
def initialize(path, constraints, handler)
|
12
|
+
fail ArgumentError, 'You must define a route handler' if handler.nil?
|
13
|
+
|
12
14
|
@constraints = Constraint.load(constraints)
|
13
15
|
@handler = handler
|
14
16
|
@path = path_matcher(path)
|
data/lib/newark/version.rb
CHANGED
data/test/test_app.rb
CHANGED
@@ -10,6 +10,18 @@ class NameApp
|
|
10
10
|
get '/upcaser' do
|
11
11
|
upcase(params[:name])
|
12
12
|
end
|
13
|
+
|
14
|
+
get '/hello1' do
|
15
|
+
hello
|
16
|
+
end
|
17
|
+
|
18
|
+
get '/hello2', :hello
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def hello
|
23
|
+
'Hello'
|
24
|
+
end
|
13
25
|
end
|
14
26
|
|
15
27
|
class TestApp < Minitest::Unit::TestCase
|
@@ -26,4 +38,21 @@ class TestApp < Minitest::Unit::TestCase
|
|
26
38
|
assert_equal 'MIKE', last_response.body
|
27
39
|
end
|
28
40
|
|
41
|
+
def test_alternate_action_invocation
|
42
|
+
get '/hello1'
|
43
|
+
assert last_response.ok?
|
44
|
+
assert_equal 'Hello', last_response.body
|
45
|
+
|
46
|
+
get '/hello2'
|
47
|
+
assert last_response.ok?
|
48
|
+
assert_equal 'Hello', last_response.body
|
49
|
+
end
|
50
|
+
|
51
|
+
# def test_before_hooks_halting_execution
|
52
|
+
# get '/'
|
53
|
+
# refute last_response.ok?
|
54
|
+
# assert_equal 403, last_response.status
|
55
|
+
# assert_equal '', last_response.body
|
56
|
+
# end
|
57
|
+
|
29
58
|
end
|
data/test/test_request.rb
CHANGED
@@ -14,6 +14,10 @@ class RequestApp
|
|
14
14
|
post '/body' do
|
15
15
|
request.body
|
16
16
|
end
|
17
|
+
|
18
|
+
get '/request_id' do
|
19
|
+
request.request_id
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
class TestRequest < Minitest::Unit::TestCase
|
@@ -34,9 +38,25 @@ class TestRequest < Minitest::Unit::TestCase
|
|
34
38
|
assert_equal 'Bar', last_response.body
|
35
39
|
end
|
36
40
|
|
41
|
+
def test_headers_with_odd_format
|
42
|
+
get '/headers', {}, { 'HTTP_X-fu' => 'Bar' }
|
43
|
+
assert_equal 'Bar', last_response.body
|
44
|
+
end
|
45
|
+
|
37
46
|
def test_body
|
38
47
|
post '/body', {}, { 'rack.input' => StringIO.new('fubar') }
|
39
48
|
assert_equal 'fubar', last_response.body
|
40
49
|
end
|
41
50
|
|
51
|
+
def test_request_id
|
52
|
+
get '/request_id'
|
53
|
+
refute last_response.body.empty?
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_request_id_when_passed
|
57
|
+
request_id = '1234567890'
|
58
|
+
get '/request_id', {}, { 'HTTP_X_REQUEST_ID' => request_id }
|
59
|
+
assert_equal request_id, last_response.body
|
60
|
+
end
|
61
|
+
|
42
62
|
end
|
metadata
CHANGED
@@ -1,127 +1,127 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: newark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Evans
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: rack
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 1.5.2
|
20
14
|
requirement: !ruby/object:Gem::Requirement
|
21
15
|
requirements:
|
22
16
|
- - '>='
|
23
17
|
- !ruby/object:Gem::Version
|
24
18
|
version: 1.5.2
|
19
|
+
name: rack
|
25
20
|
prerelease: false
|
26
21
|
type: :runtime
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activesupport
|
29
22
|
version_requirements: !ruby/object:Gem::Requirement
|
30
23
|
requirements:
|
31
24
|
- - '>='
|
32
25
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
26
|
+
version: 1.5.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
34
28
|
requirement: !ruby/object:Gem::Requirement
|
35
29
|
requirements:
|
36
30
|
- - '>='
|
37
31
|
- !ruby/object:Gem::Version
|
38
32
|
version: '0'
|
33
|
+
name: activesupport
|
39
34
|
prerelease: false
|
40
35
|
type: :runtime
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: bundler
|
43
36
|
version_requirements: !ruby/object:Gem::Requirement
|
44
37
|
requirements:
|
45
|
-
- -
|
38
|
+
- - '>='
|
46
39
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
48
42
|
requirement: !ruby/object:Gem::Requirement
|
49
43
|
requirements:
|
50
44
|
- - ~>
|
51
45
|
- !ruby/object:Gem::Version
|
52
46
|
version: '1.5'
|
47
|
+
name: bundler
|
53
48
|
prerelease: false
|
54
49
|
type: :development
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
50
|
version_requirements: !ruby/object:Gem::Requirement
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
62
56
|
requirement: !ruby/object:Gem::Requirement
|
63
57
|
requirements:
|
64
58
|
- - '>='
|
65
59
|
- !ruby/object:Gem::Version
|
66
60
|
version: '0'
|
61
|
+
name: rake
|
67
62
|
prerelease: false
|
68
63
|
type: :development
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rack-test
|
71
64
|
version_requirements: !ruby/object:Gem::Requirement
|
72
65
|
requirements:
|
73
66
|
- - '>='
|
74
67
|
- !ruby/object:Gem::Version
|
75
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
76
70
|
requirement: !ruby/object:Gem::Requirement
|
77
71
|
requirements:
|
78
72
|
- - '>='
|
79
73
|
- !ruby/object:Gem::Version
|
80
74
|
version: '0'
|
75
|
+
name: rack-test
|
81
76
|
prerelease: false
|
82
77
|
type: :development
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: pry
|
85
78
|
version_requirements: !ruby/object:Gem::Requirement
|
86
79
|
requirements:
|
87
80
|
- - '>='
|
88
81
|
- !ruby/object:Gem::Version
|
89
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
90
84
|
requirement: !ruby/object:Gem::Requirement
|
91
85
|
requirements:
|
92
86
|
- - '>='
|
93
87
|
- !ruby/object:Gem::Version
|
94
88
|
version: '0'
|
89
|
+
name: pry
|
95
90
|
prerelease: false
|
96
91
|
type: :development
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: benchmark-ips
|
99
92
|
version_requirements: !ruby/object:Gem::Requirement
|
100
93
|
requirements:
|
101
94
|
- - '>='
|
102
95
|
- !ruby/object:Gem::Version
|
103
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
104
98
|
requirement: !ruby/object:Gem::Requirement
|
105
99
|
requirements:
|
106
100
|
- - '>='
|
107
101
|
- !ruby/object:Gem::Version
|
108
102
|
version: '0'
|
103
|
+
name: benchmark-ips
|
109
104
|
prerelease: false
|
110
105
|
type: :development
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: multi_json
|
113
106
|
version_requirements: !ruby/object:Gem::Requirement
|
114
107
|
requirements:
|
115
108
|
- - '>='
|
116
109
|
- !ruby/object:Gem::Version
|
117
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
118
112
|
requirement: !ruby/object:Gem::Requirement
|
119
113
|
requirements:
|
120
114
|
- - '>='
|
121
115
|
- !ruby/object:Gem::Version
|
122
116
|
version: '0'
|
117
|
+
name: multi_json
|
123
118
|
prerelease: false
|
124
119
|
type: :development
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
125
|
description: Because everyone should write their own framework.
|
126
126
|
email:
|
127
127
|
- mike@urlgonomics.com
|