forked from launchdarkly/hello-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
72 lines (57 loc) · 1.93 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
require 'ldclient-rb'
sdk_key = ENV['LAUNCHDARKLY_SDK_KEY']
# Set feature_flag_key to the feature flag key you want to evaluate
feature_flag_key = ENV['LAUNCHDARKLY_FLAG_KEY']
ci = ENV['CI']
if sdk_key == ''
puts "*** Please set the LAUNCHDARKLY_SDK_KEY environment variable\n"
exit 1
elsif feature_flag_key == ''
puts "*** Please set the LAUNCHDARKLY_FLAG_KEY environment variable\n"
exit 1
end
def show_flag_message(flag_key, flag_value)
puts "*** The '#{flag_key}' feature flag evaluates to #{flag_value}.\n"
if flag_value
puts
puts " ██ "
puts " ██ "
puts " ████████ "
puts " ███████ "
puts "██ LAUNCHDARKLY █"
puts " ███████ "
puts " ████████ "
puts " ██ "
puts " ██ "
puts
end
end
class FlagChangeListener
def update(changed)
show_flag_message(changed.key, changed.new_value)
end
end
client = LaunchDarkly::LDClient.new(sdk_key)
if client.initialized?
puts "*** SDK successfully initialized!\n"
else
puts "*** SDK failed to initialize\n"
exit 1
end
# Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
# soon after you run the demo.
context = LaunchDarkly::LDContext.create({
key: 'example-user-key',
kind: 'user',
name: 'Sandy'
})
flag_value = client.variation(feature_flag_key, context, false)
show_flag_message(feature_flag_key, flag_value)
exit 0 if ci
client.flag_tracker.add_flag_value_change_listener(feature_flag_key, context, FlagChangeListener.new)
# Run the Hello App continuously to react to flag change in LaunchDarkly
thr = Thread.new {
puts "*** Waiting for changes."
sleep
}
thr.join