Class: Codebot::Formatter
- Inherits:
-
Object
- Object
- Codebot::Formatter
- Defined in:
- lib/codebot/formatter.rb
Overview
This class formats events.
Direct Known Subclasses
Codebot::Formatters::CommitComment, Codebot::Formatters::Fork, Codebot::Formatters::Gitlab::IssueHook, Codebot::Formatters::Gitlab::JobHook, Codebot::Formatters::Gitlab::MergeRequestHook, Codebot::Formatters::Gitlab::NoteHook, Codebot::Formatters::Gitlab::PipelineHook, Codebot::Formatters::Gitlab::PushHook, Codebot::Formatters::Gitlab::WikiPageHook, Codebot::Formatters::Gollum, Codebot::Formatters::IssueComment, Codebot::Formatters::Issues, Codebot::Formatters::Ping, Codebot::Formatters::Public, Codebot::Formatters::PullRequest, Codebot::Formatters::PullRequestReviewComment, Codebot::Formatters::Push, Codebot::Formatters::Watch
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
The JSON payload object.
Instance Method Summary collapse
-
#abbreviate(text, suffix: ' ...', length: 200) {|String| ... } ⇒ Object
Truncates the given text, appending a suffix if it was above the allowed length.
-
#action ⇒ String?
Extracts the action from the payload.
-
#ary_to_sentence(ary, empty_sentence = nil) ⇒ String
Constructs a sentence from array elements, connecting them with commas and conjunctions.
-
#closed? ⇒ Boolean
Checks whether the action is ‘closed’.
-
#extract(*path) ⇒ Object?
Safely extracts a value from a JSON object.
-
#format ⇒ Array<String>
Formats IRC messages for an unknown event.
-
#format_branch(branch) ⇒ String
Formats a branch name.
-
#format_dangerous(text) ⇒ String
Formats the name of a potentially dangerous operation, such as a deletion or force-push.
-
#format_event(name) ⇒ String
Formats the name of a webhook event.
-
#format_hash(hash) ⇒ String
Formats a commit hash.
-
#format_number(num, singular = nil, plural = nil) ⇒ String
Formats a number.
-
#format_repository(repository) ⇒ String
Formats a repository name.
-
#format_url(url) ⇒ String
Formats a URL.
-
#format_user(user) ⇒ String
Formats a user name.
- #gitlab_action ⇒ Object
-
#gitlab_closed? ⇒ Boolean
Checks whether the action is ‘closed’.
- #gitlab_opened? ⇒ Boolean
- #gitlab_repository_url ⇒ Object
- #gitlab_url ⇒ Object
-
#initialize(payload, shortener) ⇒ Formatter
constructor
Initializes a new formatter.
-
#opened? ⇒ Boolean
Checks whether the action is ‘opened’.
-
#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.
-
#repository_name ⇒ String?
Extracts the repository name from the payload.
-
#repository_url ⇒ String?
Extracts the repository URL from the payload.
-
#sanitize(text) ⇒ String
Sanitize the given text for delivery to an IRC channel.
-
#sender_name ⇒ String?
Extracts the user name of the person who triggered this event.
- #shorten_url(url) ⇒ Object
-
#url ⇒ String
Shortens the summary URL.
Constructor Details
#initialize(payload, shortener) ⇒ Formatter
Initializes a new formatter.
15 16 17 18 |
# File 'lib/codebot/formatter.rb', line 15 def initialize(payload, shortener) @payload = payload @shortener = shortener end |
Instance Attribute Details
#payload ⇒ Object (readonly)
Returns 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.
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 |
#action ⇒ String?
Extracts the action from the payload.
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.
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’.
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.
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 |
#format ⇒ Array<String>
Formats IRC messages for an unknown event.
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.
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.
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.
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.
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.
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.
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.
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.
67 68 69 |
# File 'lib/codebot/formatter.rb', line 67 def format_user(user) ::Cinch::Formatting.format(:silver, user.to_s) end |
#gitlab_action ⇒ Object
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’.
213 214 215 |
# File 'lib/codebot/formatter.rb', line 213 def gitlab_closed? gitlab_action.eql? 'close' end |
#gitlab_opened? ⇒ Boolean
206 207 208 |
# File 'lib/codebot/formatter.rb', line 206 def gitlab_opened? gitlab_action.eql? 'open' end |
#gitlab_repository_url ⇒ Object
177 178 179 |
# File 'lib/codebot/formatter.rb', line 177 def gitlab_repository_url extract(:repository, :homepage) end |
#gitlab_url ⇒ Object
35 36 37 |
# File 'lib/codebot/formatter.rb', line 35 def gitlab_url summary_url end |
#opened? ⇒ Boolean
Checks 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.
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_name ⇒ String?
Extracts the repository name from the payload.
166 167 168 |
# File 'lib/codebot/formatter.rb', line 166 def repository_name extract(:repository, :name) end |
#repository_url ⇒ String?
Extracts the repository URL from the payload.
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.
132 133 134 |
# File 'lib/codebot/formatter.rb', line 132 def sanitize(text) ::Cinch::Formatting.unformat(text.to_s.gsub(/[[:space:]]+/, ' ')).strip end |
#sender_name ⇒ String?
Extracts the user name of the person who triggered this event.
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 |
#url ⇒ String
Shortens the summary URL. If this method is used, the child class must implement the #summary_url
method.
31 32 33 |
# File 'lib/codebot/formatter.rb', line 31 def url shorten_url summary_url end |