Skip to content

Commit a9a29a4

Browse files
committed
Merge pull request #1201 from marshall-lee/rack_builder_used_right
Don't call builder on every request.
2 parents c1c4300 + c29c54b commit a9a29a4

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

.rubocop_todo.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Lint/NestedMethodDefinition:
1313

1414
# Offense count: 37
1515
Metrics/AbcSize:
16-
Max: 46
16+
Max: 51
1717

1818
# Offense count: 1
1919
Metrics/BlockNesting:
@@ -36,7 +36,7 @@ Metrics/LineLength:
3636
# Offense count: 42
3737
# Configuration parameters: CountComments.
3838
Metrics/MethodLength:
39-
Max: 35
39+
Max: 36
4040

4141
# Offense count: 8
4242
# Configuration parameters: CountComments.

lib/grape/endpoint.rb

+12-11
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,16 @@ def compile_path(prepared_path, anchor = true, requirements = {})
188188
end
189189

190190
def call(env)
191+
build_app
191192
dup.call!(env)
192193
end
193194

194195
def call!(env)
195196
extend helpers
196197

197198
env[Grape::Env::API_ENDPOINT] = self
198-
if options[:app]
199-
options[:app].call(env)
200-
else
201-
builder = build_middleware
202-
builder.run ->(arg) { run(arg) }
203-
builder.call(env)
204-
end
199+
@env = env
200+
@app.call(env)
205201
end
206202

207203
# Return the collection of endpoints within this endpoint.
@@ -216,9 +212,8 @@ def equals?(e)
216212

217213
protected
218214

219-
def run(env)
215+
def run
220216
ActiveSupport::Notifications.instrument('endpoint_run.grape', endpoint: self, env: env) do
221-
@env = env
222217
@header = {}
223218

224219
@request = Grape::Request.new(env)
@@ -262,7 +257,11 @@ def run(env)
262257
end
263258
end
264259

265-
def build_middleware
260+
def build_app
261+
@app ||= options[:app] || build_stack
262+
end
263+
264+
def build_stack
266265
b = Rack::Builder.new
267266

268267
b.use Rack::Head
@@ -303,7 +302,9 @@ def build_middleware
303302
formatters: Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(:formatters)),
304303
parsers: Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(:parsers))
305304

306-
b
305+
b.run ->(env) { env[Grape::Env::API_ENDPOINT].run }
306+
307+
b.to_app
307308
end
308309

309310
def helpers

0 commit comments

Comments
 (0)