Class: OpenApi::DSL::Api
- Inherits:
-
Hash
- Object
- Hash
- OpenApi::DSL::Api
- Includes:
- Helpers
- Defined in:
- lib/open_api/dsl/api.rb
Instance Attribute Summary collapse
-
#_all_params ⇒ Object
Returns the value of attribute _all_params.
-
#action_path ⇒ Object
Returns the value of attribute action_path.
-
#dry_blocks ⇒ Object
Returns the value of attribute dry_blocks.
-
#dry_only ⇒ Object
Returns the value of attribute dry_only.
-
#dry_skip ⇒ Object
Returns the value of attribute dry_skip.
-
#dryed ⇒ Object
Returns the value of attribute dryed.
-
#param_order ⇒ Object
Returns the value of attribute param_order.
Instance Method Summary collapse
- #all_params ⇒ Object
- #body(media_type, data: { }) ⇒ Object
- #body!(media_type, data: { }) ⇒ Object
- #body_ref(component_key) ⇒ Object
- #callback(event_name, http_method, callback_url, &block) ⇒ Object
- #data(name, type = nil, schema = { }) ⇒ Object
- #desc(desc) ⇒ Object (also: #description)
- #dry(only: nil, skip: nil, none: false) ⇒ Object
- #form(data:) ⇒ Object
- #form!(data:) ⇒ Object
- #in_query(**params) ⇒ Object
- #in_query!(**params) ⇒ Object
-
#initialize(action_path = "", summary: nil, tags: [ ], id: nil) ⇒ Api
constructor
A new instance of Api.
- #json(data:) ⇒ Object
- #json!(data:) ⇒ Object
- #param(param_type, name, type, required, schema = { }) ⇒ Object (also: #parameter)
- #param_examples(exp_params = :all, examples_hash) ⇒ Object (also: #examples)
- #param_ref(component_key, *keys) ⇒ Object
- #query(name, type = nil, **schema) ⇒ Object
- #query!(name, type = nil, **schema) ⇒ Object
-
#request_body(required, media_type, data: { }, desc: "", **options) ⇒ Object
options: ‘exp_params` and `examples`.
- #response(code, desc, media_type = nil, headers: { }, data: { }) ⇒ Object (also: #resp, #error)
-
#response_ref(code_and_compkey) ⇒ Object
{ }.
- #run_dsl(dry: false, &block) ⇒ Object
- #security_require(scheme_name, scopes: [ ]) ⇒ Object (also: #security, #auth, #auth_with)
- #server(url, desc: "") ⇒ Object
- #this_api_is_invalid! ⇒ Object (also: #this_api_is_expired!, #this_api_is_unused!, #this_api_is_under_repair!)
Methods included from Helpers
#_combined_schema, #arrow_writing_support, #process_schema_input
Constructor Details
#initialize(action_path = "", summary: nil, tags: [ ], id: nil) ⇒ Api
Returns a new instance of Api.
13 14 15 16 17 18 19 20 21 |
# File 'lib/open_api/dsl/api.rb', line 13 def initialize(action_path = "", summary: nil, tags: [ ], id: nil) self.action_path = action_path self.dry_blocks = [ ] self._all_params = { } self.merge!( summary: summary, operationId: id, tags: , description: "", parameters: [ ], requestBody: nil, responses: { }, callbacks: { }, links: { }, security: [ ], servers: [ ] ) end |
Instance Attribute Details
#_all_params ⇒ Object
Returns the value of attribute _all_params.
10 11 12 |
# File 'lib/open_api/dsl/api.rb', line 10 def _all_params @_all_params end |
#action_path ⇒ Object
Returns the value of attribute action_path.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def action_path @action_path end |
#dry_blocks ⇒ Object
Returns the value of attribute dry_blocks.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def dry_blocks @dry_blocks end |
#dry_only ⇒ Object
Returns the value of attribute dry_only.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def dry_only @dry_only end |
#dry_skip ⇒ Object
Returns the value of attribute dry_skip.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def dry_skip @dry_skip end |
#dryed ⇒ Object
Returns the value of attribute dryed.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def dryed @dryed end |
#param_order ⇒ Object
Returns the value of attribute param_order.
11 12 13 |
# File 'lib/open_api/dsl/api.rb', line 11 def param_order @param_order end |
Instance Method Details
#all_params ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/open_api/dsl/api.rb', line 149 def all_params _all_params.transform_values do |t| if t.is_a?(Hash) t.key?(:type) ? t.merge!(type: t[:type].to_s.underscore) : { type: t } else { type: t.to_s.underscore } end end end |
#body(media_type, data: { }) ⇒ Object
83 |
# File 'lib/open_api/dsl/api.rb', line 83 def body(media_type, data: { }, **) = request_body(:optional, media_type, data:, **) |
#body!(media_type, data: { }) ⇒ Object
84 |
# File 'lib/open_api/dsl/api.rb', line 84 def body!(media_type, data: { }, **) = request_body(:required, media_type, data:, **) |
#body_ref(component_key) ⇒ Object
96 97 98 |
# File 'lib/open_api/dsl/api.rb', line 96 def body_ref(component_key) self[:requestBody] = RefObj.new(:requestBody, component_key) end |
#callback(event_name, http_method, callback_url, &block) ⇒ Object
122 123 124 125 126 |
# File 'lib/open_api/dsl/api.rb', line 122 def callback(event_name, http_method, callback_url, &block) self[:callbacks].deep_merge!( CallbackObj.new(event_name, http_method, callback_url, &block).process ) end |
#data(name, type = nil, schema = { }) ⇒ Object
91 92 93 94 |
# File 'lib/open_api/dsl/api.rb', line 91 def data(name, type = nil, schema = { }) schema[:type] = type if type.present? form data: { name => schema } end |
#desc(desc) ⇒ Object Also known as: description
30 31 32 |
# File 'lib/open_api/dsl/api.rb', line 30 def desc(desc) self[:description] = desc end |
#dry(only: nil, skip: nil, none: false) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/open_api/dsl/api.rb', line 35 def dry(only: nil, skip: nil, none: false) return if dry_blocks.blank? || dryed self.dry_skip = Array(skip).compact.presence self.dry_only = none ? [:none] : Array(only).compact.presence dry_blocks.each { |blk| instance_eval(&blk) } self.dry_skip = self.dry_only = nil self.dryed = true end |
#form(data:) ⇒ Object
88 |
# File 'lib/open_api/dsl/api.rb', line 88 def form(data:, **) = body(:form, data:, **) |
#form!(data:) ⇒ Object
89 |
# File 'lib/open_api/dsl/api.rb', line 89 def form!(data:, **) = body!(:form, data:, **) |
#in_query(**params) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/open_api/dsl/api.rb', line 60 i[ header header! path path! query query! ].each do |param_type| define_method(param_type) do |name, type = nil, **schema| param param_type, name, type, required?(param_type), schema end define_method("in_#{param_type}") do |params| params.each_pair do |param_name, schema| param param_type, param_name, nil, required?(param_type, param_name), schema end end end |
#in_query!(**params) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/open_api/dsl/api.rb', line 60 i[ header header! path path! query query! ].each do |param_type| define_method(param_type) do |name, type = nil, **schema| param param_type, name, type, required?(param_type), schema end define_method("in_#{param_type}") do |params| params.each_pair do |param_name, schema| param param_type, param_name, nil, required?(param_type, param_name), schema end end end |
#json(data:) ⇒ Object
86 |
# File 'lib/open_api/dsl/api.rb', line 86 def json(data:, **) = body(:json, data:, **) |
#json!(data:) ⇒ Object
87 |
# File 'lib/open_api/dsl/api.rb', line 87 def json!(data:, **) = body!(:json, data:, **) |
#param(param_type, name, type, required, schema = { }) ⇒ Object Also known as: parameter
45 46 47 48 49 50 51 52 53 |
# File 'lib/open_api/dsl/api.rb', line 45 def param(param_type, name, type, required, schema = { }) return if dry_skip&.include?(name) || dry_only&.exclude?(name) return unless (schema_obj = process_schema_input(type, schema, name)) _all_params[name] = schema.is_a?(Hash) ? schema.merge(type:) : { type: } unless param_type["header"] override_or_append_to_parameters( ParamObj.new(name, param_type, type, required, schema_obj) ) end |
#param_examples(exp_params = :all, examples_hash) ⇒ Object Also known as: examples
132 133 134 135 |
# File 'lib/open_api/dsl/api.rb', line 132 def param_examples(exp_params = :all, examples_hash) exp_params = self[:parameters].map(&:name) if exp_params == :all self[:examples] = ExampleObj.new(examples_hash, exp_params, multiple: true).process end |
#param_ref(component_key, *keys) ⇒ Object
72 73 74 |
# File 'lib/open_api/dsl/api.rb', line 72 def param_ref(component_key, *keys) self[:parameters] += [component_key, *keys].map { |key| RefObj.new(:parameter, key) } end |
#query(name, type = nil, **schema) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/open_api/dsl/api.rb', line 60 i[ header header! path path! query query! ].each do |param_type| define_method(param_type) do |name, type = nil, **schema| param param_type, name, type, required?(param_type), schema end define_method("in_#{param_type}") do |params| params.each_pair do |param_name, schema| param param_type, param_name, nil, required?(param_type, param_name), schema end end end |
#query!(name, type = nil, **schema) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/open_api/dsl/api.rb', line 60 i[ header header! path path! query query! ].each do |param_type| define_method(param_type) do |name, type = nil, **schema| param param_type, name, type, required?(param_type), schema end define_method("in_#{param_type}") do |params| params.each_pair do |param_name, schema| param param_type, param_name, nil, required?(param_type, param_name), schema end end end |
#request_body(required, media_type, data: { }, desc: "", **options) ⇒ Object
options: ‘exp_params` and `examples`
77 78 79 80 81 |
# File 'lib/open_api/dsl/api.rb', line 77 def request_body(required, media_type, data: { }, desc: "", **) self[:requestBody] ||= RequestBodyObj.new(required, desc) self[:requestBody].absorb(media_type, { data:, ** }) _all_params.merge!(data) end |
#response(code, desc, media_type = nil, headers: { }, data: { }) ⇒ Object Also known as: resp, error
100 101 102 103 |
# File 'lib/open_api/dsl/api.rb', line 100 def response(code, desc, media_type = nil, headers: { }, data: { }, **) self[:responses][code.to_s] ||= ResponseObj.new(desc) self[:responses][code.to_s].absorb(desc, media_type, headers:, data:, **) end |
#response_ref(code_and_compkey) ⇒ Object
{ }
108 109 110 111 112 |
# File 'lib/open_api/dsl/api.rb', line 108 def response_ref(code_and_compkey) # = { } code_and_compkey.each do |code, component_key| self[:responses][code.to_s] = RefObj.new(:response, component_key) end end |
#run_dsl(dry: false, &block) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/open_api/dsl/api.rb', line 139 def run_dsl(dry: false, &block) instance_exec(&block) if block_given? dry() if dry self[:parameters].map!(&:process) self[:requestBody] = self[:requestBody].try(:process) self[:responses] = self[:responses].transform_values(&:process).sort.to_h self.delete_if { |_, v| v.blank? } end |
#security_require(scheme_name, scopes: [ ]) ⇒ Object Also known as: security, auth, auth_with
114 115 116 |
# File 'lib/open_api/dsl/api.rb', line 114 def security_require(scheme_name, scopes: [ ]) self[:security] << { scheme_name => scopes } end |
#server(url, desc: "") ⇒ Object
128 129 130 |
# File 'lib/open_api/dsl/api.rb', line 128 def server(url, desc: "") self[:servers] << { url: url, description: desc } end |
#this_api_is_invalid! ⇒ Object Also known as: this_api_is_expired!, this_api_is_unused!, this_api_is_under_repair!
23 24 25 |
# File 'lib/open_api/dsl/api.rb', line 23 def this_api_is_invalid!(*) self[:deprecated] = true end |