module ActiveSwitch
Constants
- AlreadyRegistered
- NoRedisClient
- REGISTRATIONS
- STORAGE_KEY
- UnknownName
- VERSION
Attributes
redis[W]
Public Class Methods
redis()
click to toggle source
# File lib/active_switch.rb, line 17 def redis @redis or raise NoRedisClient, "No redis client configured" end
Public Instance Methods
active()
click to toggle source
# File lib/active_switch.rb, line 63 def active all.select { |_, s| s.active? } end
all()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/active_switch.rb, line 67 def inactive all.select { |_, s| s.inactive? } end
mark_reported(name)
click to toggle source
Considered private API
# File lib/active_switch.rb, line 77 def mark_reported(name) redis.hset(STORAGE_KEY, name, Time.now.to_i) end
register(*args)
click to toggle source
# 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) { || ... }
click to toggle source
# 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()
click to toggle source
# File lib/active_switch.rb, line 71 def report_on_inactive inactive.keys.map { |name| report(name) } end
status(name)
click to toggle source
# 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