Class: Codebot::Formatters::Gollum

Inherits:
Codebot::Formatter show all
Defined in:
lib/codebot/formatters/gollum.rb

Overview

This class formats gollum events.

Instance Attribute Summary

Attributes inherited from Codebot::Formatter

#payload

Instance Method Summary collapse

Methods inherited from Codebot::Formatter

#abbreviate, #action, #ary_to_sentence, #closed?, #extract, #format_branch, #format_dangerous, #format_event, #format_hash, #format_number, #format_repository, #format_url, #format_user, #gitlab_action, #gitlab_closed?, #gitlab_opened?, #gitlab_repository_url, #gitlab_url, #initialize, #opened?, #prettify, #repository_name, #repository_url, #sanitize, #sender_name, #shorten_url, #url

Constructor Details

This class inherits a constructor from Codebot::Formatter

Instance Method Details

#action_countsObject



48
49
50
51
52
# File 'lib/codebot/formatters/gollum.rb', line 48

def action_counts
  Hash.new(0).tap do |hash|
    pages.each { |page| hash[page['action']] += 1 }
  end.sort
end

#default_formatObject



25
26
27
# File 'lib/codebot/formatters/gollum.rb', line 25

def default_format
  '[%<repository>s] %<sender>s %<summary>s'
end

#formatArray<String>

Formats IRC messages for a gollum event.

Returns:

  • (Array<String>)

    the formatted messages



13
14
15
# File 'lib/codebot/formatters/gollum.rb', line 13

def format
  ["#{summary}: #{format_url url}"]
end

#multi_page_summaryObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/codebot/formatters/gollum.rb', line 36

def multi_page_summary
  actions = []
  counts = action_counts
  counts.each { |verb, num| actions << "#{verb} #{format_number num}" }
  changes = ary_to_sentence(
    actions,
    'pushed an empty commit that did not affect any'
  )
  singular_noun = counts.last.to_a.last.to_i.eql?(1)
  "#{changes} wiki #{singular_noun ? 'page' : 'pages'}"
end

#pagesObject



62
63
64
# File 'lib/codebot/formatters/gollum.rb', line 62

def pages
  extract(:pages).to_a
end

#single_page_summaryObject



29
30
31
32
33
34
# File 'lib/codebot/formatters/gollum.rb', line 29

def single_page_summary
  page = pages.first.to_h
  short = prettify page['summary']
  suffix = ": #{short}" unless short.empty?
  "#{page['action']} wiki page #{page['title']}#{suffix}"
end

#summaryObject



17
18
19
20
21
22
23
# File 'lib/codebot/formatters/gollum.rb', line 17

def summary
  default_format % {
    repository: format_repository(repository_name),
    sender: format_user(sender_name),
    summary: (pages.one? ? single_page_summary : multi_page_summary)
  }
end

#summary_urlObject



54
55
56
57
58
59
60
# File 'lib/codebot/formatters/gollum.rb', line 54

def summary_url
  if pages.one?
    pages.first['html_url'].to_s
  else
    "#{repository_url}/wiki"
  end
end