Class: Slk::Models::DndState
- Inherits:
-
Data
- Object
- Data
- Slk::Models::DndState
- Defined in:
- lib/slk/models/dnd_state.rb
Overview
Current Do Not Disturb state for one workspace, from dnd.info.
Slack reports two independent things through that one endpoint: a manual
snooze ("pause notifications for 2 hours") and the configured DND
schedule ("quiet from 8pm to 8am"). From the outside they are the same
thing — messages do not notify — so active? covers both and source
says which one is responsible.
Instance Attribute Summary collapse
-
#next_end ⇒ Object
readonly
Returns the value of attribute next_end.
-
#next_start ⇒ Object
readonly
Returns the value of attribute next_start.
-
#scheduled ⇒ Object
readonly
Returns the value of attribute scheduled.
-
#snooze_endtime ⇒ Object
readonly
Returns the value of attribute snooze_endtime.
-
#snoozing ⇒ Object
readonly
Returns the value of attribute snoozing.
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
-
#in_scheduled_hours? ⇒ Boolean
next_dnd_*names the next window while DND hours are off and the current one while they are running, so "now falls inside it" is the only reading that means notifications are being held right now. - #source ⇒ Object
-
#to_s ⇒ Object
Empty when notifications are flowing: there is nothing to say, and a "[dnd off]" on every line would bury the workspace where it is on.
-
#until_label ⇒ Object
Same-day times need no date; an overnight schedule ending tomorrow morning would otherwise read as "until 8:00am" of a day already gone.
-
#until_time ⇒ Object
The later of the two when both apply: notifications stay off until every reason for holding them has expired, not the first.
Instance Attribute Details
#next_end ⇒ Object (readonly)
Returns the value of attribute next_end
14 15 16 |
# File 'lib/slk/models/dnd_state.rb', line 14 def next_end @next_end end |
#next_start ⇒ Object (readonly)
Returns the value of attribute next_start
14 15 16 |
# File 'lib/slk/models/dnd_state.rb', line 14 def next_start @next_start end |
#scheduled ⇒ Object (readonly)
Returns the value of attribute scheduled
14 15 16 |
# File 'lib/slk/models/dnd_state.rb', line 14 def scheduled @scheduled end |
#snooze_endtime ⇒ Object (readonly)
Returns the value of attribute snooze_endtime
14 15 16 |
# File 'lib/slk/models/dnd_state.rb', line 14 def snooze_endtime @snooze_endtime end |
#snoozing ⇒ Object (readonly)
Returns the value of attribute snoozing
14 15 16 |
# File 'lib/slk/models/dnd_state.rb', line 14 def snoozing @snoozing end |
Class Method Details
.from_api(data) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/slk/models/dnd_state.rb', line 15 def self.from_api(data) data = {} unless data.is_a?(Hash) new( snoozing: data['snooze_enabled'] == true, snooze_endtime: data['snooze_endtime'].to_i, scheduled: data['dnd_enabled'] == true, next_start: data['next_dnd_start_ts'].to_i, next_end: data['next_dnd_end_ts'].to_i ) end |
Instance Method Details
#active? ⇒ Boolean
27 |
# File 'lib/slk/models/dnd_state.rb', line 27 def active? = snoozing || in_scheduled_hours? |
#in_scheduled_hours? ⇒ Boolean
next_dnd_* names the next window while DND hours are off and the
current one while they are running, so "now falls inside it" is the
only reading that means notifications are being held right now.
Slack reports 1 for both schedule timestamps when no schedule is
configured, so a bare positive? would read that as a window that
opened in 1970 and never closed.
36 37 38 39 40 41 |
# File 'lib/slk/models/dnd_state.rb', line 36 def in_scheduled_hours? return false unless scheduled && next_start > 1 && next_end > next_start now = Time.now.to_i next_start <= now && now < next_end end |
#source ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/slk/models/dnd_state.rb', line 43 def source return :both if snoozing && in_scheduled_hours? return :snooze if snoozing return :schedule if in_scheduled_hours? nil end |
#to_s ⇒ Object
Empty when notifications are flowing: there is nothing to say, and a "[dnd off]" on every line would bury the workspace where it is on.
71 72 73 74 75 76 77 78 |
# File 'lib/slk/models/dnd_state.rb', line 71 def to_s return '' unless active? label = until_label # A snooze with no end time is a state Slack can report; saying "[dnd]" # is honest about not knowing when it lifts. label ? "[dnd until #{label}]" : '[dnd]' end |
#until_label ⇒ Object
Same-day times need no date; an overnight schedule ending tomorrow morning would otherwise read as "until 8:00am" of a day already gone.
64 65 66 67 |
# File 'lib/slk/models/dnd_state.rb', line 64 def until_label finish = until_time or return nil finish.to_date == Date.today ? finish.strftime('%-l:%M%P') : finish.strftime('%a %-l:%M%P') end |
#until_time ⇒ Object
The later of the two when both apply: notifications stay off until every reason for holding them has expired, not the first.
53 54 55 56 57 58 59 60 |
# File 'lib/slk/models/dnd_state.rb', line 53 def until_time finishes = [] finishes << snooze_endtime if snoozing && snooze_endtime.positive? finishes << next_end if in_scheduled_hours? finish = finishes.max finish ? Time.at(finish) : nil end |