Class: Rack::Schema

Inherits:
Object
  • Object
show all
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

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, options = {}, &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(options)
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