Module: Roda::RodaPlugins::ParamsCheck::InstanceMethods
- Defined in:
- lib/zleb/plugins/params_check.rb
Instance Method Summary collapse
- #checked_params ⇒ Object
- #def_checker(&block) ⇒ Object
- #desc(description) ⇒ Object
- #is_interface_info_request? ⇒ Boolean
- #output_filter(type = :hash, safe = true, name = nil, &block) ⇒ Object
- #params_check(name = nil, &block) ⇒ Object
- #process(&block) ⇒ Object
Instance Method Details
#checked_params ⇒ Object
243 244 245 |
# File 'lib/zleb/plugins/params_check.rb', line 243 def checked_params @_checked_params.to_h end |
#def_checker(&block) ⇒ Object
239 240 241 |
# File 'lib/zleb/plugins/params_check.rb', line 239 def def_checker(&block) block end |
#desc(description) ⇒ Object
235 236 237 |
# File 'lib/zleb/plugins/params_check.rb', line 235 def desc(description) @interface_info = {desc: description} end |
#is_interface_info_request? ⇒ Boolean
178 179 180 |
# File 'lib/zleb/plugins/params_check.rb', line 178 def is_interface_info_request? request.headers["Interface-Info"] != nil end |
#output_filter(type = :hash, safe = true, name = nil, &block) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/zleb/plugins/params_check.rb', line 182 def output_filter(type = :hash, safe = true, name = nil, &block) request.output_entity_filter_safe = safe if name == nil name = "#{request.request_method} #{request.matched_path}" end if type == :array contract_block = Proc.new do params do required(OutputEncloseName).array(:hash, &block) end end else contract_block = Proc.new do params do required(OutputEncloseName).hash(&block) end end end if !(ResponseEntityFilterContainer.key?(name)) contract = Class.new(ApplicationContract, &contract_block) ResponseEntityFilterContainer.register(name, contract.new) end filter = ResponseEntityFilterContainer.resolve(name) interface_info_flag = request.headers["Interface-Info"] if interface_info_flag output_entity_schema_info = filter.class.schema_info interface_info[:output_schema] = output_entity_schema_info response.status = 200 response['Content-Type'] = 'application/json' res_data = nil if interface_info_flag == 'output' res_data = {output_schema: output_entity_schema_info} elsif interface_info_flag == 'input' res_data = {input_schema: @interface_info[:input_schema]} elsif interface_info_flag == 'mock' res_data = mock_data_extract(output_entity_schema_info)[OutputEncloseName] elsif interface_info_flag == 'all' res_data = @interface_info else res_data = {} end response.write(res_data.to_json) throw :halt, response.finish end @output_entity_filter = filter request.output_entity_filter = filter end |
#params_check(name = nil, &block) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/zleb/plugins/params_check.rb', line 132 def params_check(name=nil, &block) if name == nil name = "#{request.request_method} #{request.path}" end if !(RequestCheckerContainer.key?(name)) contract = Class.new(ApplicationContract, &block) begin RequestCheckerContainer.register(name, contract.new) rescue Dry::Container::Error => e puts e. end end @input_checkers ||= [] checker = RequestCheckerContainer.resolve(name) @input_checkers << checker interface_info_flag = request.headers["Interface-Info"] if interface_info_flag input_params_schema_info = {} @input_checkers.each do |ch| input_params_schema_info = input_params_schema_info.deep_merge(ch.class.schema_info) end interface_info[:input_schema] = input_params_schema_info return end result = checker.(request.params) if (result.failure?) response.status = 400 response['Content-Type'] = 'application/json' response.write({error: result.errors.to_h }.to_json) throw :halt, response.finish end @_checked_params = result end |
#process(&block) ⇒ Object
169 170 171 172 173 174 175 |
# File 'lib/zleb/plugins/params_check.rb', line 169 def process(&block) if is_interface_info_request? interface_info else self.instance_exec &block end end |