1717
1818module GrapeSwagger
1919 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+
2075 def hide_documentation_path
2176 @@hide_documentation_path
2277 end
@@ -26,54 +81,32 @@ def mount_path
2681 end
2782
2883 def setup ( options )
29- options = defaults . merge ( options )
84+ options = DEFAULTS . merge ( options )
3085
3186 # options could be set on #add_swagger_documentation call,
3287 # for available options see #defaults
3388 target_class = options [ :target_class ]
3489 guard = options [ :swagger_endpoint_guard ]
35- formatter = options [ :format ]
3690 api_doc = options [ :api_documentation ] . dup
3791 specific_api_doc = options [ :specific_api_documentation ] . dup
3892
3993 class_variables_from ( options )
4094
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 ] )
4696
4797 desc api_doc . delete ( :desc ) , api_doc
4898
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-
6699 instance_eval ( guard ) unless guard . nil?
67100
68101 get mount_path do
69102 header [ 'Access-Control-Allow-Origin' ] = '*'
70103 header [ 'Access-Control-Request-Method' ] = '*'
71104
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 )
73107 end
74108
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 }
77110
78111 params do
79112 requires :name , type : String , desc : 'Resource name of mounted API'
@@ -88,51 +121,21 @@ def setup(options)
88121 combined_routes = target_class . combined_namespace_routes [ params [ :name ] ]
89122 error! ( { error : 'named resource not exist' } , 400 ) if combined_routes . nil?
90123
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 )
92126 end
93127 end
94128
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-
120129 def class_variables_from ( options )
121130 @@mount_path = options [ :mount_path ]
122131 @@class_name = options [ :class_name ] || options [ :mount_path ] . delete ( '/' )
123132 @@hide_documentation_path = options [ :hide_documentation_path ]
124133 end
125134
126- def tags_from ( paths , options )
127- tags = GrapeSwagger :: DocMethods :: TagNameDescription . build ( paths )
135+ def setup_formatter ( formatter )
136+ return unless formatter
128137
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 ) }
136139 end
137140 end
138141end
0 commit comments