@@ -7,122 +7,129 @@ module DSL
7
7
class Api < Hash
8
8
include DSL ::Helpers
9
9
10
+ attr_accessor :_all_params
10
11
attr_accessor :action_path , :dry_skip , :dry_only , :dry_blocks , :dryed , :param_order
11
12
12
- def initialize ( action_path = '' , summary : nil , tags : [ ] , id : nil )
13
+ def initialize ( action_path = "" , summary : nil , tags : [ ] , id : nil )
13
14
self . action_path = action_path
14
15
self . dry_blocks = [ ]
15
- self . merge! ( summary : summary , operationId : id , tags : tags , description : '' , parameters : [ ] ,
16
- requestBody : nil , responses : { } , callbacks : { } , links : { } , security : [ ] , servers : [ ] )
16
+ self . _all_params = { }
17
+ self . merge! (
18
+ summary : summary , operationId : id , tags : tags , description : "" , parameters : [ ] ,
19
+ requestBody : nil , responses : { } , callbacks : { } , links : { } , security : [ ] , servers : [ ]
20
+ )
17
21
end
18
22
19
23
def this_api_is_invalid! ( *)
20
24
self [ :deprecated ] = true
21
25
end
22
-
23
26
alias this_api_is_expired! this_api_is_invalid!
24
27
alias this_api_is_unused! this_api_is_invalid!
25
28
alias this_api_is_under_repair! this_api_is_invalid!
26
29
27
- def desc desc
30
+ def desc ( desc )
28
31
self [ :description ] = desc
29
32
end
30
-
31
33
alias description desc
32
34
33
- def dry only : nil , skip : nil , none : false
35
+ def dry ( only : nil , skip : nil , none : false )
34
36
return if dry_blocks . blank? || dryed
35
- self . dry_skip = skip && Array ( skip )
36
- self . dry_only = none ? [ :none ] : only && Array ( only )
37
+
38
+ self . dry_skip = Array ( skip ) . compact . presence
39
+ self . dry_only = none ? [ :none ] : Array ( only ) . compact . presence
37
40
dry_blocks . each { |blk | instance_eval ( &blk ) }
38
41
self . dry_skip = self . dry_only = nil
39
42
self . dryed = true
40
43
end
41
44
42
- def param param_type , name , type , required , schema = { }
45
+ def param ( param_type , name , type , required , schema = { } )
43
46
return if dry_skip &.include? ( name ) || dry_only &.exclude? ( name )
47
+ return unless ( schema_obj = process_schema_input ( type , schema , name ) )
44
48
45
- return unless schema = process_schema_input ( type , schema , name )
46
- param_obj = ParamObj . new ( name , param_type , type , required , schema )
47
- # The definition of the same name parameter will be overwritten
48
- index = self [ :parameters ] . map ( &:name ) . index ( param_obj . name )
49
- index ? self [ :parameters ] [ index ] = param_obj : self [ :parameters ] << param_obj
49
+ _all_params [ name ] = schema . is_a? ( Hash ) ? schema . merge ( type :) : { type : } unless param_type [ "header" ]
50
+ override_or_append_to_parameters (
51
+ ParamObj . new ( name , param_type , type , required , schema_obj )
52
+ )
50
53
end
51
-
52
54
alias parameter param
53
55
56
+ # @!method query(name, type = nil, **schema)
57
+ # @!method query!(name, type = nil, **schema)
58
+ # @!method in_query(**params)
59
+ # @!method in_query!(**params)
54
60
%i[ header header! path path! query query! cookie cookie! ] . each do |param_type |
55
- define_method param_type do |name , type = nil , **schema |
56
- param param_type , name , type , ( param_type [ '!' ] ? :req : :opt ) , schema
61
+ define_method ( param_type ) do |name , type = nil , **schema |
62
+ param param_type , name , type , required? ( param_type ) , schema
57
63
end
58
64
59
- define_method "in_#{ param_type } " do |params |
65
+ define_method ( "in_#{ param_type } " ) do |params |
60
66
params . each_pair do |param_name , schema |
61
- param param_type , param_name , nil , ( param_type [ '!' ] || param_name [ '!' ] ? :req : :opt ) , schema
67
+ param param_type , param_name , nil , required? ( param_type , param_name ) , schema
62
68
end
63
69
end
64
70
end
65
71
66
- def param_ref component_key , *keys
72
+ def param_ref ( component_key , *keys )
67
73
self [ :parameters ] += [ component_key , *keys ] . map { |key | RefObj . new ( :parameter , key ) }
68
74
end
69
75
70
76
# options: `exp_params` and `examples`
71
- def request_body required , media_type , data : { } , desc : '' , **options
72
- ( self [ :requestBody ] ||= RequestBodyObj . new ( required , desc ) ) . absorb ( media_type , { data : data , **options } )
77
+ def request_body ( required , media_type , data : { } , desc : "" , **options )
78
+ self [ :requestBody ] ||= RequestBodyObj . new ( required , desc )
79
+ self [ :requestBody ] . absorb ( media_type , { data :, **options } )
80
+ _all_params . merge! ( data )
73
81
end
74
82
75
- def body_ref component_key
76
- self [ :requestBody ] = RefObj . new ( :requestBody , component_key )
77
- end
83
+ def body ( media_type , data : { } , **) = request_body ( :optional , media_type , data :, **)
84
+ def body! ( media_type , data : { } , **) = request_body ( :required , media_type , data :, **)
78
85
79
- %i[ body body! ] . each do |method |
80
- define_method method do |media_type , data : { } , **options |
81
- request_body ( method [ '!' ] ? :req : :opt ) , media_type , data : data , **options
82
- end
83
- end
84
-
85
- def form data :, **options
86
- body :form , data : data , **options
87
- end
86
+ def json ( data :, **) = body ( :json , data :, **)
87
+ def json! ( data :, **) = body! ( :json , data :, **)
88
+ def form ( data :, **) = body ( :form , data :, **)
89
+ def form! ( data :, **) = body! ( :form , data :, **)
88
90
89
- def form! data :, **options
90
- body! :form , data : data , **options
91
- end
92
-
93
- def data name , type = nil , schema = { }
91
+ def data ( name , type = nil , schema = { } )
94
92
schema [ :type ] = type if type . present?
95
93
form data : { name => schema }
96
94
end
97
95
98
- def response code , desc , media_type = nil , headers : { } , data : { } , **options
99
- ( self [ :responses ] [ code . to_s ] ||= ResponseObj . new ( desc ) ) . absorb ( desc , media_type , headers : headers , data : data , **options )
96
+ def body_ref ( component_key )
97
+ self [ :requestBody ] = RefObj . new ( :requestBody , component_key )
98
+ end
99
+
100
+ def response ( code , desc , media_type = nil , headers : { } , data : { } , **)
101
+ self [ :responses ] [ code . to_s ] ||= ResponseObj . new ( desc )
102
+ self [ :responses ] [ code . to_s ] . absorb ( desc , media_type , headers :, data :, **)
100
103
end
101
104
102
105
alias_method :resp , :response
103
106
alias_method :error , :response
104
107
105
- def response_ref code_and_compkey # = { }
106
- code_and_compkey . each { |code , component_key | self [ :responses ] [ code . to_s ] = RefObj . new ( :response , component_key ) }
108
+ def response_ref ( code_and_compkey ) # = { }
109
+ code_and_compkey . each do |code , component_key |
110
+ self [ :responses ] [ code . to_s ] = RefObj . new ( :response , component_key )
111
+ end
107
112
end
108
113
109
- def security_require scheme_name , scopes : [ ]
114
+ def security_require ( scheme_name , scopes : [ ] )
110
115
self [ :security ] << { scheme_name => scopes }
111
116
end
112
117
113
118
alias security security_require
114
119
alias auth security_require
115
120
alias auth_with security_require
116
121
117
- def callback event_name , http_method , callback_url , &block
118
- self [ :callbacks ] . deep_merge! CallbackObj . new ( event_name , http_method , callback_url , &block ) . process
122
+ def callback ( event_name , http_method , callback_url , &block )
123
+ self [ :callbacks ] . deep_merge! (
124
+ CallbackObj . new ( event_name , http_method , callback_url , &block ) . process
125
+ )
119
126
end
120
127
121
- def server url , desc : ''
128
+ def server ( url , desc : "" )
122
129
self [ :servers ] << { url : url , description : desc }
123
130
end
124
131
125
- def param_examples exp_params = :all , examples_hash
132
+ def param_examples ( exp_params = :all , examples_hash )
126
133
exp_params = self [ :parameters ] . map ( &:name ) if exp_params == :all
127
134
self [ :examples ] = ExampleObj . new ( examples_hash , exp_params , multiple : true ) . process
128
135
end
@@ -135,10 +142,31 @@ def run_dsl(dry: false, &block)
135
142
136
143
self [ :parameters ] . map! ( &:process )
137
144
self [ :requestBody ] = self [ :requestBody ] . try ( :process )
138
- self [ :responses ] . each { |code , response | self [ :responses ] [ code ] = response . process }
139
- self [ :responses ] = self [ :responses ] . sort . to_h
145
+ self [ :responses ] = self [ :responses ] . transform_values ( &:process ) . sort . to_h
140
146
self . delete_if { |_ , v | v . blank? }
141
147
end
148
+
149
+ def all_params
150
+ _all_params . transform_values do |t |
151
+ if t . is_a? ( Hash )
152
+ t . key? ( :type ) ? t . merge! ( type : t [ :type ] . to_s . underscore ) : { type : t }
153
+ else
154
+ { type : t . to_s . underscore }
155
+ end
156
+ end
157
+ end
158
+
159
+ private
160
+
161
+ def override_or_append_to_parameters ( param_obj )
162
+ # The definition of the same name parameter will be override.
163
+ index = self [ :parameters ] . map ( &:name ) . index ( param_obj . name )
164
+ index ? self [ :parameters ] [ index ] = param_obj : self [ :parameters ] << param_obj
165
+ end
166
+
167
+ def required? ( *passed )
168
+ passed . join [ "!" ] ? :required : :optional
169
+ end
142
170
end
143
171
end
144
172
end
0 commit comments