Class: Climer::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/climer/timer.rb

Constant Summary collapse

CARRIAGE_RETURN =
"\r"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration, alert_name = 'Climer::Timer', notification = 'Timer expired!', urgency = :normal) ⇒ Timer

Returns a new instance of Timer.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/climer/timer.rb', line 29

def initialize(duration, alert_name = 'Climer::Timer', notification = 'Timer expired!', urgency = :normal)
  @alert_time = Time.now + duration

  puts "Alert set to activate at #{@alert_time.strftime('%l:%M %p on %B %e, %Y')}"
  print countdown_string

  countdown_update_interval = '1 second'
  job = Rufus::Scheduler.every countdown_update_interval do
    print "#{CARRIAGE_RETURN}#{countdown_string}"
  end

  Rufus::Scheduler.at @alert_time do
    Rufus::Scheduler.scheduler.unschedule(
      job.job_id
    )
    sleep TimeDistribution::SmartDuration.parse(countdown_update_interval).total
    puts "\nALERT! #{notification}"
    Libnotify.show summary: alert_name, body: notification, urgency: urgency
  end
end

Instance Attribute Details

#alert_timeObject (readonly)

Returns the value of attribute alert_time.



26
27
28
# File 'lib/climer/timer.rb', line 26

def alert_time
  @alert_time
end

Instance Method Details

#countdown_stringObject



53
54
55
# File 'lib/climer/timer.rb', line 53

def countdown_string
  time_remaining.format('%h:%M:%S')
end

#time_remainingObject



50
51
52
# File 'lib/climer/timer.rb', line 50

def time_remaining
  Duration.new(@alert_time - Time.now)
end