Class: CommitCommentTools::ReportParser

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-comment-tools/report-parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReportParser

Returns a new instance of ReportParser.



32
33
34
# File 'lib/commit-comment-tools/report-parser.rb', line 32

def initialize
  @person_reports = {}
end

Class Method Details

.parse(report_directory = nil) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/commit-comment-tools/report-parser.rb', line 23

def parse(report_directory=nil)
  report_directory ||= "."
  report_unexpanded_path = File.join(report_directory, "**", "*.txt")
  report_file_paths = Dir.glob(report_unexpanded_path)

  new.parse(report_file_paths)
end

Instance Method Details

#entry(date, read_ratio, comment) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/commit-comment-tools/report-parser.rb', line 80

def entry(date, read_ratio, comment)
  {
    :date       => date,
    :read_ratio => read_ratio,
    :comment    => comment.chomp
  }
end

#generate_daily_report(entries) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/commit-comment-tools/report-parser.rb', line 88

def generate_daily_report(entries)
  daily_report = {}

  entries.each do |entry|
    date = entry.delete(:date)
    daily_report[date] = entry
  end
  daily_report
end

#parse(report_files) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/commit-comment-tools/report-parser.rb', line 36

def parse(report_files)
  report_files.each do |report_file|
    person_name = File.basename(report_file, ".txt")
    next unless person_name == person_name.downcase
    daily_entries = parse_file(report_file)
    @person_reports[person_name] = generate_daily_report(daily_entries)
  end

  @person_reports
end

#parse_file(report_file) ⇒ Object



47
48
49
50
51
# File 'lib/commit-comment-tools/report-parser.rb', line 47

def parse_file(report_file)
  File.open(report_file, "r") do |report_io|
    parse_stream(report_io)
  end
end

#parse_stream(report_io) ⇒ Object



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
# File 'lib/commit-comment-tools/report-parser.rb', line 53

def parse_stream(report_io)
  entries = []
  date = ""
  read_ratio = ""
  comment = ""

  report_io.each_line.with_index do |line, line_number|
    case line.chomp
    when /\A(\d\d\d\d-\d+-\d+):(\d+)%:?(.*)\z/
      unless line_number.zero?
        entries << entry(date, read_ratio, comment)
      end

      date = $1
      read_ratio = $2
      comment = $3
    when /\A\s+/
      comment << $POSTMATCH << "\n"
    end
  end

  unless comment.empty?
    entries << entry(date, read_ratio, comment)
  end
  entries
end