Class: Featureswitches

Inherits:
Object
  • Object
show all
Defined in:
lib/featureswitches.rb,
lib/featureswitches/version.rb

Constant Summary collapse

VERSION =
"0.8.3"

Instance Method Summary collapse

Constructor Details

#initialize(customer_key, environment_key, options = {}) ⇒ Featureswitches

Returns a new instance of Featureswitches.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/featureswitches.rb', line 7

def initialize(customer_key, environment_key, options={})
    @customer_key = customer_key
    @environment_key = environment_key
    @cache_timeout = options[:cache_timeout] ||= 300
    @check_interval = options[:check_interval] ||= 10
    @last_update = 0
    @last_dirty_check = 0
    @api = options[:api] ||= 'https://api.featureswitches.com/v1/'

    @cache = Cache.new

    if @cache_timeout > 0
        @dirty_check_thread = do_dirty_check
    end
end

Instance Method Details

#add_user(user_identifier, customer_identifier = nil, name = nil, email = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/featureswitches.rb', line 71

def add_user(user_identifier, customer_identifier=nil, name=nil, email=nil)
    endpoint = 'user/add'
    params = {
        'user_identifier' => user_identifier,
        'customer_identifier' => customer_identifier,
        'name' => name,
        'email' => email
    }
    response = api_request(endpoint, params, :post)

    if response[:success]
        return true
    end
    return false
end

#authenticateObject



23
24
25
26
27
28
# File 'lib/featureswitches.rb', line 23

def authenticate
    endpoint = 'authenticate'
    response = api_request(endpoint)

    return response
end

#dirty_checkObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/featureswitches.rb', line 87

def dirty_check
    endpoint = 'dirty-check'
    response = api_request(endpoint)
    
    if response[:success]
        @last_dirty_check = Time.now.to_i

        if response[:data]['last_update'] > @last_update
            @last_update = response[:data]['last_update']
            sync()
        end
    end
end

#is_enabled(feature_key, user_identifier = nil, default = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/featureswitches.rb', line 45

def is_enabled(feature_key, user_identifier=nil, default=false)
    feature = @cache[feature_key]
    if not feature or cache_is_stale(feature)
        feature = get_feature(feature_key)
    end

    if feature
        result = enabled_for_user(feature, user_identifier)

        if not result and feature['enabled'] == true and feature['rollout_progress'] < feature['rollout_target']
            enabled = get_feature_enabled(feature_key, user_identifier)

            if enabled == true and @cache_timeout > 0
                feature['include_users'].push(user_identifier)
                @cache[feature['feature_key']] = feature
            end

            return enabled
        end

        return result
    end

    return default
end

#syncObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/featureswitches.rb', line 30

def sync
    endpoint = 'features'

    if @cache_timeout > 0
        response = api_request(endpoint)

        features = response[:data]['features']

        features.each do |feature|
            feature['last_sync'] = Time.now.to_i
            @cache[feature['feature_key']] = feature
        end
    end
end