Sha256: ad6917054d5673d560f639a7967bdf18a8220805a9fa985291a80eec41e36eb9

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 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 required?
      !!@options[:presence]
    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

3 entries across 3 versions & 1 rubygems

Version Path
api-regulator-0.1.11 lib/api_regulator/param.rb
api-regulator-0.1.10 lib/api_regulator/param.rb
api-regulator-0.1.9 lib/api_regulator/param.rb