Class: SlackRubyProgressBar
- Inherits:
-
Object
- Object
- SlackRubyProgressBar
- 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
-
#progress ⇒ Object
Returns the value of attribute progress.
-
#progress_text ⇒ Object
Returns the value of attribute progress_text.
Instance Method Summary collapse
- #bar_segment_empty ⇒ Object
- #bar_segment_filled ⇒ Object
- #clear ⇒ Object
- #decrement ⇒ Object
- #elapsed_time ⇒ Object
- #finish ⇒ Object
- #finished? ⇒ Boolean
- #formatted_elapsed_time ⇒ Object
- #formatted_progress_text ⇒ Object
- #increment ⇒ Object
-
#initialize(channel:, username: nil, icon_emoji: nil, icon_url: nil, slack_token: nil, show_percent: true, bar_color: :green) ⇒ SlackRubyProgressBar
constructor
A new instance of SlackRubyProgressBar.
- #percentage_text ⇒ Object
- #post_to_slack ⇒ Object
- #rendered_bar ⇒ Object
- #update(progress: @progress, progress_text: @progress_text, bar_color: @bar_color) ⇒ Object
- #update_thread(message) ⇒ Object
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 = @channel = channel @username = username @icon_emoji = icon_emoji @icon_url = icon_url @slack_message = nil end |
Instance Attribute Details
#progress ⇒ Object
Returns the value of attribute progress.
23 24 25 |
# File 'lib/slack-ruby-progress-bar.rb', line 23 def progress @progress end |
#progress_text ⇒ Object
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_empty ⇒ Object
95 96 97 |
# File 'lib/slack-ruby-progress-bar.rb', line 95 def BAR_CHARACTERS[:empty] end |
#bar_segment_filled ⇒ Object
91 92 93 |
# File 'lib/slack-ruby-progress-bar.rb', line 91 def BAR_CHARACTERS[@bar_color.to_sym] end |
#clear ⇒ Object
63 64 65 |
# File 'lib/slack-ruby-progress-bar.rb', line 63 def clear update(progress: MIN_PROGRESS, progress_text: '') end |
#decrement ⇒ Object
59 60 61 |
# File 'lib/slack-ruby-progress-bar.rb', line 59 def decrement update(progress: @progress - 1) end |
#elapsed_time ⇒ Object
111 112 113 |
# File 'lib/slack-ruby-progress-bar.rb', line 111 def elapsed_time Time.now - (@started_at || Time.now) end |
#finish ⇒ Object
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
72 73 74 |
# File 'lib/slack-ruby-progress-bar.rb', line 72 def finished? @progress >= MAX_PROGRESS end |
#formatted_elapsed_time ⇒ Object
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_text ⇒ Object
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 |
#increment ⇒ Object
55 56 57 |
# File 'lib/slack-ruby-progress-bar.rb', line 55 def increment update(progress: @progress + 1) end |
#percentage_text ⇒ Object
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_slack ⇒ Object
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: + formatted_progress_text) else @slack_message = @slack_client.chat_postMessage(channel: @channel, username: @username, icon_url: @icon_url, icon_emoji: @icon_emoji, text: + formatted_progress_text) end end |
#rendered_bar ⇒ Object
84 85 86 87 88 89 |
# File 'lib/slack-ruby-progress-bar.rb', line 84 def fill_count = ((@progress / MAX_PROGRESS.to_f) * MAX_PROGRESS).to_i / MAX_BAR_CHARACTERS empty_count = MAX_BAR_CHARACTERS - fill_count "#{ * fill_count}#{ * 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 = post_to_slack end |
#update_thread(message) ⇒ Object
51 52 53 |
# File 'lib/slack-ruby-progress-bar.rb', line 51 def update_thread() @slack_client.chat_postMessage(channel: @channel, thread_ts: @slack_message.ts, text: ) end |