class Rcurtain::Curtain
Attributes
redis[R]
Public Class Methods
new()
click to toggle source
# File lib/rcurtain/curtain.rb, line 9 def initialize @redis = Redis.new(:url => Rcurtain.configuration.url) end
Public Instance Methods
get_users(feature)
click to toggle source
# File lib/rcurtain/curtain.rb, line 19 def get_users(feature) get_feature(feature).users rescue Redis::CannotConnectError Rcurtain.configuration.default_response end
opened?(feature, users = [])
click to toggle source
# File lib/rcurtain/curtain.rb, line 13 def opened?(feature, users = []) compare_percentage?(percentage(feature)) || users_enabled?(feature, users) rescue Redis::CannotConnectError return Rcurtain.configuration.default_response end
Private Instance Methods
compare_percentage?(percentage)
click to toggle source
# File lib/rcurtain/curtain.rb, line 47 def compare_percentage? (percentage) rnd = Random.new rnd.rand(1..100) <= percentage.to_f end
get_feature(name)
click to toggle source
# File lib/rcurtain/curtain.rb, line 26 def get_feature (name) percentage = redis.get("feature:#{name}:percentage") || 0 users = redis.smembers("feature:#{name}:users") || [] return Feature.new(name, percentage, users) end
invalid_users?(users)
click to toggle source
# File lib/rcurtain/curtain.rb, line 39 def invalid_users?(users) users.nil? || users.empty? end
percentage(feature_name)
click to toggle source
# File lib/rcurtain/curtain.rb, line 43 def percentage(feature_name) redis.get("feature:#{feature_name}:percentage") || 0 end
users_enabled?(feature_name, users = [])
click to toggle source
# File lib/rcurtain/curtain.rb, line 34 def users_enabled?(feature_name, users = []) return false if invalid_users?(users) users.all? { |user| redis.sismember("feature:#{feature_name}:users", user) } end