Class: Hatchet::Reaper::AppAge

Inherits:
Object
  • Object
show all
Defined in:
lib/hatchet/reaper/app_age.rb

Overview

Class for figuring out how old a given time is relative to another time

Expects inputs as a DateTime instance

Example:

time_now = DateTime.parse("2020-07-28T14:40:00Z")
age = AppAge.new(created_at: DateTIme.parse("2020-07-28T14:40:00Z"), time_now: time_now, ttl_minutes: 1)
age.in_minutes => 0.0
age.too_young_to_die? # => true
age.can_delete? # => false
age.sleep_for_ttl #=> 60

Constant Summary collapse

SECONDS_IN_A_DAY =
24 * 60 * 60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(created_at:, ttl_minutes:, time_now: DateTime.now.new_offset(0)) ⇒ AppAge

Returns a new instance of AppAge.



20
21
22
23
24
# File 'lib/hatchet/reaper/app_age.rb', line 20

def initialize(created_at:, ttl_minutes:, time_now: DateTime.now.new_offset(0))
  @seconds_ago = date_time_diff_in_seconds(time_now, created_at)
  @ttl_minutes = ttl_minutes
  @ttl_seconds = ttl_minutes * 60
end

Instance Attribute Details

#ttl_minutesObject (readonly)

Returns the value of attribute ttl_minutes.



18
19
20
# File 'lib/hatchet/reaper/app_age.rb', line 18

def ttl_minutes
  @ttl_minutes
end

Instance Method Details

#can_delete?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/hatchet/reaper/app_age.rb', line 34

def can_delete?
  @seconds_ago > @ttl_seconds
end

#date_time_diff_in_seconds(now, whence) ⇒ Object



26
27
28
# File 'lib/hatchet/reaper/app_age.rb', line 26

def date_time_diff_in_seconds(now, whence)
  (now - whence) * SECONDS_IN_A_DAY
end

#in_minutesObject



44
45
46
# File 'lib/hatchet/reaper/app_age.rb', line 44

def in_minutes
  (@seconds_ago / 60.0).round(2)
end

#sleep_for_ttlObject



38
39
40
41
42
# File 'lib/hatchet/reaper/app_age.rb', line 38

def sleep_for_ttl
  return 0 if can_delete?

  @ttl_seconds - @seconds_ago
end

#too_young_to_die?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/hatchet/reaper/app_age.rb', line 30

def too_young_to_die?
  !can_delete?
end