Class: TimeBombPolicy

Inherits:
AppPolicy show all
Defined in:
lib/models/time_bomb_policy.rb

Overview

Time Bomb policy for this app

Constant Summary collapse

OPTION_KILL =

Constants

'kill'
OPTION_WIPE =
'wipe'
BOMB_OPTIONS =
[OPTION_KILL, OPTION_WIPE].freeze
OPTION_AM =
'am'
OPTION_PM =
'pm'
TIME_OPTIONS =
[OPTION_AM, OPTION_PM].freeze
HOURS_TO_MS =
60 * 60 * 1000
UNIT_HOURS =
'hours'
UNIT_DAYS =
'days'
UNIT_WEEKS =
'weeks'
UNIT_MONTHS =
'months'
ALL_UNITS =
[UNIT_HOURS, UNIT_DAYS, UNIT_WEEKS, UNIT_MONTHS].freeze

Instance Method Summary collapse

Instance Method Details

#check_in_milliseconds(type) ⇒ Object

Return the number of milli seconds between check ins



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/models/time_bomb_policy.rb', line 123

def check_in_milliseconds(type)
  interval = send("#{type}_after_interval").to_i
  case send "#{type}_after_unit"
  when UNIT_HOURS
    interval * HOURS_TO_MS
  when UNIT_DAYS
    24 * interval * HOURS_TO_MS
  when UNIT_WEEKS
    7 * 24 * interval * HOURS_TO_MS
  when UNIT_MONTHS
    30 * 24 * interval * HOURS_TO_MS
  else
    raise "Unknown unit: #{type}"
  end
end

#check_in_policyObject

Determine if the check in, or “Time bomb” policy should be enforced



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/models/time_bomb_policy.rb', line 108

def check_in_policy
  return {} unless check_in_active

  policy = { message: inactivity_message }
  policy[:kill_after] = check_in_milliseconds(:kill) if kill_after_interval.present?
  policy[:wipe_after] = check_in_milliseconds(:wipe) if wipe_after_interval.present?
  { check_in: policy }
rescue StandardError => error
  log_error("Unable to build time bomb check in policy: #{inspect}", error)
  {}
end

#enforce_policy(agent) ⇒ Object

return the policy hash for this agent



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/models/time_bomb_policy.rb', line 48

def enforce_policy(agent)
  policy = super.merge(enforce_time_bomb: 0)
  return policy unless active?

  time_bomb_policies = expiration_policy.merge(check_in_policy).merge(time_window_policy)

  if time_bomb_policies.present?
    policy[:time_bomb] = time_bomb_policies
    policy[:enforce_time_bomb] = 1
  end

  policy
rescue StandardError => error
  log_error("Unable to build time bomb policy for agent: #{agent.inspect} - #{inspect}", error)
  # Return the default policy
  { enforce_time_bomb: 0 }
end

#expiration_policyObject

determine if we should enforce the expire at policy



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/models/time_bomb_policy.rb', line 88

def expiration_policy
  return {} unless expiration_active? && expire_at.present?

  {
    expire:
      {
        expiration_after: expire_at,
        time_zone: expiration_time_zone,
        type: expiration_type,
        message: expiration_message
      }
  }
rescue StandardError => error
  log_error("Unable to build time bomb expire at policy: #{inspect}", error)
  {}
end

#time_window_policyObject

Determine if we should enforce time of day policy



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/models/time_bomb_policy.rb', line 69

def time_window_policy
  return {} unless time_window_active?

  {
    time_window:
      { start_time: start_time,
        end_time: end_time,
        time_zone: time_window_time_zone,
        days: days,
        message: time_window_message }
  }
rescue StandardError => error
  log_error("Unable to build time bomb time window policy: #{inspect}", error)
  {}
end