Sha256: 8fcf6b8c2ad11553607e4c485758b4c921bd3ccc24381a41f530a23463fb1b40

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

# frozen_string_literal: true
require 'request_handler/option_handler'
require 'request_handler/error'
module RequestHandler
  class IncludeOptionHandler < OptionHandler
    def run
      return [] unless params.key?('include')
      options = fetch_options
      raise ExternalArgumentError, include: 'must not contain a space' if options.include? ' '
      allowed_options(options.split(','))
    end

    def allowed_options(options)
      options.map do |option|
        begin
          allowed_options_type&.call(option)
        rescue Dry::Types::ConstraintError
          raise OptionNotAllowedError, option.to_sym => 'is not an allowed include option'
        end
        option.to_sym
      end
    end

    def fetch_options
      raise ExternalArgumentError, include_options: 'query paramter must not be empty' if empty_param?('include')
      params.fetch('include') { '' }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
request_handler-0.8.0 lib/request_handler/include_option_handler.rb