Module: Gown

Defined in:
lib/gown.rb,
lib/gown/version.rb,
lib/gown/patterns.rb

Defined Under Namespace

Classes: Patterns

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.patternsObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gown.rb', line 8

def patterns
  patterns_to_remove = (
    Gown::Patterns::KGP_lifecycle + 
    Gown::Patterns::KGP_warnings +
    Gown::Patterns::KGP_metadata +
    Gown::Patterns::KGP_traffic +
    Gown::Patterns::CHANNEL_events +
    Gown::Patterns::MAIN_lifecycle
  ).map {|pattern| Regexp.new (Gown::Patterns::Timestamp + pattern)}

  return patterns_to_remove
end

.strip(input) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/gown.rb', line 21

def strip input
  patterns_to_remove = patterns
  output = ""

  input.each do |line|
    if line.match /^[[:space:]]*$/
      # blank_lines_removed +=1
    else
      filtered = patterns_to_remove.find { |pattern| pattern.match line }
      if !filtered
        output.append line
      else
        # lines_removed+= 1
      end
    end
  end

  return output
end

.strip_file(input_filename, output_filename) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gown.rb', line 41

def strip_file input_filename, output_filename
  patterns_to_remove = patterns

  puts "Checking for #{patterns_to_remove.length} patterns"

  spinner = TTY::Spinner.new("[:spinner] Parsing #{input_filename}")
  begin

    lines_removed = 0
    blank_lines_removed = 0

    spinner.auto_spin
    input = File.open input_filename
    input.advise(:sequential)

    output = File.new output_filename, "a"

    input.each do |line|
      if line.match /^[[:space:]]*$/
        blank_lines_removed +=1
      else
        filtered = patterns_to_remove.find { |pattern| pattern.match line }
        if !filtered
          output.puts line
        else
          lines_removed+= 1
        end
      end
    end

    status = " -- Removed #{lines_removed} lines"
    status << ", #{blank_lines_removed} blank lines" if blank_lines_removed > 0
    spinner.success status
  rescue => e
    spinner.error "Something dire happened!"
    puts e
  ensure
    input.close if input
    output.close if output
  end
end