Class: Codebot::Formatter

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

Overview

This class formats events.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload, shortener) ⇒ Formatter

Initializes a new formatter.

Parameters:

  • payload (Object)

    the JSON payload object



15
16
17
18
# File 'lib/codebot/formatter.rb', line 15

def initialize(payload, shortener)
  @payload = payload
  @shortener = shortener
end

Instance Attribute Details

#payloadObject (readonly)

Returns the JSON payload object.

Returns:

  • (Object)

    the JSON payload object



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

def payload
  @payload
end

Instance Method Details

#abbreviate(text, suffix: ' ...', length: 200) {|String| ... } ⇒ Object

Truncates the given text, appending a suffix if it was above the allowed length.

Parameters:

  • text (String)

    the text to truncate

  • suffix (String) (defaults to: ' ...')

    the suffix to append if the text is truncated

  • length (Integer) (defaults to: 200)

    the maximum length including the suffix

Yields:

  • (String)

    the truncated string before the ellipsis is appended



144
145
146
147
148
149
150
# File 'lib/codebot/formatter.rb', line 144

def abbreviate(text, suffix: ' ...', length: 200)
  content_length = length - suffix.length
  short = text.to_s.lines.first.to_s.strip[0...content_length].strip
  yield text if block_given?
  short << suffix unless short.eql? text.to_s.strip
  short
end

#actionString?

Extracts the action from the payload.

Returns:

  • (String, nil)

    the action



184
185
186
# File 'lib/codebot/formatter.rb', line 184

def action
  extract(:action).to_s
end

#ary_to_sentence(ary, empty_sentence = nil) ⇒ String

Constructs a sentence from array elements, connecting them with commas and conjunctions.

Parameters:

  • ary (Array<String>)

    the array

  • empty_sentence (String, nil) (defaults to: nil)

    the sentence to return if the array is empty

Returns:

  • (String)

    the constructed sentence



114
115
116
117
118
119
120
121
122
123
# File 'lib/codebot/formatter.rb', line 114

def ary_to_sentence(ary, empty_sentence = nil)
  case ary.length
  when 0 then empty_sentence.to_s
  when 1 then ary.first
  when 2 then ary.join(' and ')
  else
    *ary, last_element = ary
    ary_to_sentence([ary.join(', '), last_element])
  end
end

#closed?Boolean

Checks whether the action is ‘closed’.

Returns:

  • (Boolean)

    whether the action is ‘closed’.



202
203
204
# File 'lib/codebot/formatter.rb', line 202

def closed?
  action.eql? 'closed'
end

#extract(*path) ⇒ Object?

Safely extracts a value from a JSON object.

Parameters:

  • path (Array<#to_s>)

    the path to traverse

Returns:

  • (Object, nil)

    the extracted object or nil if no object was found at the given path



229
230
231
232
233
234
235
236
# File 'lib/codebot/formatter.rb', line 229

def extract(*path)
  node = payload
  node if path.all? do |sub|
    break unless node.is_a? Hash

    node = node[sub.to_s]
  end
end

#formatArray<String>

Formats IRC messages for an unknown event.

Returns:

  • (Array<String>)

    the formatted messages



23
24
25
# File 'lib/codebot/formatter.rb', line 23

def format
  ['An unknown event occurred']
end

#format_branch(branch) ⇒ String

Formats a branch name.

Parameters:

  • branch (String)

    the name

Returns:

  • (String)

    the formatted name



51
52
53
# File 'lib/codebot/formatter.rb', line 51

def format_branch(branch)
  ::Cinch::Formatting.format(:purple, branch.to_s)
end

#format_dangerous(text) ⇒ String

Formats the name of a potentially dangerous operation, such as a deletion or force-push.

Parameters:

  • text (String)

    the text to format

Returns:

  • (String)

    the formatted text



95
96
97
# File 'lib/codebot/formatter.rb', line 95

def format_dangerous(text)
  ::Cinch::Formatting.format(:red, text.to_s)
end

#format_event(name) ⇒ String

Formats the name of a webhook event.

Parameters:

  • name (String)

    the name to format

Returns:

  • (String)

    the formatted name



103
104
105
# File 'lib/codebot/formatter.rb', line 103

def format_event(name)
  ::Cinch::Formatting.format(:bold, name.to_s)
end

#format_hash(hash) ⇒ String

Formats a commit hash.

Parameters:

  • hash (String)

    the hash

Returns:

  • (String)

    the formatted hash



59
60
61
# File 'lib/codebot/formatter.rb', line 59

def format_hash(hash)
  ::Cinch::Formatting.format(:grey, hash.to_s[0..6])
end

#format_number(num, singular = nil, plural = nil) ⇒ String

Formats a number.

Parameters:

  • num (Integer)

    the number

  • singular (String, nil) (defaults to: nil)

    the singular noun to append to the number

  • plural (String, nil) (defaults to: nil)

    the plural noun to append to the number

Returns:

  • (String)

    the formatted number



85
86
87
88
# File 'lib/codebot/formatter.rb', line 85

def format_number(num, singular = nil, plural = nil)
  bold_num = ::Cinch::Formatting.format(:bold, num.to_s)
  (bold_num + ' ' + (num == 1 ? singular : plural).to_s).strip
end

#format_repository(repository) ⇒ String

Formats a repository name.

Parameters:

  • repository (String)

    the name

Returns:

  • (String)

    the formatted name



43
44
45
# File 'lib/codebot/formatter.rb', line 43

def format_repository(repository)
  ::Cinch::Formatting.format(:pink, repository.to_s)
end

#format_url(url) ⇒ String

Formats a URL.

Parameters:

  • url (String)

    the URL

Returns:

  • (String)

    the formatted URL



75
76
77
# File 'lib/codebot/formatter.rb', line 75

def format_url(url)
  ::Cinch::Formatting.format(:blue, :underline, url.to_s)
end

#format_user(user) ⇒ String

Formats a user name.

Parameters:

  • user (String)

    the name

Returns:

  • (String)

    the formatted name



67
68
69
# File 'lib/codebot/formatter.rb', line 67

def format_user(user)
  ::Cinch::Formatting.format(:silver, user.to_s)
end

#gitlab_actionObject



188
189
190
# File 'lib/codebot/formatter.rb', line 188

def gitlab_action
  extract(:object_attributes, :action).to_s
end

#gitlab_closed?Boolean

Checks whether the action is ‘closed’.

Returns:

  • (Boolean)

    whether the action is ‘closed’.



213
214
215
# File 'lib/codebot/formatter.rb', line 213

def gitlab_closed?
  gitlab_action.eql? 'close'
end

#gitlab_opened?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/codebot/formatter.rb', line 206

def gitlab_opened?
  gitlab_action.eql? 'open'
end

#gitlab_repository_urlObject



177
178
179
# File 'lib/codebot/formatter.rb', line 177

def gitlab_repository_url
  extract(:repository, :homepage)
end

#gitlab_urlObject



35
36
37
# File 'lib/codebot/formatter.rb', line 35

def gitlab_url
  summary_url
end

#opened?Boolean

Checks whether the action is ‘opened’.

Returns:

  • (Boolean)

    whether the action is ‘opened’.



195
196
197
# File 'lib/codebot/formatter.rb', line 195

def opened?
  action.eql? 'opened'
end

#prettify(text) ⇒ String

Abbreviates the given text, removes any trailing punctuation except for the ellipsis appended if the text was truncated, and sanitizes the text for delivery to IRC.

Parameters:

  • text (String)

    the text to process

Returns:

  • (String)

    the processed text



158
159
160
161
# File 'lib/codebot/formatter.rb', line 158

def prettify(text)
  pretty = abbreviate(text) { |short| short.sub!(/[[:punct:]]+\z/, '') }
  sanitize pretty
end

#repository_nameString?

Extracts the repository name from the payload.

Returns:

  • (String, nil)

    the repository name



166
167
168
# File 'lib/codebot/formatter.rb', line 166

def repository_name
  extract(:repository, :name)
end

#repository_urlString?

Extracts the repository URL from the payload.

Returns:

  • (String, nil)

    the repository URL



173
174
175
# File 'lib/codebot/formatter.rb', line 173

def repository_url
  extract(:repository, :html_url)
end

#sanitize(text) ⇒ String

Sanitize the given text for delivery to an IRC channel. Most importantly, this method prevents attackers from injecting arbitrary commands into the bot’s connection by ensuring that the text does not contain any newline characters. Any IRC formatting codes in the text are also removed.

Parameters:

  • text (String)

    the text to sanitize

Returns:

  • (String)

    the sanitized text



132
133
134
# File 'lib/codebot/formatter.rb', line 132

def sanitize(text)
  ::Cinch::Formatting.unformat(text.to_s.gsub(/[[:space:]]+/, ' ')).strip
end

#sender_nameString?

Extracts the user name of the person who triggered this event.

Returns:

  • (String, nil)

    the user name



220
221
222
# File 'lib/codebot/formatter.rb', line 220

def sender_name
  extract(:sender, :login)
end

#shorten_url(url) ⇒ Object



238
239
240
# File 'lib/codebot/formatter.rb', line 238

def shorten_url(url)
  @shortener.shorten_url(url)
end

#urlString

Shortens the summary URL. If this method is used, the child class must implement the #summary_url method.

Returns:

  • (String)

    the shortened summary URL



31
32
33
# File 'lib/codebot/formatter.rb', line 31

def url
  shorten_url summary_url
end