Class: Dap::Filter::FilterTransform

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/dap/filter/simple.rb

Instance Attribute Summary

Attributes included from Base

#name, #opts

Instance Method Summary collapse

Methods included from Base

#initialize

Instance Method Details

#process(doc) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/dap/filter/simple.rb', line 220

def process(doc)
  self.opts.each_pair do |k,v|
    if doc.has_key?(k)
      case v
      when /^int(?<base>\d+)?$/
        base = $LAST_MATCH_INFO['base']
        if base.nil?
          doc[k] = doc[k].to_s.to_i
        else
          doc[k] = doc[k].to_s.to_i(base.to_i)
        end
      when 'float'
        doc[k] = doc[k].to_f
      when 'reverse'
        doc[k] = doc[k].to_s.reverse
      when 'downcase'
        doc[k] = doc[k].to_s.downcase
      when 'upcase'
        doc[k] = doc[k].to_s.upcase
      when 'ascii'
        doc[k] = doc[k].to_s.gsub(/[\x00-\x1f\x7f-\xff]/n, '')
      when 'utf8encode'
        doc[k] = doc[k].to_s.encode!('UTF-8', invalid: :replace, undef: :replace, replace: '')
      when 'base64decode'
        doc[k] = doc[k].to_s.unpack('m*').first
      when 'base64encode'
        doc[k] = [doc[k].to_s].pack('m*').gsub(/\s+/n, '')
      when 'qprintdecode'
        doc[k] = doc[k].to_s.gsub(/=([0-9A-Fa-f]{2})/n){ |x| [x[1,2]].pack("H*") }
      when 'qprintencode'
        doc[k] = doc[k].to_s.gsub(/[\x00-\x20\x3d\x7f-\xff]/n){|x| ( "=%.2x" % x.unpack("C").first ).upcase }
      when 'hexdecode'
        doc[k] = [ doc[k].to_s ].pack("H*")
      when 'hexencode'
        doc[k] = doc[k].to_s.unpack("H*").first
      else
        fail "Invalid transform '#{v}'"
      end
    end
  end
 [ doc ]
end