module OpenApi::DSL

Public Instance Methods

_set_apis(api, routes, http) click to toggle source
# File lib/open_api/dsl.rb, line 51
def _set_apis(api, routes, http)
  routes.each do |route|
    path = oas[:apis][route[:path]] ||= { }
    (http || route[:http_verb]).split('|').each { |verb| path[verb] = api }
  end
  api
end
api(action, summary = '', id: nil, tag: nil, http: nil, dry: Config.default_run_dry, &block) click to toggle source
# File lib/open_api/dsl.rb, line 33
def api action, summary = '', id: nil, tag: nil, http: nil, dry: Config.default_run_dry, &block
  doc_tag if oas[:doc].blank?
  action_path = "#{oas[:route_base]}##{action}"
  routes = Router.routes_list[oas[:route_base]]
               &.select { |api| api[:action_path][/^#{action_path}$/].present? }
  return Tip.no_route(action_path) if routes.blank?

  tag = tag || oas[:doc][:tag][:name]
  api = Api.new(action_path, summary: summary, tags: [tag], id: id || "#{tag}_#{action.to_s.camelize}")
  [action, tag, :all].each { |key| api.dry_blocks.concat(oas[:dry_blocks][key] || [ ]) }
  api.run_dsl(dry: dry, &block)
  _set_apis(api, routes, http)
end
api_dry(action_or_tags = :all, &block) click to toggle source
# File lib/open_api/dsl.rb, line 47
def api_dry action_or_tags = :all, &block
  Array(action_or_tags).each { |a| (oas[:dry_blocks][a.to_sym] ||= [ ]) << block }
end
components(&block) click to toggle source
# File lib/open_api/dsl.rb, line 26
def components &block
  doc_tag if oas[:doc].blank?
  (components = Components.new).instance_exec(&block)
  components.process_objs
  (oas[:doc][:components] ||= { }).deep_merge!(components)
end
doc_tag(name: nil, **tag_info) click to toggle source

APIs will be grouped by tags.

# File lib/open_api/dsl.rb, line 22
def doc_tag name: nil, **tag_info #  description: ..., externalDocs: ...
  oas[:doc][:tag] = { name: name || oas[:tag_name], **tag_info }
end
oas() click to toggle source
# File lib/open_api/dsl.rb, line 11
def oas
  @oas ||= { doc: { }, dry_blocks: { }, apis: { }, route_base: try(:controller_path),
             tag_name: try(:controller_name)&.camelize }
end
route_base(path) click to toggle source
# File lib/open_api/dsl.rb, line 16
def route_base path
  oas[:route_base] = path
  oas[:tag_name] = path.split('/').last.camelize
end