Repository navigation
Expand file tree
/
Copy pathrails_example.rb
More file actions
executable file
·94 lines (78 loc) · 2.77 KB
/
Copy pathrails_example.rb
File metadata and controls
executable file
·94 lines (78 loc) · 2.77 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "open_feature/sdk"
require "confidence/openfeature"
# Ruby on Rails integration example for Confidence OpenFeature Provider
#
# This example demonstrates how to use the provider in a Rails-like environment
# Run with: ruby examples/rails_example.rb
puts "=== Confidence OpenFeature Provider - Rails Example ==="
puts
# Simulate Rails environment variable loading
client_secret = ENV["CONFIDENCE_CLIENT_SECRET"] || "CONFIDENCE_CLIENT_SECRET"
# 1. Configuration (like config/initializers/confidence.rb in Rails)
puts "1. Configuring OpenFeature with Confidence provider..."
OpenFeature::SDK.configure do |config|
api_client = Confidence::OpenFeature::APIClient.new(
client_secret: client_secret,
region: Confidence::OpenFeature::Region::EU
)
config.set_provider(Confidence::OpenFeature::Provider.new(api_client: api_client))
end
puts " ✓ Provider configured"
puts
# 2. Simulate a Rails ApplicationController with helper methods
class ApplicationController
attr_accessor :current_user, :session, :request
def initialize(current_user: nil, session: {}, request: {})
@current_user = current_user
@session = session
@request = request
end
def open_feature_client
@open_feature_client ||= OpenFeature::SDK.build_client(domain: "rails-app")
end
def evaluation_context
user_id = current_user&.fetch(:id, nil) || session[:id]
OpenFeature::SDK::EvaluationContext.new(
targeting_key: user_id,
user_id: user_id
)
end
def feature_enabled?(flag_key, default: false)
open_feature_client.fetch_boolean_value(
flag_key: flag_key,
default_value: default,
evaluation_context: evaluation_context
)
end
end
# 3. Simulate different user scenarios
puts "2. Testing feature flags with different users..."
puts
premium_user = {id: "user-123", premium: true}
request = {country_code: "SE"}
session = {id: "session-abc"}
controller = ApplicationController.new(
current_user: premium_user,
session: session,
request: request
)
new_ui_enabled = controller.feature_enabled?("mattias-boolean-flag.enabled", default: false)
puts " New UI enabled: #{new_ui_enabled}"
config = controller.open_feature_client.fetch_object_value(
flag_key: "mattias-boolean-flag",
default_value: {},
evaluation_context: controller.evaluation_context
)
puts " Full config: #{config}"
puts
puts "=== Rails Integration Tips ==="
puts "1. Add configuration in config/initializers/confidence.rb"
puts "2. Use ENV variables for client_secret (never hardcode)"
puts "3. Create helper methods in ApplicationController"
puts "4. Build evaluation context from current_user and request data"
puts "5. The provider is thread-safe for multi-threaded Rails servers"
puts
puts "Done!"