Module: Slack::BlockKit::Formatting

Extended by:
Formatting
Included in:
Formatting
Defined in:
lib/slack/block_kit/formatting.rb

Overview

Formatting contains some utility functions to help formatting text

See: api.slack.com/reference/surfaces/formatting

Constant Summary collapse

DATE_FORMAT_TOKENS =
[
  # 2014-02-18
  DATE_NUM = '{date_num}',

  # February 18th, 2014
  DATE = '{date}',

  # Feb 18, 2014
  DATE_SHORT = '{date_short}',

  # Tuesday, February 18th, 2014
  DATE_LONG = '{date_long}',

  # February 18th, 2014 but uses "yesterday", "today", or "tomorrow"
  # where
  # appropriate
  DATE_PRETTY = '{date_pretty}',

  # Feb 18, 2014 but uses "yesterday", "today", or "tomorrow" where
  # appropriate
  DATE_SHORT_PRETTY = '{date_short_pretty}',

  # Tuesday, February 18th, 2014 but uses "yesterday", "today", or
  # "tomorrow" where appropriate
  DATE_LONG_PRETTY = '{date_long_pretty}',

  # 6:39 AM or 6:39 PM / 06:39 or 18:39 (depending on user preferences)
  TIME = '{time}',

  # 6:39:45 AM 6:39:42 PM / 06:39:45 or 18:39:42 (depending on user
  # preferences)
  TIME_SECS = '{time_secs}'
].freeze

Instance Method Summary collapse

Instance Method Details

#at_channelObject

Mention @channel (all users in the current channel)

See: api.slack.com/reference/surfaces/formatting#special-mentions



91
92
93
# File 'lib/slack/block_kit/formatting.rb', line 91

def at_channel
  '<!channel>'
end

#at_everyoneObject

Mention @everyone (all users in #general)

See: api.slack.com/reference/surfaces/formatting#special-mentions



98
99
100
# File 'lib/slack/block_kit/formatting.rb', line 98

def at_everyone
  '<!everyone>'
end

#at_hereObject

Mention @here (all active users in the current channel)

See: api.slack.com/reference/surfaces/formatting#special-mentions



83
84
85
86
# File 'lib/slack/block_kit/formatting.rb', line 83

def at_here
  # additional "|here" is for supporting very old mobile clients apparently!
  '<!here|here>'
end

#channel(identifier) ⇒ Object



62
63
64
# File 'lib/slack/block_kit/formatting.rb', line 62

def channel(identifier)
  "<##{identifier}>"
end

#date(timestamp, token_string:, link: nil, fallback_text: nil) ⇒ Object

Localise a UNIX timestamp to the user’s local timezone

See: api.slack.com/reference/surfaces/formatting#date-formatting



105
106
107
108
109
110
# File 'lib/slack/block_kit/formatting.rb', line 105

def date(timestamp, token_string:, link: nil, fallback_text: nil)
  datetime_str = [timestamp, token_string, link].compact.join('^')
  str = [datetime_str, fallback_text].compact.join('|')

  "<!date^#{str}>"
end

#escape(text) ⇒ Object

Escape special characters (<,>,&) with their HTML entity code so Slack’s text parsers knows to interpret them as literals.

See: api.slack.com/reference/surfaces/formatting#escaping



116
117
118
119
120
121
# File 'lib/slack/block_kit/formatting.rb', line 116

def escape(text)
  text
    .gsub('&', '&amp;')
    .gsub('>', '&gt;')
    .gsub('<', '&lt;')
end

#group(identifier) ⇒ Object



76
77
78
# File 'lib/slack/block_kit/formatting.rb', line 76

def group(identifier)
  "<!subteam^#{identifier}>"
end


48
49
50
# File 'lib/slack/block_kit/formatting.rb', line 48

def link(url, link_text: nil)
  "<#{[url, link_text].compact.join('|')}>"
end

#mailto(address, link_text: nil) ⇒ Object



55
56
57
# File 'lib/slack/block_kit/formatting.rb', line 55

def mailto(address, link_text: nil)
  "<mailto:#{[address, link_text].compact.join('|')}>"
end

#user(identifier) ⇒ Object



69
70
71
# File 'lib/slack/block_kit/formatting.rb', line 69

def user(identifier)
  "<@#{identifier}>"
end