Class: Codebot::Formatters::Ping

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

Overview

This class formats ping 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

#default_formatObject



22
23
24
# File 'lib/codebot/formatters/ping.rb', line 22

def default_format
  '[%<scope>s] %<sender>s added a webhook for %<events>s'
end

#formatArray<String>

Formats IRC messages for a ping event.

Returns:

  • (Array<String>)

    the formatted messages



10
11
12
# File 'lib/codebot/formatters/ping.rb', line 10

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

#format_eventsObject



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

def format_events
  if hook_events.empty?
    'no events'
  elsif hook_events.include? '*'
    format_event 'all events'
  else
    format_events_some
  end
end

#format_events_someObject



46
47
48
49
50
51
52
53
54
# File 'lib/codebot/formatters/ping.rb', line 46

def format_events_some
  if hook_events.length > 5
    "#{format_number(hook_events.length)} events"
  elsif hook_events.one?
    "the #{format_event hook_events.first} event"
  else
    "the #{formatted_hook_events} events"
  end
end

#format_scopeString

Formats the name of the repository or organization the webhook belongs to.

Returns:

  • (String)

    the formatted scope



68
69
70
71
72
73
74
75
76
# File 'lib/codebot/formatters/ping.rb', line 68

def format_scope
  case extract(:hook, :type)
  when /\Aorganization\z/i
    format_user extract(:organization, :login)
  when /\Arepository\z/i
     = extract(:repository, :owner, :login)
    "#{format_user }/#{format_repository repository_name}"
  end
end

#formatted_hook_eventsObject



60
61
62
# File 'lib/codebot/formatters/ping.rb', line 60

def formatted_hook_events
  ary_to_sentence(hook_events.sort.map { |event| format_event event })
end

#hook_eventsObject



56
57
58
# File 'lib/codebot/formatters/ping.rb', line 56

def hook_events
  extract(:hook, :events).to_a.uniq
end

#summaryObject



14
15
16
17
18
19
20
# File 'lib/codebot/formatters/ping.rb', line 14

def summary
  default_format % {
    scope: format_scope,
    sender: format_user(sender_name),
    events: format_events
  }
end

#summary_urlObject



26
27
28
29
30
31
32
33
34
# File 'lib/codebot/formatters/ping.rb', line 26

def summary_url
  case extract(:hook, :type)
  when /\Aorganization\z/i
     = extract(:organization, :login).to_s
    "https://github.com/#{}"
  when /\Arepository\z/i
    extract(:repository, :html_url).to_s
  end
end