class SwaggerYard::Specification

Attributes

authorizations[RW]

Public Class Methods

new(controller_path = SwaggerYard.config.controller_path, model_path = SwaggerYard.config.model_path) click to toggle source
# File lib/swagger_yard/specification.rb, line 5
def initialize(controller_path = SwaggerYard.config.controller_path,
               model_path = SwaggerYard.config.model_path)
  @model_paths = [*model_path].compact
  @controller_paths = [*controller_path].compact

  @resource_to_file_path = {}
  @authorizations = []
end

Public Instance Methods

model_objects() click to toggle source
# File lib/swagger_yard/specification.rb, line 25
def model_objects
  Hash[models.map {|m| [m.id, m]}]
end
path_objects() click to toggle source
# File lib/swagger_yard/specification.rb, line 14
def path_objects
  api_groups.map(&:paths).reduce(Paths.new({}), :merge).tap do |paths|
    warn_duplicate_operations(paths)
  end
end
security_objects() click to toggle source
# File lib/swagger_yard/specification.rb, line 29
def security_objects
  api_groups # triggers controller parsing in case it did not happen before
  Hash[authorizations.map {|auth| [auth.id, auth]}]
end
tag_objects() click to toggle source

Resources

# File lib/swagger_yard/specification.rb, line 21
def tag_objects
  api_groups.map(&:tag)
end

Private Instance Methods

api_groups() click to toggle source
# File lib/swagger_yard/specification.rb, line 39
def api_groups
  @api_groups ||= parse_controllers
end
models() click to toggle source
# File lib/swagger_yard/specification.rb, line 35
def models
  @models ||= parse_models
end
parse_controllers() click to toggle source
# File lib/swagger_yard/specification.rb, line 53
def parse_controllers
  @controller_paths.map do |controller_path|
    Dir[controller_path.to_s].map do |file_path|
      SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
        obj.tags.select {|t| t.tag_name == "authorization"}.each do |t|
          @authorizations << Authorization.from_yard_object(t)
        end
        ApiGroup.from_yard_object(obj)
      end
    end
  end.flatten.select(&:valid?)
end
parse_models() click to toggle source
# File lib/swagger_yard/specification.rb, line 43
def parse_models
  @model_paths.map do |model_path|
    Dir[model_path.to_s].map do |file_path|
      SwaggerYard.yard_class_objects_from_file(file_path).map do |obj|
        Model.from_yard_object(obj)
      end
    end
  end.flatten.compact.select(&:valid?)
end
warn_duplicate_operations(paths) click to toggle source
# File lib/swagger_yard/specification.rb, line 66
def warn_duplicate_operations(paths)
  operation_ids = []
  paths.path_items.each do |path,pi|
    pi.operations.each do |_, op|
      if operation_ids.include?(op.operation_id)
        SwaggerYard.log.warn("duplicate operation #{op.operation_id}")
        next
      end
      operation_ids << op.operation_id
    end
  end
end