38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/meta/rails.rb', line 38
def self.included(base)
return if self.respond_to?(:route_definitions)
base.extend ClassMethods
route_definitions = {}
base.define_singleton_method(:route_definitions) { route_definitions }
base.define_singleton_method(:apply_route_definition) do |klass, method_name|
if @current_route_builder
self.route_definitions[[klass, method_name]] = @current_route_builder.build
@current_route_builder = nil
end
end
base.define_singleton_method(:method_added) do |name|
apply_route_definition(self, name)
end
attr_accessor :params_on_schema
base.before_action do
route_definitions = self.class.route_definitions
route_definition = route_definitions[[self.class, params[:action].to_sym]]
next unless route_definition
meta_definition = route_definition.meta
next if meta_definition[:parameters].empty? && meta_definition[:request_body].nil?
raw_params = self.params.to_unsafe_h
final_params = {}
if meta_definition[:parameters]
parameters_meta = meta_definition[:parameters]
final_params = parameters_meta.filter(request)
end
if meta_definition[:request_body]
params_schema = meta_definition[:request_body]
final_params.merge! params_schema.filter(raw_params, stage: :param)
end
self.params_on_schema = final_params
rescue JsonSchema::ValidationErrors => e
raise Errors::ParameterInvalid.new(e.errors)
end
end
|