Class: ActiveSwitch::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/active_switch/status.rb

Constant Summary collapse

ACTIVE =
"ACTIVE".freeze
INACTIVE =
"INACTIVE".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, last_reported_at:, threshold_seconds:) ⇒ Status

Returns a new instance of Status.



8
9
10
11
12
# File 'lib/active_switch/status.rb', line 8

def initialize(name:, last_reported_at:, threshold_seconds:)
  @name              = name.to_s
  @last_reported_at  = cast_timestamp(last_reported_at, cast_null: false)
  @threshold_seconds = threshold_seconds.to_i
end

Instance Attribute Details

#last_reported_atObject (readonly)

Returns the value of attribute last_reported_at.



6
7
8
# File 'lib/active_switch/status.rb', line 6

def last_reported_at
  @last_reported_at
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/active_switch/status.rb', line 6

def name
  @name
end

#threshold_secondsObject (readonly)

Returns the value of attribute threshold_seconds.



6
7
8
# File 'lib/active_switch/status.rb', line 6

def threshold_seconds
  @threshold_seconds
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_switch/status.rb', line 14

def active?
  (cast_timestamp(last_reported_at) + threshold_seconds) > cast_timestamp(now)
end

#inactive?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/active_switch/status.rb', line 18

def inactive?
  !active?
end

#stateObject Also known as: to_s



22
23
24
# File 'lib/active_switch/status.rb', line 22

def state
  active? ? ACTIVE : INACTIVE
end