Sha256: 04f7e4f434cdd20d3fcece2dcebe0b13e5aaa1884b6a9bfbead0dcb9716e407a

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Kingsman
  class ParameterFilter
    def initialize(case_insensitive_keys, strip_whitespace_keys)
      @case_insensitive_keys = case_insensitive_keys || []
      @strip_whitespace_keys = strip_whitespace_keys || []
    end

    def filter(conditions)
      conditions = stringify_params(conditions.dup)

      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :downcase, @case_insensitive_keys))
      conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :strip, @strip_whitespace_keys))

      conditions
    end

    def filtered_hash_by_method_for_given_keys(conditions, method, condition_keys)
      condition_keys.each do |k|
        next unless conditions.key?(k)

        value = conditions[k]
        conditions[k] = value.send(method) if value.respond_to?(method)
      end

      conditions
    end

    # Force keys to be string to avoid injection on mongoid related database.
    def stringify_params(conditions)
      return conditions unless conditions.is_a?(Hash)
      conditions.each do |k, v|
        conditions[k] = v.to_s if param_requires_string_conversion?(v)
      end
    end

    private

    def param_requires_string_conversion?(value)
      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kingsman-0.1.1 lib/kingsman/parameter_filter.rb
kingsman-0.1.0 lib/kingsman/parameter_filter.rb