module Sinatra::RatPackSwagger

Public Class Methods

registered(app) click to toggle source
# File lib/rat_pack_swagger.rb, line 135
def self.registered(app)

  app.get '/v2/swagger.json' do
    content_type 'application/json'
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Headers'] = 'Content-Type, api-key, Authorization'
    response['Access-Control-Allow-Methods'] = 'GET, POST'
    errors = ::JSON::Validator.fully_validate(@@spec.swagger_schema, @@spec.api_spec, errors_as_objects: true)
    if errors.empty? then @@spec.api_spec_json else errors.to_json end 
  end

  app.before do
    @@validators ||= ::RatPackSwagger::RequestValidatorCollection.new
    vs = @@validators.get(request.path.gsub(/:(\w+)/, '{\1}'), request.request_method.downcase)
    if vs && vs[:body]
      request.body.rewind
      vs[:body].validate(request.body.read)
      request.body.rewind
    end
    # TODO: add other param types like 'query'
  end
end
route_added(verb, path, block) click to toggle source
# File lib/rat_pack_swagger.rb, line 158
def self.route_added(verb, path, block)
  return if path == '/v2/swagger.json'
  verb.downcase!
  path = path.gsub(/:(\w+)/, '{\1}')
  return unless ['get', 'post', 'put', 'delete'].include?(verb)
  return unless @@spec.this_route.swagger?
  @@spec.register_this_route(path, verb)

  parameters = @@spec.resolved_spec[:paths][path][verb][:parameters]
  return unless parameters

  @@validators ||= ::RatPackSwagger::RequestValidatorCollection.new
  body_param = parameters.select{|p| p[:in].to_sym == :body}.first
  if @@spec.route_consumes?(path, verb, 'application/json')
    if body_param
      @@validators.set(path, verb, :body, ::RatPackSwagger::JsonBodyValidator.new(body_param[:schema]))
    end
  # TODO: add validators for other param types like 'query'
  end
end

Public Instance Methods

_spec() click to toggle source
# File lib/rat_pack_swagger.rb, line 85
def _spec
  @@spec ||= ::RatPackSwagger::SwaggerSpec.new
end
_validators() click to toggle source
# File lib/rat_pack_swagger.rb, line 88
def _validators
  @@validators ||= ::RatPackSwagger::RequestValidatorCollection.new
end
consumes(*args) click to toggle source
# File lib/rat_pack_swagger.rb, line 122
def consumes(*args)
  _spec.this_route.consumes.concat(args)
end
definitions(*constants) click to toggle source
# File lib/rat_pack_swagger.rb, line 102
def definitions(*constants)
  _spec.add_definitions(*constants)
end
description(d) click to toggle source
# File lib/rat_pack_swagger.rb, line 106
def description(d)
  _spec.this_route.description = d
end
param(**kwargs, &block) click to toggle source
# File lib/rat_pack_swagger.rb, line 110
def param(**kwargs, &block)
  _spec.this_route.parameters << SwaggerObject.new(**kwargs, &block).to_h
end
produces(*args) click to toggle source
# File lib/rat_pack_swagger.rb, line 126
def produces(*args)
  _spec.this_route.produces.concat(args)
end
response(http_status_code, **kwargs, &block) click to toggle source
# File lib/rat_pack_swagger.rb, line 130
def response(http_status_code, **kwargs, &block)
  _spec.this_route.responses[http_status_code] = SwaggerObject.new(**kwargs, &block).to_h
end
summary(s) click to toggle source
# File lib/rat_pack_swagger.rb, line 118
def summary(s)
  _spec.this_route.summary = s
end
swagger(*args, **kwargs, &block) click to toggle source
# File lib/rat_pack_swagger.rb, line 92
def swagger(*args, **kwargs, &block)
  if args.count.zero?
    # assume passing data into method call
    _spec.spec.merge!(SwaggerObject.new(**kwargs, &block).to_h)
  else
    # assume single argument is filename of existing json
    _spec.spec.merge!(::JSON.parse(File.read(args[0])))
  end
end
tags(*args) click to toggle source
# File lib/rat_pack_swagger.rb, line 114
def tags(*args)
  _spec.this_route.tags.concat(args)
end