Module: GrapeSwagger::DocMethods

Defined in:
lib/grape-swagger/doc_methods.rb,
lib/grape-swagger/doc_methods/headers.rb,
lib/grape-swagger/doc_methods/data_type.rb,
lib/grape-swagger/doc_methods/extensions.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

Defined Under Namespace

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

Instance Method Summary collapse

Instance Method Details

#class_variables_from(options) ⇒ Object



104
105
106
107
108
# File 'lib/grape-swagger/doc_methods.rb', line 104

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

#defaultsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/grape-swagger/doc_methods.rb', line 80

def 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,
    markdown: false,
    hide_documentation_path: true,
    format: :json,
    authorizations: nil,
    security_definitions: 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,
    oauth_token: nil
  }
end

#hide_documentation_pathObject



20
21
22
# File 'lib/grape-swagger/doc_methods.rb', line 20

def hide_documentation_path
  @@hide_documentation_path
end

#mount_pathObject



24
25
26
# File 'lib/grape-swagger/doc_methods.rb', line 24

def mount_path
  @@mount_path
end

#nameObject



16
17
18
# File 'lib/grape-swagger/doc_methods.rb', line 16

def name
  @@class_name
end

#setup(options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/grape-swagger/doc_methods.rb', line 28

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]
  formatter        = options[:format]

  class_variables_from(options)

  [:format, :default_format, :default_error_formatter].each do |method|
    send(method, formatter)
  end if formatter

  send(guard.split.first.to_sym, *guard.split(/[\s,]+/).drop(1)) unless guard.nil?

  output_path_definitions = proc do |combi_routes, endpoint|
    output = endpoint.swagger_object(
      target_class,
      endpoint.request,
      options
    )

    paths, definitions = endpoint.path_and_definition_objects(combi_routes, options)
    output[:paths]       = paths unless paths.blank?
    output[:definitions] = definitions unless definitions.blank?

    output
  end

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

    output_path_definitions.call(target_class.combined_namespace_routes, self)
  end

  params do
    requires :name, type: String, desc: 'Resource name of mounted API'
    optional :locale, type: Symbol, desc: 'Locale of API documentation'
  end
  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?

    output_path_definitions.call({ params[:name] => combined_routes }, self)
  end
end