class Dry::Swagger::ContractParser

Constants

PREDICATE_TO_TYPE

Attributes

keys[R]

@api private

Public Class Methods

new() click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 22
def initialize
  @keys = {}
  @config = Config::ContractConfiguration
end

Public Instance Methods

call(contract, &block) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 33
def call(contract, &block)
  @keys = {}
  visit(contract.schema.to_ast)
  instance_eval(&block) if block_given?
  self
end
predicate_description(name, value) click to toggle source
# File lib/dry/swagger/contract_parser.rb, line 119
def predicate_description(name, value)
  ::I18n.t("contract.descriptions.#{name}", value: value, default: '')
end
to_h() click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 28
def to_h
  { keys: keys }
end
to_swagger() click to toggle source
# File lib/dry/swagger/contract_parser.rb, line 123
def to_swagger
  DocumentationGenerator.new(@config).generate_documentation(keys)
end
visit(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 41
def visit(node, opts = {})
  meth, rest = node
  public_send(:"visit_#{meth}", rest, opts)
end
visit_and(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 61
def visit_and(node, opts = {})
  left, right = node

  visit(left, opts)
  visit(right, opts)
end
visit_each(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 81
def visit_each(node, opts = {})
  visit(node, opts.merge(member: true))
end
visit_implication(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 73
def visit_implication(node, opts = {})
  node.each do |el|
    opts = opts.merge(required: false)
    visit(el, opts)
  end
end
visit_key(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 86
def visit_key(node, opts = {})
  name, rest = node
  opts = opts.merge(key: name)
  opts = opts.merge(required: true)
  visit(rest, opts)
end
visit_not(_node, opts = {}) click to toggle source
# File lib/dry/swagger/contract_parser.rb, line 68
def visit_not(_node, opts = {})
  keys[opts[:key]][@config.nullable_type] = true
end
visit_predicate(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 94
def visit_predicate(node, opts = {})
  name, rest = node

  key = opts[:key]

  if name.equal?(:key?)
    keys[rest[0][1]] = { required: opts.fetch(:required, true) }
  elsif name.equal?(:array?)
    keys[key][:array] = true
  elsif name.equal?(:included_in?)
    enums = rest[0][1]
    enums += [nil] if opts.fetch(@config.nullable_type, false)
    keys[key][:enum] = enums
  elsif PREDICATE_TO_TYPE[name]
    keys[key][:type] = PREDICATE_TO_TYPE[name]
  else
    description = predicate_description(name.to_s, rest[0][1].to_s)
    if keys[key][:description].to_s.empty?
      keys[key][:description] = description unless description.to_s.empty?
    else
      keys[key][:description] += ", #{description}" unless description.to_s.empty?
    end
  end
end
visit_set(node, opts = {}) click to toggle source

@api private

# File lib/dry/swagger/contract_parser.rb, line 47
def visit_set(node, opts = {})
  target = (key = opts[:key]) ? self.class.new : self

  node.map { |child| target.visit(child, opts) }

  return unless key

  target_info = opts[:member] ? target.to_h : target.to_h
  type = keys[key][:array] ? 'array' : 'hash'

  keys.update(key => { **keys[key], type: type, **target_info })
end