Sha256: 089e9e1d093413a36559a7bc09dc443c0fa09e758fd0b7e5b065c15d4a78b5cb

Contents?: true

Size: 1.62 KB

Versions: 7

Compression:

Stored size: 1.62 KB

Contents

module ApiRegulator
  class Param
    attr_reader :name, :type, :options, :item_type, :desc, :location, :children

    def initialize(name, type = nil, item_type: nil, desc: "", location: :body, **options, &block)
      @name = name
      @type = type&.to_sym || (block_given? ? :object : :string)
      @item_type = item_type
      @location = location.to_sym
      @desc = desc
      @options = options
      @children = []

      instance_eval(&block) if block_given?
    end

    def param(name, type = nil, item_type: nil, desc: "", location: :body, **options, &block)
      child = Param.new(name, type, item_type: item_type, desc: desc, location: location, **options, &block)
      @children << child
    end

    def ref(ref_name, except: [], only: [])
      shared_schema = ApiRegulator.shared_schema(ref_name)
      raise "Shared schema #{ref_name} not found" unless shared_schema

      # Filter parameters based on `only` or `except` options
      filtered_params = shared_schema.params

      if only.any?
        filtered_params = filtered_params.select { |param| only.include?(param.name) }
      elsif except.any?
        filtered_params = filtered_params.reject { |param| except.include?(param.name) }
      end

      filtered_params.each do |shared_param|
        @children << shared_param
      end
    end

    def body?
      location == :body
    end

    def query?
      location == :query
    end

    def path?
      location == :path
    end

    def parameter?
      [:path, :query].include?(location)
    end

    def object?
      type.to_sym == :object
    end

    def array?
      type.to_sym == :array
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
api-regulator-0.1.8 lib/api_regulator/param.rb
api-regulator-0.1.7 lib/api_regulator/param.rb
api-regulator-0.1.6 lib/api_regulator/param.rb
api-regulator-0.1.5 lib/api_regulator/param.rb
api-regulator-0.1.4 lib/api_regulator/param.rb
api-regulator-0.1.3 lib/api_regulator/param.rb
api-regulator-0.1.2 lib/api_regulator/param.rb