Module: SwaggerYard

Defined in:
lib/swagger_yard.rb,
lib/swagger_yard/type.rb,
lib/swagger_yard/model.rb,
lib/swagger_yard/example.rb,
lib/swagger_yard/openapi.rb,
lib/swagger_yard/swagger.rb,
lib/swagger_yard/version.rb,
lib/swagger_yard/property.rb,
lib/swagger_yard/api_group.rb,
lib/swagger_yard/operation.rb,
lib/swagger_yard/parameter.rb,
lib/swagger_yard/path_item.rb,
lib/swagger_yard/type_parser.rb,
lib/swagger_yard/authorization.rb,
lib/swagger_yard/configuration.rb,
lib/swagger_yard/specification.rb

Defined Under Namespace

Modules: Directives, Example, Handlers Classes: ApiGroup, Authorization, Configuration, Error, Info, InvalidTypeError, Model, OpenAPI, Operation, Parameter, PathItem, Paths, Property, Response, Specification, Swagger, Tag, Type, TypeParser, UndefinedSchemaError, UnknownConstant

Constant Summary collapse

VERSION =
"1.1.1"

Class Method Summary collapse

Class Method Details

.configObject



41
42
43
# File 'lib/swagger_yard.rb', line 41

def config
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Configuration for Swagger Yard, use like:

SwaggerYard.configure do |config|
  config.swagger_version = "1.1"
  config.api_version = "0.1"
  config.doc_base_path = "http://swagger.example.com/doc"
  config.api_base_path = "http://swagger.example.com/api"
  config.reload = true # Rails.env.development?
end

Yields:



37
38
39
# File 'lib/swagger_yard.rb', line 37

def configure
  yield config
end

.logObject



45
46
47
# File 'lib/swagger_yard.rb', line 45

def log
  YARD::Logger.instance
end

.register_custom_yard_tags!Object

Register some custom yard tags used by swagger-ui



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/swagger_yard.rb', line 104

def register_custom_yard_tags!
  ::YARD::Tags::Library.define_tag("Api resource", :resource)
  ::YARD::Tags::Library.define_tag("Api path", :path, :with_types)
  ::YARD::Tags::Library.define_tag("Parameter", :parameter, :with_types_name_and_default)
  ::YARD::Tags::Library.define_tag("Response type", :response_type, :with_types)
  ::YARD::Tags::Library.define_tag("Error response message", :error_message, :with_types_and_name)
  ::YARD::Tags::Library.define_tag("Response", :response, :with_types_and_name)
  ::YARD::Tags::Library.define_tag("Api Summary", :summary)
  ::YARD::Tags::Library.define_tag("Model resource", :model)
  ::YARD::Tags::Library.define_tag("Model superclass", :inherits)
  ::YARD::Tags::Library.define_tag("Model property", :property, :with_types_name_and_default)
  ::YARD::Tags::Library.define_tag("Model discriminator", :discriminator, :with_types_name_and_default)
  ::YARD::Tags::Library.define_tag("Additional properties", :additional_properties)
  ::YARD::Tags::Library.define_tag("Authorization", :authorization, :with_types_and_name)
  ::YARD::Tags::Library.define_tag("Authorization Use", :authorize_with)
  # @example is a core YARD tag, let's use it
  # ::YARD::Tags::Library.define_tag("Example", :example, :with_title_and_text)
  ::YARD::Tags::Library.define_directive(:model, :with_title_and_text, Directives::ParamClassDirective)
end

.requires_attrs(tag, *attrs) ⇒ Object

Validates that the tag has non-nil values for the given attribute methods. Logs a warning message and returns nil if the tag is not valid.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/swagger_yard.rb', line 51

def requires_attrs(tag, *attrs)
  valid = true
  attrs.each do |a|
    valid &&= tag.send(a)
    break unless valid
  end
  unless valid
    if tag.object
      object   = " in #{tag.object.to_s}"
      location = " near #{tag.object.files.first.join(':')}" if tag.object.files.first
    end
    log.warn "invalid @#{tag.tag_name} tag#{object}#{location}"
    return nil
  end
  tag
end

.requires_name(tag) ⇒ Object



68
69
70
# File 'lib/swagger_yard.rb', line 68

def requires_name(tag)
  requires_attrs(tag, :name)
end

.requires_name_and_type(tag) ⇒ Object



72
73
74
# File 'lib/swagger_yard.rb', line 72

def requires_name_and_type(tag)
  requires_attrs(tag, :name, :types)
end

.requires_type(tag) ⇒ Object



76
77
78
# File 'lib/swagger_yard.rb', line 76

def requires_type(tag)
  requires_attrs(tag, :types)
end

.yard_class_objects_from_file(file_path) ⇒ YARD

Parse all objects in the file and return the class objects found.

Parameters:

  • file_path (string)

    The complete path to file

Returns:

  • (YARD)

    objects representing classes from the file



98
99
100
# File 'lib/swagger_yard.rb', line 98

def yard_class_objects_from_file(file_path)
  yard_objects_from_file(file_path, :class)
end

.yard_objects_from_file(file_path, *types) ⇒ YARD

Use YARD to parse object tags from a file

Parameters:

  • file_path (string)

    The complete path to file

  • types

    additional types by which to filter the result (:class/:module/:method)

Returns:

  • (YARD)

    objects representing class/methods and tags from the file



87
88
89
90
# File 'lib/swagger_yard.rb', line 87

def yard_objects_from_file(file_path, *types)
  ::YARD.parse(file_path)
  ::YARD::Registry.all(*types).select {|co| co.file == file_path }
end