Class: Hunter::Hunt

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_to_match) ⇒ Hunt

Returns a new instance of Hunt.



10
11
12
# File 'lib/hunter.rb', line 10

def initialize(string_to_match)
  @string_to_match = string_to_match
end

Instance Attribute Details

#string_to_matchObject

Returns the value of attribute string_to_match.



8
9
10
# File 'lib/hunter.rb', line 8

def string_to_match
  @string_to_match
end

Instance Method Details

#look_for_match(line) ⇒ Object

HELPERS



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

def look_for_match(line)
  true if line.include? @string_to_match
end

#look_in(file) ⇒ Object

ONE FILE



15
16
17
18
19
20
21
22
23
24
# File 'lib/hunter.rb', line 15

def look_in(file)
  counter = 0
  File.open(file, "r") do |infile|
    while line = infile.gets
      counter += 1 if look_for_match(line)
    end
  end
  puts counter
  counter
end

#look_in_archived(file) ⇒ Object

ONE .GZ FILE



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hunter.rb', line 27

def look_in_archived(file)
  counter = 0
  File.open(file) do |f|
    gz = Zlib::GzipReader.new(f)
    gz.each_line do |line|
      counter += 1 if look_for_match(line)
    end
    gz.close
  end
  puts counter
  counter
end

#look_through_archive(directory, file_basename = "*", compression = "gz") ⇒ Object

ARCHIVE OF .GZ FILES



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hunter.rb', line 41

def look_through_archive(directory, file_basename = "*", compression = "gz")
  counter = 0
  files = Dir.glob("/#{directory}/#{file_basename}.*.#{compression}")
  files = sort_archive(files)

  files.each do |file|
    matches = look_in_archived(file)
    counter += matches
    puts "#{file}: #{matches}"
  end

  puts "TOTAL: " + counter.to_s
  counter
end

#sort_archive(files) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/hunter.rb', line 62

def sort_archive(files)
  r = Regexp.new(".([0-9]+).gz")
  sorted_files = files.sort do |f1, f2|
    n1 = r.match(f1)[1].to_i
    n2 = r.match(f2)[1].to_i
    n1 <=> n2
  end
  sorted_files
end