Class: RubstApi::APIRouter
- Inherits:
-
Object
- Object
- RubstApi::APIRouter
- Defined in:
- lib/rubst_api/routing.rb
Direct Known Subclasses
Constant Summary collapse
- HTTP_METHODS =
%i[get post put patch delete options head trace].freeze
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
Returns the value of attribute dependencies.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add_api_route(path, endpoint = nil, methods: ["GET"], **options, &block) ⇒ Object
- #api_route(path, methods: ["GET"], **options, &block) ⇒ Object
- #include_router(router, prefix: "", tags: [], dependencies: []) ⇒ Object
-
#initialize(prefix: "", tags: [], dependencies: [], **defaults) ⇒ APIRouter
constructor
A new instance of APIRouter.
- #websocket(path, endpoint = nil, **options, &block) ⇒ Object
Constructor Details
#initialize(prefix: "", tags: [], dependencies: [], **defaults) ⇒ APIRouter
Returns a new instance of APIRouter.
46 47 48 49 |
# File 'lib/rubst_api/routing.rb', line 46 def initialize(prefix: "", tags: [], dependencies: [], **defaults) @prefix, @tags, @dependencies, @defaults = prefix, , dependencies, defaults @routes = [] end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
Returns the value of attribute dependencies.
44 45 46 |
# File 'lib/rubst_api/routing.rb', line 44 def dependencies @dependencies end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
44 45 46 |
# File 'lib/rubst_api/routing.rb', line 44 def prefix @prefix end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
44 45 46 |
# File 'lib/rubst_api/routing.rb', line 44 def routes @routes end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
44 45 46 |
# File 'lib/rubst_api/routing.rb', line 44 def @tags end |
Instance Method Details
#add_api_route(path, endpoint = nil, methods: ["GET"], **options, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/rubst_api/routing.rb', line 61 def add_api_route(path, endpoint = nil, methods: ["GET"], **, &block) callable = endpoint || block raise ArgumentError, "an endpoint block or callable is required" unless callable.respond_to?(:call) route = Route.new("#{prefix}#{path}", endpoint: callable, methods:, tags: + Array(.delete(:tags)), dependencies: dependencies + Array(.delete(:dependencies)), **@defaults, **) routes << route route end |
#api_route(path, methods: ["GET"], **options, &block) ⇒ Object
57 58 59 |
# File 'lib/rubst_api/routing.rb', line 57 def api_route(path, methods: ["GET"], **, &block) add_api_route(path, block, methods:, **) end |
#include_router(router, prefix: "", tags: [], dependencies: []) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubst_api/routing.rb', line 72 def include_router(router, prefix: "", tags: [], dependencies: []) router.routes.each do |route| routes << Route.new("#{prefix}#{route.path}", endpoint: route.endpoint, methods: route.methods, params: route.params, response_model: route.response_model, status_code: route.status_code, tags: + route., summary: route.summary, description: route.description, operation_id: route.operation_id, deprecated: route.deprecated, include_in_schema: route.include_in_schema, responses: route.responses, dependencies: dependencies + route.dependencies, name: route.name) end self end |
#websocket(path, endpoint = nil, **options, &block) ⇒ Object
85 86 87 |
# File 'lib/rubst_api/routing.rb', line 85 def websocket(path, endpoint = nil, **, &block) add_api_route(path, endpoint || block, methods: ["WEBSOCKET"], include_in_schema: false, **) end |