lib/fakeit/app/app.rb in fakeit-0.1.1 vs lib/fakeit/app/app.rb in fakeit-0.1.2
- old
+ new
@@ -1,35 +1,42 @@
module Fakeit
module App
class << self
- def create(spec_file)
+ def create(spec_file, options)
specification = Fakeit::Openapi.load(spec_file)
proc do |env|
request = Rack::Request.new(env)
specification
- .operation(request.request_method.downcase.to_sym, request.path_info)
- &.tap { |operation| validate(operation, request) }
- .then(&method(:rack_response))
- rescue Fakeit::Validation::ValidationError => e
- error_response(e)
+ .operation(request.request_method.downcase.to_sym, request.path_info, options)
+ .then { |operation| operation ? handle(operation, request, options) : not_found }
end
end
private
- def error_response(err)
- { message: err.message }
- .to_json
- .then { |message| [418, { 'Content-Type' => 'application/json' }, [message]] }
- end
-
- def rack_response(operation)
- if operation
- [operation.status, operation.headers, [operation.body]]
+ def handle(operation, request, options)
+ validate(operation, request)
+ response(operation)
+ rescue Fakeit::Validation::ValidationError => e
+ if options.permissive
+ Fakeit::Logger.warn(e.message)
+ response(operation)
else
- [404, {}, ['Not Found']]
+ error(e)
end
+ end
+
+ def error(err)
+ [418, { 'Content-Type' => 'application/json' }, [{ message: err.message }.to_json]]
+ end
+
+ def not_found
+ [404, {}, ['Not Found']]
+ end
+
+ def response(operation)
+ [operation.status, operation.headers, [operation.body]]
end
def validate(operation, request)
operation.validate(
body: request.body&.read.to_s,