kenji 1.1.1 → 1.1.2
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 +7 -0
- data/.gitignore +0 -1
- data/Gemfile.lock +39 -0
- data/README.md +13 -2
- data/bin/kenji +0 -0
- data/lib/kenji/controller.rb +4 -1
- data/lib/kenji/version.rb +1 -1
- data/lib/kenji.rb +29 -38
- data/spec/1/controllers/main.rb +14 -0
- data/spec/kenji_spec.rb +11 -12
- metadata +19 -30
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cf14e401da97852b1581ed7c36fecb4ca5730150
|
4
|
+
data.tar.gz: 640b48b7bd7caaedd92c7a7071c74b09917c8cb2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec8185bb579998a5108aa4059d7c229500fb0340ef098c5888897d76a3e66166939d1e99052f0071b9fae161c12d5675c9f44747a5753a053bfed08b8b8dbe4f
|
7
|
+
data.tar.gz: 8074e6d539c044abbb5bc031cb6f9a3c5978ed81440ea3482574f26663ff08e785b591f9458c60835379b4cc49a86ac814ef4971da4e9885b3f216189f78faef
|
data/.gitignore
CHANGED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
kenji (1.1.1)
|
5
|
+
json
|
6
|
+
rack (~> 1.5.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
inifile (2.0.2)
|
13
|
+
json (1.8.1)
|
14
|
+
rack (1.5.2)
|
15
|
+
rack-test (0.6.2)
|
16
|
+
rack (>= 1.0)
|
17
|
+
rake (10.3.2)
|
18
|
+
rspec (3.0.0)
|
19
|
+
rspec-core (~> 3.0.0)
|
20
|
+
rspec-expectations (~> 3.0.0)
|
21
|
+
rspec-mocks (~> 3.0.0)
|
22
|
+
rspec-core (3.0.3)
|
23
|
+
rspec-support (~> 3.0.0)
|
24
|
+
rspec-expectations (3.0.3)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.0.0)
|
27
|
+
rspec-mocks (3.0.3)
|
28
|
+
rspec-support (~> 3.0.0)
|
29
|
+
rspec-support (3.0.3)
|
30
|
+
|
31
|
+
PLATFORMS
|
32
|
+
ruby
|
33
|
+
|
34
|
+
DEPENDENCIES
|
35
|
+
inifile
|
36
|
+
kenji!
|
37
|
+
rack-test
|
38
|
+
rake
|
39
|
+
rspec
|
data/README.md
CHANGED
@@ -100,12 +100,23 @@ And already, your app is ready to go:
|
|
100
100
|
|
101
101
|
## Changelog
|
102
102
|
|
103
|
+
#### 1.1.2
|
104
|
+
|
105
|
+
- The `respond_raw` method allows responding with raw data, instead of the
|
106
|
+
default which serializes the response as a JSON object.
|
107
|
+
- Supports PATCH requests natively.
|
108
|
+
- Remove support for `auto_cors`. Instead of having Kenji implement it
|
109
|
+
automatically, use a middleware like [Rack::Cors][rack-cors] which does
|
110
|
+
a better job than the barebones implementation in Kenji does.
|
111
|
+
|
112
|
+
[rack-cors]: https://github.com/cyu/rack-cors
|
113
|
+
|
103
114
|
#### 1.1.1
|
104
115
|
|
105
|
-
- No longer catching ArgumentErrors when calling the block for a route.
|
116
|
+
- No longer catching ArgumentErrors when calling the block for a route. This
|
106
117
|
fixes a bug where Kenji incorrectly responds with a 404 when the block is
|
107
118
|
passed the wrong number of arguments.
|
108
|
-
- Fixed logic for matching a pass.
|
119
|
+
- Fixed logic for matching a pass. the path now must match the pass exactly
|
109
120
|
whereas before the pass would match if any subset of the path matched the
|
110
121
|
pass.
|
111
122
|
|
data/bin/kenji
CHANGED
File without changes
|
data/lib/kenji/controller.rb
CHANGED
@@ -35,7 +35,10 @@ module Kenji
|
|
35
35
|
route(:delete, path, &block)
|
36
36
|
end
|
37
37
|
|
38
|
-
#
|
38
|
+
# Route PATCH
|
39
|
+
def self.patch(path, &block)
|
40
|
+
route(:patch, path, &block)
|
41
|
+
end
|
39
42
|
|
40
43
|
# Route all methods for given path
|
41
44
|
def self.all(path, &block)
|
data/lib/kenji/version.rb
CHANGED
data/lib/kenji.rb
CHANGED
@@ -27,10 +27,6 @@ module Kenji
|
|
27
27
|
#
|
28
28
|
# `options` is an options hash that accepts the following keys:
|
29
29
|
#
|
30
|
-
# :auto_cors => true | false
|
31
|
-
#
|
32
|
-
# automatically deal with CORS / Access-Control
|
33
|
-
#
|
34
30
|
# :directory => String (path)
|
35
31
|
#
|
36
32
|
# this is the preferred way of setting the root directory, when
|
@@ -71,7 +67,6 @@ module Kenji
|
|
71
67
|
options[:directory] = File.expand_path(root) if root
|
72
68
|
|
73
69
|
@options = {
|
74
|
-
auto_cors: true,
|
75
70
|
catch_exceptions: true,
|
76
71
|
root_controller: nil,
|
77
72
|
directory: File.expand_path(Dir.getwd),
|
@@ -86,8 +81,6 @@ module Kenji
|
|
86
81
|
#
|
87
82
|
def call
|
88
83
|
|
89
|
-
auto_cors if @options[:auto_cors]
|
90
|
-
|
91
84
|
catch(:KenjiRespondControlFlowInterrupt) do
|
92
85
|
path = @env['PATH_INFO']
|
93
86
|
|
@@ -100,7 +93,8 @@ module Kenji
|
|
100
93
|
method = @env['REQUEST_METHOD'].downcase.to_sym
|
101
94
|
|
102
95
|
segments = path.split('/')
|
103
|
-
|
96
|
+
# ensure existence of leading /'s empty segment
|
97
|
+
segments = segments.unshift('') unless segments.first == ''
|
104
98
|
|
105
99
|
if @options[:root_controller]
|
106
100
|
controller = controller_instance(@options[:root_controller])
|
@@ -111,7 +105,8 @@ module Kenji
|
|
111
105
|
acc = ''; out = '', success = false
|
112
106
|
while head = segments.shift
|
113
107
|
acc = "#{acc}/#{head}"
|
114
|
-
|
108
|
+
# if we have a valid controller
|
109
|
+
if controller = controller_for(acc)
|
115
110
|
subpath = '/'+segments.join('/')
|
116
111
|
out = controller.call(method, subpath).to_json
|
117
112
|
success = true
|
@@ -126,21 +121,12 @@ module Kenji
|
|
126
121
|
end
|
127
122
|
rescue Exception => e
|
128
123
|
raise e unless @options[:catch_exceptions]
|
129
|
-
|
124
|
+
# log exceptions
|
125
|
+
@stderr.puts e.inspect
|
130
126
|
e.backtrace.each {|b| @stderr.puts " #{b}" }
|
131
127
|
response_500
|
132
128
|
end
|
133
129
|
|
134
|
-
# 500 error
|
135
|
-
def response_500
|
136
|
-
[500, @headers, [{status: 500, message: 'Something went wrong...'}.to_json]]
|
137
|
-
end
|
138
|
-
|
139
|
-
# 404 error
|
140
|
-
def response_404
|
141
|
-
[404, @headers, [{status: 404, message: 'Not found!'}.to_json]]
|
142
|
-
end
|
143
|
-
|
144
130
|
|
145
131
|
|
146
132
|
# Methods for users!
|
@@ -180,33 +166,35 @@ module Kenji
|
|
180
166
|
#
|
181
167
|
def respond(code, message, hash={})
|
182
168
|
@status = code
|
183
|
-
response = { # default structure.
|
184
|
-
:
|
185
|
-
:
|
169
|
+
response = { # default structure.
|
170
|
+
status: code,
|
171
|
+
message: message,
|
186
172
|
}.merge(hash)
|
187
173
|
throw(:KenjiRespondControlFlowInterrupt, [@status, @headers, [response.to_json]])
|
188
174
|
end
|
189
175
|
|
176
|
+
# Respond with raw bytes
|
177
|
+
#
|
178
|
+
# `data` can either be a string or an IO object.
|
179
|
+
#
|
180
|
+
def respond_raw(status = 200, data)
|
181
|
+
@status = status
|
182
|
+
body = data.is_a?(IO) ? data : [data]
|
183
|
+
throw(:KenjiRespondControlFlowInterrupt, [@status, @headers, data])
|
184
|
+
end
|
190
185
|
|
191
186
|
|
192
187
|
# Private methods
|
193
188
|
private
|
194
189
|
|
195
|
-
#
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
origin = env['HTTP_ORIGIN']
|
200
|
-
header 'Access-Control-Allow-Origin' => origin if origin
|
201
|
-
|
202
|
-
if env['REQUEST_METHOD'] == 'OPTIONS'
|
203
|
-
header 'Access-Control-Allow-Methods' => 'OPTIONS, GET, POST, PUT, DELETE'
|
190
|
+
# 500 error
|
191
|
+
def response_500
|
192
|
+
[500, @headers, [{status: 500, message: 'Something went wrong...'}.to_json]]
|
193
|
+
end
|
204
194
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
respond(200, 'CORS is allowed.')
|
209
|
-
end
|
195
|
+
# 404 error
|
196
|
+
def response_404
|
197
|
+
[404, @headers, [{status: 404, message: 'Not found!'}.to_json]]
|
210
198
|
end
|
211
199
|
|
212
200
|
# Will attempt to fetch the controller, and verify that it is a implements
|
@@ -227,7 +215,10 @@ module Kenji
|
|
227
215
|
#
|
228
216
|
def controller_instance(controller_class)
|
229
217
|
# ensure protocol compliance
|
230
|
-
|
218
|
+
unless (controller_class.method_defined?(:call) &&
|
219
|
+
controller_class.instance_method(:call).arity == 2)
|
220
|
+
return
|
221
|
+
end
|
231
222
|
controller = controller_class.new
|
232
223
|
controller.kenji = self if controller.respond_to?(:kenji=)
|
233
224
|
return controller if controller
|
data/spec/1/controllers/main.rb
CHANGED
@@ -21,8 +21,22 @@ class MainController < Kenji::Controller
|
|
21
21
|
{status:1337}
|
22
22
|
end
|
23
23
|
|
24
|
+
patch '/' do
|
25
|
+
{status:1337}
|
26
|
+
end
|
27
|
+
|
24
28
|
get '/respond' do
|
25
29
|
kenji.respond(123, 'hello')
|
26
30
|
raise # never called
|
27
31
|
end
|
32
|
+
|
33
|
+
get '/respond-raw' do
|
34
|
+
kenji.respond_raw('hello raw')
|
35
|
+
raise # never called
|
36
|
+
end
|
37
|
+
|
38
|
+
get '/respond-io' do
|
39
|
+
kenji.respond_raw(123, StringIO.new('hello io'))
|
40
|
+
raise # never called
|
41
|
+
end
|
28
42
|
end
|
data/spec/kenji_spec.rb
CHANGED
@@ -51,7 +51,7 @@ describe Kenji::Kenji, 'expected responses' do
|
|
51
51
|
last_response.body.should == expected_response
|
52
52
|
end
|
53
53
|
|
54
|
-
[:post, :put, :delete].each do |method|
|
54
|
+
[:post, :put, :delete, :patch].each do |method|
|
55
55
|
|
56
56
|
it "should route a #{method.to_s.upcase} to a defined #{method.to_s} call" do
|
57
57
|
send(method, '/main')
|
@@ -74,19 +74,18 @@ describe Kenji::Kenji, 'expected responses' do
|
|
74
74
|
last_response.status.should == 123
|
75
75
|
end
|
76
76
|
|
77
|
-
it 'should
|
78
|
-
|
79
|
-
|
80
|
-
last_response.
|
77
|
+
it 'should use respond immediately with kenji.respond_raw' do
|
78
|
+
get '/main/respond-raw'
|
79
|
+
expected_response = 'hello raw'
|
80
|
+
last_response.body.should == expected_response
|
81
|
+
last_response.status.should == 200
|
81
82
|
end
|
82
83
|
|
83
|
-
it 'should
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
last_response.
|
88
|
-
last_response.header['Access-Control-Allow-Methods'].should == 'OPTIONS, GET, POST, PUT, DELETE'
|
89
|
-
last_response.header['Access-Control-Allow-Headers'].should == 'Bar'
|
84
|
+
it 'should use respond immediately with kenji.respond_raw is passed an IO' do
|
85
|
+
get '/main/respond-io'
|
86
|
+
expected_response = 'hello io'
|
87
|
+
last_response.body.should == expected_response
|
88
|
+
last_response.status.should == 123
|
90
89
|
end
|
91
90
|
|
92
91
|
end
|
metadata
CHANGED
@@ -1,94 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kenji
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kenneth Ballenegger
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-08-09 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: json
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rack
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- - ~>
|
31
|
+
- - "~>"
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: 1.5.2
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- - ~>
|
38
|
+
- - "~>"
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: 1.5.2
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
|
-
- -
|
45
|
+
- - ">="
|
52
46
|
- !ruby/object:Gem::Version
|
53
47
|
version: '0'
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
|
-
- -
|
52
|
+
- - ">="
|
60
53
|
- !ruby/object:Gem::Version
|
61
54
|
version: '0'
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: rspec
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
|
-
- -
|
59
|
+
- - ">="
|
68
60
|
- !ruby/object:Gem::Version
|
69
61
|
version: '0'
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
|
-
- -
|
66
|
+
- - ">="
|
76
67
|
- !ruby/object:Gem::Version
|
77
68
|
version: '0'
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rack-test
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
|
-
- -
|
73
|
+
- - ">="
|
84
74
|
- !ruby/object:Gem::Version
|
85
75
|
version: '0'
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
|
-
- -
|
80
|
+
- - ">="
|
92
81
|
- !ruby/object:Gem::Version
|
93
82
|
version: '0'
|
94
83
|
description: A lightweight Ruby web framework.
|
@@ -99,8 +88,9 @@ executables:
|
|
99
88
|
extensions: []
|
100
89
|
extra_rdoc_files: []
|
101
90
|
files:
|
102
|
-
- .gitignore
|
91
|
+
- ".gitignore"
|
103
92
|
- Gemfile
|
93
|
+
- Gemfile.lock
|
104
94
|
- LICENSE
|
105
95
|
- README.md
|
106
96
|
- Rakefile
|
@@ -132,27 +122,26 @@ files:
|
|
132
122
|
- spec/kenji_spec.rb
|
133
123
|
homepage: https://github.com/kballenegger/kenji
|
134
124
|
licenses: []
|
125
|
+
metadata: {}
|
135
126
|
post_install_message:
|
136
127
|
rdoc_options: []
|
137
128
|
require_paths:
|
138
129
|
- lib
|
139
130
|
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
-
none: false
|
141
131
|
requirements:
|
142
|
-
- -
|
132
|
+
- - ">="
|
143
133
|
- !ruby/object:Gem::Version
|
144
134
|
version: '0'
|
145
135
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
-
none: false
|
147
136
|
requirements:
|
148
|
-
- -
|
137
|
+
- - ">="
|
149
138
|
- !ruby/object:Gem::Version
|
150
139
|
version: '0'
|
151
140
|
requirements: []
|
152
141
|
rubyforge_project:
|
153
|
-
rubygems_version:
|
142
|
+
rubygems_version: 2.2.2
|
154
143
|
signing_key:
|
155
|
-
specification_version:
|
144
|
+
specification_version: 4
|
156
145
|
summary: Kenji
|
157
146
|
test_files:
|
158
147
|
- spec/1/controllers/main.rb
|