Class: Tony::App
- Inherits:
-
Object
- Object
- Tony::App
- Defined in:
- lib/tony/app.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #error(block) ⇒ Object
- #get(path, block) ⇒ Object
-
#initialize(secret: nil) ⇒ App
constructor
A new instance of App.
- #not_found(block) ⇒ Object
- #post(path, block) ⇒ Object
Constructor Details
#initialize(secret: nil) ⇒ App
Returns a new instance of App.
6 7 8 9 |
# File 'lib/tony/app.rb', line 6 def initialize(secret: nil) @secret = secret @routes = Hash.new { |hash, key| hash[key] = {} } end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tony/app.rb', line 11 def call(env) req = Request.new(env, secret: @secret) resp = Response.new(secret: @secret) @routes[req.request_method].each_value { |route| next unless (match = route.match?(req.path)) req.params.merge!(match.named_captures) if match.is_a?(MatchData) req.params.symbolize_keys! begin run_block(route.block, req, resp) rescue Exception => error # rubocop:disable Lint/RescueException raise error unless @error_block resp.error = error resp.status = 500 run_block(@error_block, req, resp) end return resp.finish } resp.status = 404 run_block(@not_found_block, req, resp) if @not_found_block return resp.finish end |
#error(block) ⇒ Object
41 42 43 |
# File 'lib/tony/app.rb', line 41 def error(block) @error_block = block end |
#get(path, block) ⇒ Object
45 46 47 |
# File 'lib/tony/app.rb', line 45 def get(path, block) @routes['GET'][path] = Route.new(path, block) end |
#not_found(block) ⇒ Object
37 38 39 |
# File 'lib/tony/app.rb', line 37 def not_found(block) @not_found_block = block end |
#post(path, block) ⇒ Object
49 50 51 |
# File 'lib/tony/app.rb', line 49 def post(path, block) @routes['POST'][path] = Route.new(path, block) end |