Class: RubyAPI::App
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Instance Method Summary collapse
- #after(&hook) ⇒ Object
- #before(&hook) ⇒ Object
- #call(env) ⇒ Object
- #delete(path, **opts, &block) ⇒ Object
- #get(path, **opts, &block) ⇒ Object
- #group(prefix, &block) ⇒ Object
- #head(path, **opts, &block) ⇒ Object
-
#initialize(&block) ⇒ App
constructor
A new instance of App.
- #options(path, **opts, &block) ⇒ Object
- #patch(path, **opts, &block) ⇒ Object
- #plugin(name_or_klass, **opts) ⇒ Object
- #post(path, **opts, &block) ⇒ Object
- #put(path, **opts, &block) ⇒ Object
- #register(name, &block) ⇒ Object
- #route(method, path, **opts, &block) ⇒ Object
- #use(middleware, *args) ⇒ Object
- #websocket(path, &handler) ⇒ Object
Methods included from DI
Methods included from ErrorHandler
#error_handlers, extended, included, #rescue_from
Constructor Details
#initialize(&block) ⇒ App
Returns a new instance of App.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/rubyapi.rb', line 30 def initialize(&block) @router = Router.new @config = Config.new @container = Container.new @middlewares = [] @before_hooks = [] @after_hooks = [] @error_handlers = {} @plugins = [] instance_eval(&block) if block end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
28 29 30 |
# File 'lib/rubyapi.rb', line 28 def config @config end |
#container ⇒ Object (readonly)
Returns the value of attribute container.
28 29 30 |
# File 'lib/rubyapi.rb', line 28 def container @container end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
28 29 30 |
# File 'lib/rubyapi.rb', line 28 def router @router end |
Instance Method Details
#after(&hook) ⇒ Object
92 93 94 |
# File 'lib/rubyapi.rb', line 92 def after(&hook) @after_hooks << hook end |
#before(&hook) ⇒ Object
88 89 90 |
# File 'lib/rubyapi.rb', line 88 def before(&hook) @before_hooks << hook end |
#call(env) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/rubyapi.rb', line 114 def call(env) req = Rack::Request.new(env) path = req.path_info method = req.request_method.upcase if path == "/openapi.json" return serve_openapi end if path == "/docs" return serve_swagger_ui end route = @router.find(method, path) if route && route[:websocket] && env["HTTP_UPGRADE"]&.downcase == "websocket" return handle_websocket(env, route) end ctx = Context.new(req, route || dummy_route(req)) @before_hooks.each do |hook| hook.call(ctx) if ctx.response_status return error_response(ctx) end end return [404, { "content-type" => "application/json" }, ['{"error":"Not Found"}']] unless route ctx.apply_param_types! return error_response(ctx) if ctx.response_status if route[:body] ctx.validate_body!(route[:body]) return error_response(ctx) if ctx.response_status end session = Session.new(req, secret_key: @config.get(:secret_key)) ctx.session = session inject_deps(ctx, route) return error_response(ctx) if ctx.response_status begin result = route[:block].call(ctx) rescue => e handler_status = find_error_handler(e) if handler_status (ctx, session) return [handler_status, { "content-type" => "application/json" }, [JSON.generate({ error: e.class.name, message: e. })]] end raise e end @after_hooks.each { |hook| hook.call(ctx) } ctx.response_body = result if result && !ctx.response_body (ctx, session) serialize_response(ctx) end |
#delete(path, **opts, &block) ⇒ Object
58 59 60 |
# File 'lib/rubyapi.rb', line 58 def delete(path, **opts, &block) @router.add_route(:DELETE, path, **opts, &block) end |
#get(path, **opts, &block) ⇒ Object
42 43 44 |
# File 'lib/rubyapi.rb', line 42 def get(path, **opts, &block) @router.add_route(:GET, path, **opts, &block) end |
#group(prefix, &block) ⇒ Object
78 79 80 81 82 |
# File 'lib/rubyapi.rb', line 78 def group(prefix, &block) @router.push_prefix(prefix) instance_eval(&block) @router.pop_prefix(prefix) end |
#head(path, **opts, &block) ⇒ Object
66 67 68 |
# File 'lib/rubyapi.rb', line 66 def head(path, **opts, &block) @router.add_route(:HEAD, path, **opts, &block) end |
#options(path, **opts, &block) ⇒ Object
62 63 64 |
# File 'lib/rubyapi.rb', line 62 def (path, **opts, &block) @router.add_route(:OPTIONS, path, **opts, &block) end |
#patch(path, **opts, &block) ⇒ Object
54 55 56 |
# File 'lib/rubyapi.rb', line 54 def patch(path, **opts, &block) @router.add_route(:PATCH, path, **opts, &block) end |
#plugin(name_or_klass, **opts) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rubyapi.rb', line 96 def plugin(name_or_klass, **opts) klass = name_or_klass.is_a?(Class) ? name_or_klass : PluginRegistry.find(name_or_klass) return unless klass = klass..dup opts.each { |k, v| klass.option(k, v) } @plugins << klass klass.on_load(self) rescue => e klass..replace() if raise e end |
#post(path, **opts, &block) ⇒ Object
46 47 48 |
# File 'lib/rubyapi.rb', line 46 def post(path, **opts, &block) @router.add_route(:POST, path, **opts, &block) end |
#put(path, **opts, &block) ⇒ Object
50 51 52 |
# File 'lib/rubyapi.rb', line 50 def put(path, **opts, &block) @router.add_route(:PUT, path, **opts, &block) end |
#register(name, &block) ⇒ Object
110 111 112 |
# File 'lib/rubyapi.rb', line 110 def register(name, &block) @container.register(name, &block) end |
#route(method, path, **opts, &block) ⇒ Object
70 71 72 |
# File 'lib/rubyapi.rb', line 70 def route(method, path, **opts, &block) @router.add_route(method, path, **opts, &block) end |
#use(middleware, *args) ⇒ Object
84 85 86 |
# File 'lib/rubyapi.rb', line 84 def use(middleware, *args) @middlewares << [middleware, args] end |
#websocket(path, &handler) ⇒ Object
74 75 76 |
# File 'lib/rubyapi.rb', line 74 def websocket(path, &handler) @router.add_route(:GET, path, websocket: true, block: handler) end |