Module: GrapeSwagger::DocMethods

Defined in:
lib/grape-swagger/doc_methods.rb,
lib/grape-swagger/doc_methods/headers.rb,
lib/grape-swagger/doc_methods/version.rb,
lib/grape-swagger/doc_methods/data_type.rb,
lib/grape-swagger/doc_methods/extensions.rb,
lib/grape-swagger/doc_methods/format_data.rb,
lib/grape-swagger/doc_methods/move_params.rb,
lib/grape-swagger/doc_methods/path_string.rb,
lib/grape-swagger/doc_methods/operation_id.rb,
lib/grape-swagger/doc_methods/parse_params.rb,
lib/grape-swagger/doc_methods/status_codes.rb,
lib/grape-swagger/doc_methods/optional_object.rb,
lib/grape-swagger/doc_methods/produces_consumes.rb,
lib/grape-swagger/doc_methods/tag_name_description.rb,
lib/grape-swagger/doc_methods/build_model_definition.rb

Defined Under Namespace

Classes: BuildModelDefinition, DataType, Extensions, FormatData, Headers, MoveParams, OperationId, OptionalObject, ParseParams, PathString, ProducesConsumes, StatusCodes, TagNameDescription, Version

Constant Summary collapse

DEFAULTS =
{
  info: {},
  models: [],
  doc_version: '0.0.1',
  target_class: nil,
  mount_path: '/swagger_doc',
  host: nil,
  base_path: nil,
  add_base_path: false,
  add_version: true,
  add_root: false,
  hide_documentation_path: true,
  format: :json,
  authorizations: nil,
  security_definitions: nil,
  security: nil,
  api_documentation: { desc: 'Swagger compatible API description' },
  specific_api_documentation: { desc: 'Swagger compatible API description for specific API' },
  endpoint_auth_wrapper: nil,
  swagger_endpoint_guard: nil,
  token_owner: nil
}.freeze
FORMATTER_METHOD =
%i[format default_format default_error_formatter].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.output_path_definitions(combi_routes, endpoint, target_class, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grape-swagger/doc_methods.rb', line 46

def self.output_path_definitions(combi_routes, endpoint, target_class, options)
  output = endpoint.swagger_object(
    target_class,
    endpoint.request,
    options
  )

  paths, definitions   = endpoint.path_and_definition_objects(combi_routes, options)
  tags                 = tags_from(paths, options)

  output[:tags]        = tags unless tags.empty? || paths.blank?
  output[:paths]       = paths unless paths.blank?
  output[:definitions] = definitions unless definitions.blank?

  output
end

.tags_from(paths, options) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/grape-swagger/doc_methods.rb', line 63

def self.tags_from(paths, options)
  tags = GrapeSwagger::DocMethods::TagNameDescription.build(paths)

  if options[:tags]
    names = options[:tags].map { |t| t[:name] }
    tags.reject! { |t| names.include?(t[:name]) }
    tags += options[:tags]
  end

  tags
end

Instance Method Details

#class_variables_from(options) ⇒ Object



129
130
131
132
133
# File 'lib/grape-swagger/doc_methods.rb', line 129

def class_variables_from(options)
  @@mount_path              = options[:mount_path]
  @@class_name              = options[:class_name] || options[:mount_path].delete('/')
  @@hide_documentation_path = options[:hide_documentation_path]
end

#hide_documentation_pathObject



75
76
77
# File 'lib/grape-swagger/doc_methods.rb', line 75

def hide_documentation_path
  @@hide_documentation_path
end

#mount_pathObject



79
80
81
# File 'lib/grape-swagger/doc_methods.rb', line 79

def mount_path
  @@mount_path
end

#setup(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/grape-swagger/doc_methods.rb', line 83

def setup(options)
  options = DEFAULTS.merge(options)

  # options could be set on #add_swagger_documentation call,
  # for available options see #defaults
  target_class     = options[:target_class]
  guard            = options[:swagger_endpoint_guard]
  api_doc          = options[:api_documentation].dup
  specific_api_doc = options[:specific_api_documentation].dup

  class_variables_from(options)

  setup_formatter(options[:format])

  desc api_doc.delete(:desc), api_doc

  instance_eval(guard) unless guard.nil?

  get mount_path do
    header['Access-Control-Allow-Origin']   = '*'
    header['Access-Control-Request-Method'] = '*'

    GrapeSwagger::DocMethods
      .output_path_definitions(target_class.combined_namespace_routes, self, target_class, options)
  end

  desc specific_api_doc.delete(:desc), { params: specific_api_doc.delete(:params) || {}, **specific_api_doc }

  params do
    requires :name, type: String, desc: 'Resource name of mounted API'
    optional :locale, type: Symbol, desc: 'Locale of API documentation'
  end

  instance_eval(guard) unless guard.nil?

  get "#{mount_path}/:name" do
    I18n.locale = params[:locale] || I18n.default_locale

    combined_routes = target_class.combined_namespace_routes[params[:name]]
    error!({ error: 'named resource not exist' }, 400) if combined_routes.nil?

    GrapeSwagger::DocMethods
      .output_path_definitions({ params[:name] => combined_routes }, self, target_class, options)
  end
end

#setup_formatter(formatter) ⇒ Object



135
136
137
138
139
# File 'lib/grape-swagger/doc_methods.rb', line 135

def setup_formatter(formatter)
  return unless formatter

  FORMATTER_METHOD.each { |method| send(method, formatter) }
end