1717
1818module GrapeSwagger
1919 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+
2076 def hide_documentation_path
2177 @@hide_documentation_path
2278 end
@@ -26,54 +82,32 @@ def mount_path
2682 end
2783
2884 def setup ( options )
29- options = defaults . merge ( options )
85+ options = DEFAULTS . merge ( options )
3086
3187 # options could be set on #add_swagger_documentation call,
3288 # for available options see #defaults
3389 target_class = options [ :target_class ]
3490 guard = options [ :swagger_endpoint_guard ]
35- formatter = options [ :format ]
3691 api_doc = options [ :api_documentation ] . dup
3792 specific_api_doc = options [ :specific_api_documentation ] . dup
3893
3994 class_variables_from ( options )
4095
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 ] )
4697
4798 desc api_doc . delete ( :desc ) , api_doc
4899
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-
66100 instance_eval ( guard ) unless guard . nil?
67101
68102 get mount_path do
69103 header [ 'Access-Control-Allow-Origin' ] = '*'
70104 header [ 'Access-Control-Request-Method' ] = '*'
71105
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 )
73108 end
74109
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 }
77111
78112 params do
79113 requires :name , type : String , desc : 'Resource name of mounted API'
@@ -88,51 +122,21 @@ def setup(options)
88122 combined_routes = target_class . combined_namespace_routes [ params [ :name ] ]
89123 error! ( { error : 'named resource not exist' } , 400 ) if combined_routes . nil?
90124
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 )
92127 end
93128 end
94129
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-
120130 def class_variables_from ( options )
121131 @@mount_path = options [ :mount_path ]
122132 @@class_name = options [ :class_name ] || options [ :mount_path ] . delete ( '/' )
123133 @@hide_documentation_path = options [ :hide_documentation_path ]
124134 end
125135
126- def tags_from ( paths , options )
127- tags = GrapeSwagger :: DocMethods :: TagNameDescription . build ( paths )
136+ def setup_formatter ( formatter )
137+ return unless formatter
128138
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 ) }
136140 end
137141 end
138142end
0 commit comments