Sha256: 2cb7db006439beb43430620163a208c2fc92da254d8a46a1716949c5751558c3

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require_relative '../application/parameters'
require_relative '../utils/kwargs/checker'

module Meta
  module RouteDSL
    class ParametersBuilder
      def initialize(route_full_path:, route_method:, &block)
        @route_full_path = route_full_path || ''
        @route_method = route_method
        @parameter_options = {}

        instance_exec &block if block_given?
      end

      def param(name, options = {})
        # 修正 path 参数的选项
        options = options.dup
        if path_param_names.include?(name) # path 参数
          options = Utils::KeywordArgs::Checker.fix!(options, in: 'path', required: true)
        else
          options = Utils::KeywordArgs::Checker.merge_defaults!(options, in: 'query')
        end

        in_op = options.delete(:in)
        @parameter_options[name] = { in: in_op, schema: JsonSchema::BaseSchema.new(options) }
      end

      def build
        # 补充未声明的 path 参数
        (path_param_names - @parameter_options.keys).each do |name|
          @parameter_options[name] = { in: 'path', schema: JsonSchema::BaseSchema.new(required: true) }
        end

        Parameters.new(@parameter_options)
      end

      private

      def path_param_names
        @_path_param_names ||= @route_full_path.split('/')
                                               .filter { |part| part =~ /[:*].+/ }
                                               .map { |part| part[1..-1].to_sym }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
meta-api-0.1.1 lib//meta/route_dsl/parameters_builder.rb
meta-api-0.1.0 lib//meta/route_dsl/parameters_builder.rb
meta-api-0.0.9 lib//meta/route_dsl/parameters_builder.rb
meta-api-0.0.8 lib/meta/route_dsl/parameters_builder.rb
meta-api-0.0.7 lib/meta/route_dsl/parameters_builder.rb