17
17
18
18
module GrapeSwagger
19
19
module DocMethods
20
+ DEFAULTS =
21
+ {
22
+ info : { } ,
23
+ models : [ ] ,
24
+ doc_version : '0.0.1' ,
25
+ target_class : nil ,
26
+ mount_path : '/swagger_doc' ,
27
+ host : nil ,
28
+ base_path : nil ,
29
+ add_base_path : false ,
30
+ add_version : true ,
31
+ add_root : false ,
32
+ hide_documentation_path : true ,
33
+ format : :json ,
34
+ authorizations : nil ,
35
+ security_definitions : nil ,
36
+ security : nil ,
37
+ api_documentation : { desc : 'Swagger compatible API description' } ,
38
+ specific_api_documentation : { desc : 'Swagger compatible API description for specific API' } ,
39
+ endpoint_auth_wrapper : nil ,
40
+ swagger_endpoint_guard : nil ,
41
+ token_owner : nil
42
+ } . freeze
43
+
44
+ FORMATTER_METHOD = %i[ format default_format default_error_formatter ] . freeze
45
+
46
+ def self . output_path_definitions ( combi_routes , endpoint , target_class , options )
47
+ output = endpoint . swagger_object (
48
+ target_class ,
49
+ endpoint . request ,
50
+ options
51
+ )
52
+
53
+ paths , definitions = endpoint . path_and_definition_objects ( combi_routes , options )
54
+ tags = tags_from ( paths , options )
55
+
56
+ output [ :tags ] = tags unless tags . empty? || paths . blank?
57
+ output [ :paths ] = paths unless paths . blank?
58
+ output [ :definitions ] = definitions unless definitions . blank?
59
+
60
+ output
61
+ end
62
+
63
+ def self . tags_from ( paths , options )
64
+ tags = GrapeSwagger ::DocMethods ::TagNameDescription . build ( paths )
65
+
66
+ if options [ :tags ]
67
+ names = options [ :tags ] . map { |t | t [ :name ] }
68
+ tags . reject! { |t | names . include? ( t [ :name ] ) }
69
+ tags += options [ :tags ]
70
+ end
71
+
72
+ tags
73
+ end
74
+
20
75
def hide_documentation_path
21
76
@@hide_documentation_path
22
77
end
@@ -26,54 +81,32 @@ def mount_path
26
81
end
27
82
28
83
def setup ( options )
29
- options = defaults . merge ( options )
84
+ options = DEFAULTS . merge ( options )
30
85
31
86
# options could be set on #add_swagger_documentation call,
32
87
# for available options see #defaults
33
88
target_class = options [ :target_class ]
34
89
guard = options [ :swagger_endpoint_guard ]
35
- formatter = options [ :format ]
36
90
api_doc = options [ :api_documentation ] . dup
37
91
specific_api_doc = options [ :specific_api_documentation ] . dup
38
92
39
93
class_variables_from ( options )
40
94
41
- if formatter
42
- %i[ format default_format default_error_formatter ] . each do |method |
43
- send ( method , formatter )
44
- end
45
- end
95
+ setup_formatter ( options [ :format ] )
46
96
47
97
desc api_doc . delete ( :desc ) , api_doc
48
98
49
- output_path_definitions = proc do |combi_routes , endpoint |
50
- output = endpoint . swagger_object (
51
- target_class ,
52
- endpoint . request ,
53
- options
54
- )
55
-
56
- paths , definitions = endpoint . path_and_definition_objects ( combi_routes , options )
57
- tags = tags_from ( paths , options )
58
-
59
- output [ :tags ] = tags unless tags . empty? || paths . blank?
60
- output [ :paths ] = paths unless paths . blank?
61
- output [ :definitions ] = definitions unless definitions . blank?
62
-
63
- output
64
- end
65
-
66
99
instance_eval ( guard ) unless guard . nil?
67
100
68
101
get mount_path do
69
102
header [ 'Access-Control-Allow-Origin' ] = '*'
70
103
header [ 'Access-Control-Request-Method' ] = '*'
71
104
72
- output_path_definitions . call ( target_class . combined_namespace_routes , self )
105
+ GrapeSwagger ::DocMethods
106
+ . output_path_definitions ( target_class . combined_namespace_routes , self , target_class , options )
73
107
end
74
108
75
- desc specific_api_doc . delete ( :desc ) , { params :
76
- specific_api_doc . delete ( :params ) || { } } . merge ( specific_api_doc )
109
+ desc specific_api_doc . delete ( :desc ) , { params : specific_api_doc . delete ( :params ) || { } , **specific_api_doc }
77
110
78
111
params do
79
112
requires :name , type : String , desc : 'Resource name of mounted API'
@@ -88,51 +121,21 @@ def setup(options)
88
121
combined_routes = target_class . combined_namespace_routes [ params [ :name ] ]
89
122
error! ( { error : 'named resource not exist' } , 400 ) if combined_routes . nil?
90
123
91
- output_path_definitions . call ( { params [ :name ] => combined_routes } , self )
124
+ GrapeSwagger ::DocMethods
125
+ . output_path_definitions ( { params [ :name ] => combined_routes } , self , target_class , options )
92
126
end
93
127
end
94
128
95
- def defaults
96
- {
97
- info : { } ,
98
- models : [ ] ,
99
- doc_version : '0.0.1' ,
100
- target_class : nil ,
101
- mount_path : '/swagger_doc' ,
102
- host : nil ,
103
- base_path : nil ,
104
- add_base_path : false ,
105
- add_version : true ,
106
- add_root : false ,
107
- hide_documentation_path : true ,
108
- format : :json ,
109
- authorizations : nil ,
110
- security_definitions : nil ,
111
- security : nil ,
112
- api_documentation : { desc : 'Swagger compatible API description' } ,
113
- specific_api_documentation : { desc : 'Swagger compatible API description for specific API' } ,
114
- endpoint_auth_wrapper : nil ,
115
- swagger_endpoint_guard : nil ,
116
- token_owner : nil
117
- }
118
- end
119
-
120
129
def class_variables_from ( options )
121
130
@@mount_path = options [ :mount_path ]
122
131
@@class_name = options [ :class_name ] || options [ :mount_path ] . delete ( '/' )
123
132
@@hide_documentation_path = options [ :hide_documentation_path ]
124
133
end
125
134
126
- def tags_from ( paths , options )
127
- tags = GrapeSwagger :: DocMethods :: TagNameDescription . build ( paths )
135
+ def setup_formatter ( formatter )
136
+ return unless formatter
128
137
129
- if options [ :tags ]
130
- names = options [ :tags ] . map { |t | t [ :name ] }
131
- tags . reject! { |t | names . include? ( t [ :name ] ) }
132
- tags += options [ :tags ]
133
- end
134
-
135
- tags
138
+ FORMATTER_METHOD . each { |method | send ( method , formatter ) }
136
139
end
137
140
end
138
141
end
0 commit comments