Class: ApiHammer::Filtration::FormEncoded

Inherits:
Object
  • Object
show all
Defined in:
lib/api_hammer/filtration/form_encoded.rb

Instance Method Summary collapse

Constructor Details

#initialize(string, options = {}) ⇒ FormEncoded

Returns a new instance of FormEncoded.



6
7
8
9
# File 'lib/api_hammer/filtration/form_encoded.rb', line 6

def initialize(string, options = {})
  @string = string
  @options = options
end

Instance Method Details

#filterObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/api_hammer/filtration/form_encoded.rb', line 11

def filter
  ss = StringScanner.new(@string)
  filtered = ''
  while !ss.eos?
    if ss.scan(/[&;]/)
      filtered << ss[0]
    end
    if ss.scan(/[^&;]+/)
      kv = ss[0]
      key, _ = kv.split('=', 2)
      parsed_key = CGI::unescape(key)
      if [*@options[:filter_keys]].any? { |fk| parsed_key =~ /(\A|[\[\]])#{Regexp.escape(fk)}(\z|[\[\]])/ }
        filtered << [key, '[FILTERED]'].join('=')
      else
        filtered << ss[0]
      end
    end
  end
  filtered
end