Class: Parselogs::FileOps

Inherits:
Object
  • Object
show all
Defined in:
lib/parselogs/file_ops.rb

Instance Method Summary collapse

Instance Method Details

#compressed?(file) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/parselogs/file_ops.rb', line 46

def compressed?(file)
  File.extname(file) == '.gz'
end

#encoder(input) ⇒ Object



58
59
60
# File 'lib/parselogs/file_ops.rb', line 58

def encoder(input)
  input.force_encoding('UTF-8').encode('UTF-16', :invalid => :replace, :replace => '').encode('UTF-8')
end

#file_in_range?(file_date, days_back) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# File 'lib/parselogs/file_ops.rb', line 13

def file_in_range?(file_date,days_back)
  compare_date = Date.today - days_back
  file_date = Date.parse(file_date)
  file_date > compare_date
end

#get_file_with_date(file) ⇒ Object



7
8
9
10
# File 'lib/parselogs/file_ops.rb', line 7

def get_file_with_date(file)
  file_date = File.stat(file).mtime.to_s
  {:name => file, :date => file_date}
end

#logfile?(file) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/parselogs/file_ops.rb', line 38

def logfile?(file)
  if file.match(/.log$|.log.1$/)
    true
  else
    false
  end
end

#parse_compressed_file(file, search) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/parselogs/file_ops.rb', line 29

def parse_compressed_file(file,search)
  File.open(file) do |f|
    gz = Zlib::GzipReader.new(f)
    gz.each_line do |line|
      parse_line(line,search)
    end
  end
end

#parse_file(file, search) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/parselogs/file_ops.rb', line 19

def parse_file(file,search)
  if logfile?(file)
    File.open(file) do |open_file|
      open_file.each do |line|
        parse_line(line,search)
      end
    end
  end
end

#parse_line(line, search) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/parselogs/file_ops.rb', line 50

def parse_line(line,search)
  match_data = []
  line = encoder(line)
  if line.match(search)
    puts line
  end
end