Sha256: 2f899c3d3f1d02d2c0d88349860550d5637714eff4e6f3a1676d7c37a4bcb81a

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

class Rage::OpenAPI::Parsers::YAML
  def initialize(**)
  end

  def known_definition?(yaml)
    object = YAML.safe_load(yaml) rescue nil
    !!object && object.is_a?(Enumerable)
  end

  def parse(yaml)
    __parse(YAML.safe_load(yaml))
  end

  private

  def __parse(object)
    spec = {}

    if object.is_a?(Hash)
      spec = { "type" => "object", "properties" => {} }

      object.each do |key, value|
        spec["properties"][key] = if value.is_a?(Enumerable)
          __parse(value)
        else
          type_to_spec(value)
        end
      end

    elsif object.is_a?(Array) && object.length == 1
      spec = { "type" => "array", "items" => object[0].is_a?(Enumerable) ? __parse(object[0]) : type_to_spec(object[0]) }

    elsif object.is_a?(Array)
      spec = { "type" => "string", "enum" => object }
    end

    spec
  end

  private

  def type_to_spec(type)
    case type
    when "Integer"
      { "type" => "integer" }
    when "Float"
      { "type" => "number", "format" => "float" }
    when "Numeric"
      { "type" => "number" }
    when "Boolean"
      { "type" => "boolean" }
    when "Hash"
      { "type" => "object" }
    when "Date"
      { "type" => "string", "format" => "date" }
    when "DateTime", "Time"
      { "type" => "string", "format" => "date-time" }
    when "String"
      { "type" => "string" }
    else
      { "type" => "string", "enum" => [type] }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rage-rb-1.12.0 lib/rage/openapi/parsers/yaml.rb
rage-rb-1.11.0 lib/rage/openapi/parsers/yaml.rb