Module: ActiveSwitch
- Extended by:
- ActiveSwitch
- Included in:
- ActiveSwitch
- Defined in:
- lib/active_switch.rb,
lib/active_switch/status.rb,
lib/active_switch/version.rb
Defined Under Namespace
Classes: Status
Constant Summary collapse
- NoRedisClient =
Class.new(StandardError)
- AlreadyRegistered =
Class.new(ArgumentError)
- UnknownName =
Class.new(ArgumentError)
- STORAGE_KEY =
"active_switch_last_reported_ats".freeze
- REGISTRATIONS =
{}
- VERSION =
"0.0.1"
Class Attribute Summary collapse
Instance Method Summary collapse
- #active ⇒ Object
- #all ⇒ Object
- #cast_name(name) ⇒ Object
- #inactive ⇒ Object
-
#mark_reported(name) ⇒ Object
Considered private API.
- #register(*args) ⇒ Object
- #report(name) ⇒ Object
- #report_on_inactive ⇒ Object
- #status(name) ⇒ Object
Class Attribute Details
.redis ⇒ Object
17 18 19 |
# File 'lib/active_switch.rb', line 17 def redis @redis or raise NoRedisClient, "No redis client configured" end |
Instance Method Details
#active ⇒ Object
63 64 65 |
# File 'lib/active_switch.rb', line 63 def active all.select { |_, s| s.active? } end |
#all ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/active_switch.rb', line 54 def all data = redis.hgetall(STORAGE_KEY) REGISTRATIONS.each_with_object({}) do |(name, threshold_seconds), obj| obj[name] = Status.new(name: name, last_reported_at: data[name], threshold_seconds: threshold_seconds) end end |
#cast_name(name) ⇒ Object
81 82 83 84 85 |
# File 'lib/active_switch.rb', line 81 def cast_name(name) name.to_s.tap do |name| raise UnknownName, "#{name} not found" unless REGISTRATIONS[name] end end |
#inactive ⇒ Object
67 68 69 |
# File 'lib/active_switch.rb', line 67 def inactive all.select { |_, s| s.inactive? } end |
#mark_reported(name) ⇒ Object
Considered private API
77 78 79 |
# File 'lib/active_switch.rb', line 77 def mark_reported(name) redis.hset(STORAGE_KEY, name, Time.now.to_i) end |
#register(*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_switch.rb', line 22 def register(*args) if args[0].is_a?(Hash) args[0].each { |name, threshold_seconds| register(name, threshold_seconds) } else name, threshold_seconds = args[0].to_s, args[1] if REGISTRATIONS[name] raise AlreadyRegistered, "#{name} already registered" else REGISTRATIONS[name] = threshold_seconds end end end |
#report(name) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_switch.rb', line 36 def report(name) name = cast_name(name) if block_given? yield.tap { mark_reported(name) } else mark_reported(name) true end end |
#report_on_inactive ⇒ Object
71 72 73 |
# File 'lib/active_switch.rb', line 71 def report_on_inactive inactive.keys.map { |name| report(name) } end |
#status(name) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/active_switch.rb', line 47 def status(name) name = cast_name(name) ts = redis.hget(STORAGE_KEY, name) Status.new(name: name, last_reported_at: ts, threshold_seconds: REGISTRATIONS[name]) end |