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
- #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
56 57 58 |
# File 'lib/rodauth/openapi/routes.rb', line 56 def description(text) data[:paths].values.last.values.last[:description] = text end |
#error_response(status = nil, description) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/rodauth/openapi/routes.rb', line 93 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
124 125 126 |
# File 'lib/rodauth/openapi/routes.rb', line 124 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
80 81 82 |
# File 'lib/rodauth/openapi/routes.rb', line 80 def html_response(description) response(200, description) end |
#json_response(description = "", example) ⇒ Object
84 85 86 |
# File 'lib/rodauth/openapi/routes.rb', line 84 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rodauth/openapi/routes.rb', line 60 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
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rodauth/openapi/routes.rb', line 101 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
114 115 116 117 118 119 120 121 122 |
# File 'lib/rodauth/openapi/routes.rb', line 114 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 |
#route(verb, name, summary, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# 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, 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
88 89 90 91 |
# File 'lib/rodauth/openapi/routes.rb', line 88 def success_response(description) status = json ? 200 : 302 response(status, description) end |