Skip to content

Instantly share code, notes, and snippets.

@garo
Created February 24, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garo/0478d786cb075d1c0423 to your computer and use it in GitHub Desktop.
Save garo/0478d786cb075d1c0423 to your computer and use it in GitHub Desktop.
require "pp"
require "sinatra/base"
# A blank Sinatra app
class SinatraApp < Sinatra::Base
configure do
set :server, :puma
end
end
# I want a Wrapper class which wraps a block
class Wrapper
def initialize(&block)
@block = block
end
def get_proc()
block = @block
p = Proc.new { |*args|
block.call(*args)
}
return p
end
end
# I'm doing this Class.new stuff so that I can add routes dynamically to sinatra
# and also to support doing the wrapper in the middle
$app_class = Class.new(SinatraApp)
def add_get_route(path, &block)
$app_class.get(path, {}, &block)
end
add_get_route "/direct" do
puts "direct route was called"
return "direct result\n"
end
wrapper = Wrapper.new do
puts "wrapped route was called but this will throw a LocalJumpError - unexpected return"
return "wrapped result\n"
end
add_get_route("/wrapped", &wrapper.get_proc())
Rack::Server.start({
:app => $app_class.new,
:server => 'puma'
})
# Try both:
# curl http://localhost:8080/direct
# curl http://localhost:8080/wrapped
=begin
The wrapped call will throw this:
2016-02-24 12:35:20 - LocalJumpError - unexpected return:
test.rb:44:in `block in <main>'
test.rb:22:in `call'
test.rb:22:in `block in get_proc'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1610:in `call'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1610:in `block in compile!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `[]'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:994:in `route_eval'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1015:in `block in process_route'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1013:in `catch'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1013:in `process_route'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:973:in `block in route!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:972:in `each'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:972:in `route!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1085:in `block in dispatch!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `block in invoke'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `catch'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `invoke'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1082:in `dispatch!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:907:in `block in call!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `block in invoke'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `catch'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:1067:in `invoke'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:907:in `call!'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:895:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
.../2.2.0/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
.../2.2.0/gems/rack-1.6.4/lib/rack/nulllogger.rb:9:in `call'
.../2.2.0/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/show_exceptions.rb:25:in `call'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:182:in `call'
.../2.2.0/gems/sinatra-1.4.7/lib/sinatra/base.rb:2013:in `call'
.../2.2.0/gems/puma-2.16.0/lib/puma/server.rb:557:in `handle_request'
.../2.2.0/gems/puma-2.16.0/lib/puma/server.rb:404:in `process_client'
.../2.2.0/gems/puma-2.16.0/lib/puma/server.rb:270:in `block in run'
.../2.2.0/gems/puma-2.16.0/lib/puma/thread_pool.rb:106:in `call'
.../2.2.0/gems/puma-2.16.0/lib/puma/thread_pool.rb:106:in `block in spawn_thread'
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment