Class: Rodauth::OpenAPI::Routes
- Inherits:
-
Object
- Object
- Rodauth::OpenAPI::Routes
- Defined in:
- lib/rodauth/openapi/routes.rb
Constant Summary collapse
- FEATURES =
{}
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#rodauth ⇒ Object
readonly
Returns the value of attribute rodauth.
Class Method Summary collapse
Instance Method Summary collapse
- #description(text) ⇒ Object
- #error_response(status = nil, description) ⇒ Object
- #feature?(name) ⇒ Boolean
- #get ⇒ Object
- #html_response(description) ⇒ Object
-
#initialize(data, rodauth:, json:) ⇒ Routes
constructor
A new instance of Routes.
- #json_response(description = "", example) ⇒ Object
- #param(name, description, type:, example: nil, required: false, enum: nil) ⇒ Object
- #post ⇒ Object
- #redirect_error_response(name = nil, description) ⇒ Object
- #response(status, description = "", **fields) ⇒ Object
- #rodauth_invocation ⇒ Object
- #route(verb, name, summary, &block) ⇒ Object
- #section(tag, name) ⇒ Object
- #success_response(description) ⇒ Object
Constructor Details
#initialize(data, rodauth:, json:) ⇒ Routes
Returns a new instance of Routes.
15 16 17 18 19 |
# File 'lib/rodauth/openapi/routes.rb', line 15 def initialize(data, rodauth:, json:) @data = data @rodauth = rodauth @json = json end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/rodauth/openapi/routes.rb', line 13 def data @data end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
13 14 15 |
# File 'lib/rodauth/openapi/routes.rb', line 13 def json @json end |
#rodauth ⇒ Object (readonly)
Returns the value of attribute rodauth.
13 14 15 |
# File 'lib/rodauth/openapi/routes.rb', line 13 def rodauth @rodauth end |
Class Method Details
.define(name, tag, &definition) ⇒ Object
6 7 8 9 10 11 |
# File 'lib/rodauth/openapi/routes.rb', line 6 def self.define(name, tag, &definition) FEATURES[name] = -> do section tag, name instance_exec(&definition) end end |
Instance Method Details
#description(text) ⇒ Object
65 66 67 68 |
# File 'lib/rodauth/openapi/routes.rb', line 65 def description(text) path = data[:paths].values.last.values.last path[:description] = [path[:description], text].compact.join("\n") end |
#error_response(status = nil, description) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/rodauth/openapi/routes.rb', line 103 def error_response(status = nil, description) status = rodauth.send(:"#{status}_error_status") if status.is_a?(Symbol) if json && (status.nil? || !rodauth.json_response_custom_error_status?) status = rodauth.json_response_error_status end response(status, description) end |
#feature?(name) ⇒ Boolean
134 135 136 |
# File 'lib/rodauth/openapi/routes.rb', line 134 def feature?(name) rodauth.features.include?(name) end |
#get ⇒ Object
31 32 33 |
# File 'lib/rodauth/openapi/routes.rb', line 31 def get(...) route(:get, ...) unless json end |
#html_response(description) ⇒ Object
90 91 92 |
# File 'lib/rodauth/openapi/routes.rb', line 90 def html_response(description) response(200, description) end |
#json_response(description = "", example) ⇒ Object
94 95 96 |
# File 'lib/rodauth/openapi/routes.rb', line 94 def json_response(description = "", example) response(200, description, content: { "application/json" => { example: example } }) end |
#param(name, description, type:, example: nil, required: false, enum: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rodauth/openapi/routes.rb', line 70 def param(name, description, type:, example: nil, required: false, enum: nil) example ||= case type when :email then "[email protected]" when :password then "secret123" when :boolean then "true" when :token then "{account_id}#{rodauth.token_separator}{key_hmac}" end parameters = data[:paths].values.last.values.last[:parameters] parameters << { name: rodauth.send(:"#{name}_param"), in: "query", description: description, required: required, style: "form", example: example, schema: { type: type == :boolean ? "boolean" : "string", enum: enum }.compact, }.compact end |
#post ⇒ Object
35 36 37 |
# File 'lib/rodauth/openapi/routes.rb', line 35 def post(...) route(:post, ...) end |
#redirect_error_response(name = nil, description) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rodauth/openapi/routes.rb', line 111 def redirect_error_response(name = nil, description) status = if json if rodauth.json_response_custom_error_status? && name rodauth.send(:"#{name}_error_status") else rodauth.json_response_error_status end else 302 end response(status, description) end |
#response(status, description = "", **fields) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/rodauth/openapi/routes.rb', line 124 def response(status, description = "", **fields) responses = data[:paths].values.last.values.last[:responses] if responses[status] responses[status][:description] = [responses[status][:description], description].join(", ") else responses[status] = { description: description } end responses[status].merge!(fields) end |
#rodauth_invocation ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/rodauth/openapi/routes.rb', line 138 def rodauth_invocation if rodauth.class.configuration_name "rodauth(:#{rodauth.class.configuration_name})" else "rodauth" end end |
#route(verb, name, summary, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rodauth/openapi/routes.rb', line 39 def route(verb, name, summary, &block) path = rodauth.send(:"#{name}_path") return if path.nil? tag = data[:tags].last[:name] data[:paths][path] ||= {} data[:paths][path][verb] = { tags: [tag], summary: summary, description: <<~MARKDOWN, ```ruby #{rodauth_invocation}.#{name}_route #=> "#{rodauth.send(:"#{name}_route")}" #{rodauth_invocation}.#{name}_path #=> "#{path}" #{rodauth_invocation}.#{name}_url #=> "https://example.com#{path}" #{rodauth_invocation}.current_route #=> :#{name} (in the request) ``` MARKDOWN responses: {}, parameters: [], } instance_exec(&block) end |
#section(tag, name) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/rodauth/openapi/routes.rb', line 21 def section(tag, name) data[:tags] << { name: tag, externalDocs: { description: "Feature documentation", url: "http://rodauth.jeremyevans.net/rdoc/files/doc/#{name}_rdoc.html" } } end |
#success_response(description) ⇒ Object
98 99 100 101 |
# File 'lib/rodauth/openapi/routes.rb', line 98 def success_response(description) status = json ? 200 : 302 response(status, description) end |