Sha256: 83b9af5a2d92043a25db4370ef1fbe9ebbc94abc04657916b3ef6c7f2cc6c656

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

require 'mustermann/template'

module OasParser
  class Definition
    include OasParser::RawAccessor
    raw_keys :info, :servers, :components, :openapi
    attr_reader :path

    attr_accessor :raw

    def self.resolve(path)
      raw = Parser.resolve(path)
      Definition.new(raw, path)
    end

    def initialize(raw, path)
      @raw = raw
      @path = path
    end

    def format
      File.extname(@path).sub('.', '')
    end

    def paths
      raw['paths'].map do |path, definition|
        OasParser::Path.new(self, path, definition)
      end
    end

    def path_by_path(path)
      definition = raw['paths'].fetch(path) do |path|
        key = raw['paths'].keys.detect do |path_entry|
          Mustermann::Template.new(path_entry).match(path)
        end
        raw['paths'][key]
      end
      raise StandardError.new('So such path exists') unless definition
      OasParser::Path.new(self, path, definition)
    end

    def security
      raw['security'] || []
    end

    def endpoints
      paths.flat_map(&:endpoints)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oas_parser-0.16.0 lib/oas_parser/definition.rb
oas_parser-0.15.2 lib/oas_parser/definition.rb
oas_parser-0.15.1 lib/oas_parser/definition.rb