Sha256: 1bc86ed5cd3474128ed9092c4b94f627443ef5b5cb06da26c705717653086795

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'request_handler/option_parser'
require 'request_handler/error'
module RequestHandler
  class IncludeOptionParser < OptionParser
    def run
      return [] unless params.key?('include')
      options = fetch_options
      raise_error('INVALID_QUERY_PARAMETER', 'must not contain a space') if options.include?(' ')
      allowed_options(options.split(','))
    end

    def allowed_options(options)
      options.map do |option|
        option.gsub!('.', ::RequestHandler.configuration.separator)
        begin
          RequestHandler.configuration.validation_engine.validate!(option, allowed_options_type).output.to_sym
        rescue Validation::Error
          raise_error('OPTION_NOT_ALLOWED', "#{option} is not an allowed include option", OptionNotAllowedError)
        end
      end
    end

    def fetch_options
      raise_error('INVALID_QUERY_PARAMETER', 'must not be empty') if empty_param?('include')
      params.fetch('include') { '' }
    end

    private

    def raise_error(code, detail, error_klass = IncludeParamsError)
      raise error_klass, [
        {
          status: '400',
          code:   code,
          detail: detail,
          source: { param: 'include' }
        }
      ]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
request_handler-2.1.0 lib/request_handler/include_option_parser.rb
request_handler-2.0.0 lib/request_handler/include_option_parser.rb