Class: Steppe::Service
- Inherits:
-
Object
- Object
- Steppe::Service
- Defined in:
- lib/steppe/service.rb
Defined Under Namespace
Classes: OpenAPISerializer, Server, Tag
Constant Summary collapse
- VERBS =
i[get post put patch delete].freeze
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#node_name ⇒ Object
readonly
Returns the value of attribute node_name.
-
#registered_security_schemes ⇒ Object
readonly
Returns the value of attribute registered_security_schemes.
-
#security_schemes ⇒ Object
readonly
Returns the value of attribute security_schemes.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#title ⇒ Object
Returns the value of attribute title.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#basic_auth(name, store: {}) ⇒ self
Register a Basic HTTP authentication security scheme.
-
#bearer_auth(name, store: {}, format: 'string') ⇒ self
Register a Bearer token authentication security scheme.
- #endpoints ⇒ Object
-
#initialize {|_self| ... } ⇒ Service
constructor
A new instance of Service.
-
#route_with(router) ⇒ Object
Registers all defined endpoints with the given router.
-
#security(scheme_name, scopes = []) ⇒ self
Apply a security requirement globally to endpoints defined after this call.
-
#security_scheme(scheme) ⇒ self
Register a security scheme for use in endpoints.
- #server(args = {}) ⇒ Object
-
#specs(path = '/') ⇒ Object
Generates an endpoint that serves the OpenAPI specification in JSON format.
- #tag(name, description: nil, external_docs: nil) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Service
Returns a new instance of Service.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/steppe/service.rb', line 26 def initialize(&) @lookup = {} @title = '' @description = '' @version = '0.0.1' @node_name = :service @servers = [] = [] @security_schemes = {} @registered_security_schemes = {} yield self if block_given? freeze end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
24 25 26 |
# File 'lib/steppe/service.rb', line 24 def description @description end |
#node_name ⇒ Object (readonly)
Returns the value of attribute node_name.
23 24 25 |
# File 'lib/steppe/service.rb', line 23 def node_name @node_name end |
#registered_security_schemes ⇒ Object (readonly)
Returns the value of attribute registered_security_schemes.
23 24 25 |
# File 'lib/steppe/service.rb', line 23 def registered_security_schemes @registered_security_schemes end |
#security_schemes ⇒ Object (readonly)
Returns the value of attribute security_schemes.
23 24 25 |
# File 'lib/steppe/service.rb', line 23 def security_schemes @security_schemes end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
23 24 25 |
# File 'lib/steppe/service.rb', line 23 def servers @servers end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
23 24 25 |
# File 'lib/steppe/service.rb', line 23 def end |
#title ⇒ Object
Returns the value of attribute title.
24 25 26 |
# File 'lib/steppe/service.rb', line 24 def title @title end |
#version ⇒ Object
Returns the value of attribute version.
24 25 26 |
# File 'lib/steppe/service.rb', line 24 def version @version end |
Instance Method Details
#[](name) ⇒ Object
40 |
# File 'lib/steppe/service.rb', line 40 def [](name) = @lookup[name] |
#basic_auth(name, store: {}) ⇒ self
Register a Basic HTTP authentication security scheme. This is a convenience method that creates a Basic auth scheme and registers it.
110 111 112 |
# File 'lib/steppe/service.rb', line 110 def basic_auth(name, store: {}) security_scheme Auth::Basic.new(name, store:) end |
#bearer_auth(name, store: {}, format: 'string') ⇒ self
Register a Bearer token authentication security scheme. This is a convenience method that creates a Bearer auth scheme and registers it.
73 74 75 |
# File 'lib/steppe/service.rb', line 73 def bearer_auth(name, store: {}, format: 'string') security_scheme Auth::Bearer.new(name, store:, format:) end |
#endpoints ⇒ Object
41 |
# File 'lib/steppe/service.rb', line 41 def endpoints = @lookup.values |
#route_with(router) ⇒ Object
Registers all defined endpoints with the given router. The router is expected to respond to HTTP verb methods (e.g., get, post). ie. router.get ‘/users/:id’, to: rack_endpoint
218 219 220 221 222 223 |
# File 'lib/steppe/service.rb', line 218 def route_with(router) endpoints.each do |endpoint| router.public_send(endpoint.verb, endpoint.path.to_s, to: endpoint.to_rack) end router end |
#security(scheme_name, scopes = []) ⇒ self
Apply a security requirement globally to endpoints defined after this call. This registers a security scheme with required scopes at the service level, making it apply to all endpoints defined after this method is called.
IMPORTANT: Order matters! This method only applies security to endpoints defined AFTER it is called, not to endpoints defined before.
172 173 174 175 176 |
# File 'lib/steppe/service.rb', line 172 def security(scheme_name, scopes = []) scheme = security_schemes.fetch(scheme_name) @registered_security_schemes[scheme_name] = scopes self end |
#security_scheme(scheme) ⇒ self
Register a security scheme for use in endpoints. Security schemes define authentication methods that can be applied to endpoints.
133 134 135 136 137 |
# File 'lib/steppe/service.rb', line 133 def security_scheme(scheme) scheme => Auth::SecuritySchemeInterface @security_schemes[scheme.name] = scheme self end |
#server(args = {}) ⇒ Object
43 44 45 46 |
# File 'lib/steppe/service.rb', line 43 def server(args = {}) @servers << Server.parse(args) self end |
#specs(path = '/') ⇒ Object
Generates an endpoint that serves the OpenAPI specification in JSON format.
195 196 197 198 199 200 |
# File 'lib/steppe/service.rb', line 195 def specs(path = '/') get :__open_api, path do |e| e.no_spec! e.json 200..299, OpenAPISerializer.new(self) end end |
#tag(name, description: nil, external_docs: nil) ⇒ Object
48 49 50 51 |
# File 'lib/steppe/service.rb', line 48 def tag(name, description: nil, external_docs: nil) << Tag.parse(name:, description:, external_docs:) self end |