Module: CodexNotify::MessageFormatter

Defined in:
lib/codex_notify/message_formatter.rb

Defined Under Namespace

Classes: Message

Constant Summary collapse

SLACK_SAFE_LENGTH =
3500
CONTINUATION_LABEL =
'(cont.)'
PRESENTATIONS =
%i[plain block].freeze

Class Method Summary collapse

Class Method Details

.build_root_message(title, cwd, user_name: 'user', session_id: nil) ⇒ Object



55
56
57
58
# File 'lib/codex_notify/message_formatter.rb', line 55

def build_root_message(title, cwd, user_name: 'user', session_id: nil)
  body = root_body(cwd, user_name:, session_id:)
  message(title:, body:, presentation: :block)
end

.build_root_text(title, cwd, user_name: 'user', session_id: nil) ⇒ Object



60
61
62
# File 'lib/codex_notify/message_formatter.rb', line 60

def build_root_text(title, cwd, user_name: 'user', session_id: nil)
  fmt_block(title, root_body(cwd, user_name:, session_id:))
end

.chunks(message, max_length: SLACK_SAFE_LENGTH) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/codex_notify/message_formatter.rb', line 24

def chunks(message, max_length: SLACK_SAFE_LENGTH)
  return enum_for(__method__, message, max_length:) unless block_given?

  validate_max_length(max_length, message.presentation)
  body = message.body.gsub("\r\n", "\n")
  index = 0
  chunk_index = 0

  loop do
    title = fit_title(
      message.title,
      message.presentation,
      max_length,
      body.empty?,
      continuation: chunk_index.positive?
    )
    capacity = max_length - render(title, '', message.presentation).length
    part = body[index, capacity] || ''
    yield render(title, part, message.presentation)

    index += part.length
    break if index >= body.length

    chunk_index += 1
  end
end

.fmt_block(title, body) ⇒ Object



51
52
53
# File 'lib/codex_notify/message_formatter.rb', line 51

def fmt_block(title, body)
  render(title.to_s, body.to_s, :block)
end

.fmt_plain(title, body) ⇒ Object



64
65
66
# File 'lib/codex_notify/message_formatter.rb', line 64

def fmt_plain(title, body)
  render(title.to_s, body.to_s, :plain)
end

.message(title:, body:, presentation:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/codex_notify/message_formatter.rb', line 12

def message(title:, body:, presentation:)
  unless PRESENTATIONS.include?(presentation)
    raise ArgumentError, "unsupported message presentation: #{presentation.inspect}"
  end

  Message.new(
    title: title.to_s.dup.freeze,
    body: body.to_s.dup.freeze,
    presentation:
  )
end