Class: Pello::Actions::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/pello/actions/report.rb

Constant Summary collapse

POMODORI_ADDED_COMMENT =
/\((\d+) -> (\d+)\)$/.freeze

Instance Method Summary collapse

Instance Method Details

#cached_author_name(author_id) ⇒ Object



57
58
59
60
61
# File 'lib/pello/actions/report.rb', line 57

def cached_author_name(author_id)
  @author_names ||= {}
  @author_names[author_id] ||= Trello::Member.find(author_id).full_name
  @author_names[author_id]
end

#lines_with_pomodori_increases_in(comment) ⇒ Object



39
40
41
# File 'lib/pello/actions/report.rb', line 39

def lines_with_pomodori_increases_in(comment)
  comment.data['text'].split("\n").select { |line| line.match? POMODORI_ADDED_COMMENT }
end

#pello_log?(comment) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/pello/actions/report.rb', line 43

def pello_log?(comment)
  comment.data['text'] =~ /PELLO LOG/
end

#pomodori_before_and_after_from(comment_line) ⇒ Object



35
36
37
# File 'lib/pello/actions/report.rb', line 35

def pomodori_before_and_after_from(comment_line)
  comment_line.match(POMODORI_ADDED_COMMENT)[1, 2].map(&:to_i)
end


52
53
54
55
# File 'lib/pello/actions/report.rb', line 52

def print_counters(counters, label)
  puts "\n-- #{label}:"
  counters.each { |author_name, author_total| puts "* #{author_name}: #{author_total}" }
end

#run(user, board_url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pello/actions/report.rb', line 8

def run(user, board_url)
  board = Pello::Inputs.choose_board user, board_url
  return unless board

  board_totals = {}

  lists = board.lists
  lists.each do |list|
    list_totals = {}
    list.cards.each do |card|
      card.comments.select { |comment| pello_log?(comment) }.each do |comment|
        lines_with_pomodori_increases_in(comment).each do |line|
          author_name = cached_author_name(comment.creator_id)
          pom_before, pom_after = pomodori_before_and_after_from(line)
          update_counters(list_totals, author_name, (pom_after - pom_before))
          update_counters(board_totals, author_name, (pom_after - pom_before))
        end
      end
    end

    print_counters(list_totals, list.name) if list_totals.any?
  end

  print_counters(board_totals, 'Totals')
  puts "\n"
end

#update_counters(counters, author_name, amount_to_add) ⇒ Object



47
48
49
50
# File 'lib/pello/actions/report.rb', line 47

def update_counters(counters, author_name, amount_to_add)
  counters[author_name] ||= 0
  counters[author_name] = counters[author_name] + amount_to_add
end