Class: Rack::Schema
- Inherits:
-
Object
- Object
- Rack::Schema
- Defined in:
- lib/rack/schema.rb,
lib/rack/schema/version.rb
Constant Summary collapse
- ValidationError =
Class.new(StandardError)
- VERSION =
"0.7.0"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, &handler) ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize(app, options = {}, &handler) ⇒ Schema
Returns a new instance of Schema.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rack/schema.rb', line 10 def initialize(app, = {}, &handler) @app = app @handler = handler @handler ||= proc { |errors, env, (status, headers, body)| json = '' body.each { |s| json.concat s } raise ValidationError.new({ errors: errors, body: json }) if errors.any? } @options = { validate_schemas: true, swallow_links: false }.merge() end |
Instance Method Details
#call(env) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rack/schema.rb', line 26 def call(env) status, headers, body = @app.call(env) link_header = LinkHeader.parse(headers['Link']) schema_links = link_header.links.select do |link| link.attrs['rel'] == 'describedby' end errors = validate(body, schema_links) swallow(headers, link_header) if swallow_links? response = [status, headers, body] @handler.call(errors, env, response) || response end |