Class: Slk::Formatters::MessageFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/formatters/message_formatter.rb

Overview

Formats Slack messages for terminal display or JSON output rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(output:, mention_replacer:, emoji_replacer:, cache_store:, api_client: nil, text_processor: nil, on_debug: nil) ⇒ MessageFormatter

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/slk/formatters/message_formatter.rb', line 9

def initialize(output:, mention_replacer:, emoji_replacer:, cache_store:, api_client: nil,
               text_processor: nil, on_debug: nil)
  @output = output
  @cache = cache_store
  @api_client = api_client
  @on_debug = on_debug
  @text_processor = text_processor || TextProcessor.new(
    mention_replacer: mention_replacer,
    emoji_replacer: emoji_replacer
  )
  @reaction_formatter = build_reaction_formatter(output, emoji_replacer, cache_store)
  @json_formatter = JsonMessageFormatter.new(cache_store: cache_store)
end

Instance Method Details

#build_reaction_formatter(output, emoji_replacer, cache_store) ⇒ Object

rubocop:enable Metrics/ParameterLists



24
25
26
# File 'lib/slk/formatters/message_formatter.rb', line 24

def build_reaction_formatter(output, emoji_replacer, cache_store)
  ReactionFormatter.new(output: output, emoji_replacer: emoji_replacer, cache_store: cache_store)
end

#format(message, workspace:, options: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/slk/formatters/message_formatter.rb', line 28

def format(message, workspace:, options: {})
  username = resolve_username(message, workspace, options)
  timestamp = format_timestamp(message.timestamp)
  text = @text_processor.process(message.text, workspace, options)

  header = build_header(timestamp, username)
  display_text = build_display_text(text, message, header, options)
  main_line = "#{header} #{display_text}"

  build_output_lines(main_line, message, workspace, options, display_text)
end

#format_json(message, workspace: nil, options: {}) ⇒ Object



57
58
59
# File 'lib/slk/formatters/message_formatter.rb', line 57

def format_json(message, workspace: nil, options: {})
  @json_formatter.format(message, workspace: workspace, options: options)
end

#format_reaction_inline(message, options) ⇒ Object



53
54
55
# File 'lib/slk/formatters/message_formatter.rb', line 53

def format_reaction_inline(message, options)
  @reaction_formatter.format_inline(message.reactions, options)
end

#format_simple(message, workspace:, options: {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/slk/formatters/message_formatter.rb', line 40

def format_simple(message, workspace:, options: {})
  username = resolve_username(message, workspace, options)
  timestamp = format_timestamp(message.timestamp)
  text = @text_processor.process(message.text, workspace, options)

  reaction_text = ''
  unless options[:no_reactions] || message.reactions.empty?
    reaction_text = format_reaction_inline(message, options)
  end

  "#{@output.blue("[#{timestamp}]")} #{@output.bold(username)}: #{text}#{reaction_text}"
end