Sha256: 34aa80d6fab612c0f60f891b8b6d708913da852a3e73b6137bdf2276c2ae50fd

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

class OpenAPI::Loader::Translator
  #
  # Translates OAS2 host, basePath and schemes to OAS3 servers
  # Mutates the source
  #
  # @private
  #
  class ConvertServers < SimpleDelegator
    def call
      convert self
      paths.each { |item| convert item }
      operations.each { |item| convert item }
    end

    private

    def paths
      @paths ||= Enumerator.new do |yielder|
        fetch("paths", {}).each_value do |path|
          path.each_value { |item| yielder << item if item.is_a? Hash }
        end
      end
    end

    def operations
      @operations ||= Enumerator.new do |yielder|
        paths.each do |path|
          path.each_value { |item| yielder << item if item.is_a? Hash }
        end
      end
    end

    def url
      @url ||= File.join delete("host"), delete("basePath")
    end

    def convert(item)
      enum = item.delete "schemes"
      return unless enum
      item["servers"] = [{
        "url" => "{scheme}://#{url}",
        "variables" => { "scheme" => { "enum" => Array(enum) } }
      }]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
open_api-loader-0.0.1 lib/open_api/loader/translator/convert_servers.rb