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