Class: SlackRubyProgressBar

Inherits:
Object
  • Object
show all
Defined in:
lib/slack-ruby-progress-bar.rb,
lib/slack-ruby-progress-bar/version.rb

Constant Summary collapse

MIN_PROGRESS =
0
MAX_PROGRESS =
100
MAX_BAR_CHARACTERS =
10
BAR_CHARACTER =
"\u2588"
BAR_CHARACTERS =
{
  empty: "\u{2B1C}",
  black: "\u{2B1B}",
  green: "\u{1F7E9}",
  red: "\u{1F7E5}",
  orange: "\u{1F7E7}",
  yellow: "\u{1F7E8}",
  blue: "\u{1F7E6}",
  purple: "\u{1F7EA}",
  brown: "\u{1F7EB}"
}.freeze
VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel:, username: nil, icon_emoji: nil, icon_url: nil, slack_token: nil, show_percent: true, bar_color: :green) ⇒ SlackRubyProgressBar

Returns a new instance of SlackRubyProgressBar.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slack-ruby-progress-bar.rb', line 25

def initialize(channel:, username: nil, icon_emoji: nil, icon_url: nil, slack_token: nil, show_percent: true, bar_color: :green)
  @slack_client = Slack::Web::Client.new(token: slack_token || ENV.fetch('SLACK_API_TOKEN', ''))
  @progress = MIN_PROGRESS
  @progress_text = ''
  @show_percent = show_percent
  @bar_color = bar_color
  @channel = channel
  @username = username
  @icon_emoji = icon_emoji
  @icon_url = icon_url
  @slack_message = nil
end

Instance Attribute Details

#progressObject

Returns the value of attribute progress.



23
24
25
# File 'lib/slack-ruby-progress-bar.rb', line 23

def progress
  @progress
end

#progress_textObject

Returns the value of attribute progress_text.



23
24
25
# File 'lib/slack-ruby-progress-bar.rb', line 23

def progress_text
  @progress_text
end

Instance Method Details

#bar_segment_emptyObject



95
96
97
# File 'lib/slack-ruby-progress-bar.rb', line 95

def bar_segment_empty
  BAR_CHARACTERS[:empty]
end

#bar_segment_filledObject



91
92
93
# File 'lib/slack-ruby-progress-bar.rb', line 91

def bar_segment_filled
  BAR_CHARACTERS[@bar_color.to_sym]
end

#clearObject



63
64
65
# File 'lib/slack-ruby-progress-bar.rb', line 63

def clear
  update(progress: MIN_PROGRESS, progress_text: '')
end

#decrementObject



59
60
61
# File 'lib/slack-ruby-progress-bar.rb', line 59

def decrement
  update(progress: @progress - 1)
end

#elapsed_timeObject



111
112
113
# File 'lib/slack-ruby-progress-bar.rb', line 111

def elapsed_time
  Time.now - (@started_at || Time.now)
end

#finishObject



67
68
69
70
# File 'lib/slack-ruby-progress-bar.rb', line 67

def finish
  update(progress: MAX_PROGRESS, progress_text: "Finished in #{formatted_elapsed_time}")
  @started_at = nil
end

#finished?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/slack-ruby-progress-bar.rb', line 72

def finished?
  @progress >= MAX_PROGRESS
end

#formatted_elapsed_timeObject



115
116
117
# File 'lib/slack-ruby-progress-bar.rb', line 115

def formatted_elapsed_time
  elapsed_time > 300 ? "#{(elapsed_time / 60).round(2)} minutes" : "#{elapsed_time.round(2)} seconds"
end

#formatted_progress_textObject



105
106
107
108
109
# File 'lib/slack-ruby-progress-bar.rb', line 105

def formatted_progress_text
  return '' if @progress_text.strip.empty?

  "\n#{@progress_text}"
end

#incrementObject



55
56
57
# File 'lib/slack-ruby-progress-bar.rb', line 55

def increment
  update(progress: @progress + 1)
end

#percentage_textObject



99
100
101
102
103
# File 'lib/slack-ruby-progress-bar.rb', line 99

def percentage_text
  return '' unless @show_percent

  " #{@progress}%"
end

#post_to_slackObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/slack-ruby-progress-bar.rb', line 119

def post_to_slack
  if @slack_message
    @slack_client.chat_update(channel: @slack_message.channel,
                              ts: @slack_message.ts,
                              text: rendered_bar + formatted_progress_text)
  else
    @slack_message = @slack_client.chat_postMessage(channel: @channel,
                                                    username: @username,
                                                    icon_url: @icon_url,
                                                    icon_emoji: @icon_emoji,
                                                    text: rendered_bar + formatted_progress_text)
  end
end

#rendered_barObject



84
85
86
87
88
89
# File 'lib/slack-ruby-progress-bar.rb', line 84

def rendered_bar
  fill_count = ((@progress / MAX_PROGRESS.to_f) * MAX_PROGRESS).to_i / MAX_BAR_CHARACTERS
  empty_count = MAX_BAR_CHARACTERS - fill_count

  "#{bar_segment_filled * fill_count}#{bar_segment_empty * empty_count}" + percentage_text
end

#update(progress: @progress, progress_text: @progress_text, bar_color: @bar_color) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/slack-ruby-progress-bar.rb', line 38

def update(progress: @progress, progress_text: @progress_text, bar_color: @bar_color)
  @started_at ||= Time.now

  @progress = progress
  @progress = MIN_PROGRESS if @progress < MIN_PROGRESS
  @progress = MAX_PROGRESS if @progress > MAX_PROGRESS

  @progress_text = progress_text
  @bar_color = bar_color

  post_to_slack
end

#update_thread(message) ⇒ Object



51
52
53
# File 'lib/slack-ruby-progress-bar.rb', line 51

def update_thread(message)
  @slack_client.chat_postMessage(channel: @channel, thread_ts: @slack_message.ts, text: message)
end