Sha256: ec39bcbdd9803ea1864d3a42fbfd76d293a254ce30202e3c589fee05a349a728

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module OasDivider
  class RelativeDocumentReferencer
    def self.execute(root, depth)
      new(root, depth).execute
    end

    def initialize(root, depth)
      @root = root
      @depth = depth
    end

    def execute
      lookup(root)
      root
    end

    # hashで、keyが$refならconvert
    # hashかarrayならlookup
    # そうでないならなにもしない
    def lookup(object)
      if object.is_a? Array
        object.each { |value| lookup(value) if value.is_a?(Hash) || value.is_a?(Array) }
      else
        object.each do |key, value|
          set_converted_value(object, key, value) if key === "$ref"
          lookup(value) if value.is_a?(Hash) || value.is_a?(Array)
        end
      end
    end

    def set_converted_value(object, key, value)
      object[key] = convert(value)
    end
    
    def convert(ref)
      # componentから取得することしか考えない
      if ref.split('/')[2] === 'schemas'
        "../" * depth + "#{ref.split('/')[1..3].join('/')}.yml"
      else
        "../" * depth + "#{ref.split('/')[1..2].join('/')}.yml#/#{ref.split('/')[3]}"
      end
    end

  private
    attr :root, :depth
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
oas_divider-0.2.0 lib/oas_divider/relative_document_referencer.rb
oas_divider-0.1.0 lib/oas_divider/relative_document_referencer.rb