-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathenvironment.rb
More file actions
60 lines (53 loc) · 1.96 KB
/
environment.rb
File metadata and controls
60 lines (53 loc) · 1.96 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
# frozen_string_literal: true
module Flagsmith
module Engine
module Mappers
# Handles environment and feature mapping
module Environment
def self.build_environment_context(environment)
{
key: environment.api_key,
name: environment.name
}
end
def self.build_features_context(feature_states)
features = {}
feature_states.each do |feature_state|
features[feature_state.feature.name] = build_feature_hash(feature_state)
end
features
end
def self.build_feature_hash(feature_state) # rubocop:disable Metrics/MethodLength
feature_hash = {
key: feature_state.django_id&.to_s || feature_state.uuid,
feature_key: feature_state.feature.id.to_s,
name: feature_state.feature.name,
enabled: feature_state.enabled,
value: feature_state.get_value,
metadata: { flagsmith_id: feature_state.feature.id }
}
add_variants_to_feature(feature_hash, feature_state)
add_priority_to_feature(feature_hash, feature_state)
feature_hash
end
def self.add_variants_to_feature(feature_hash, feature_state)
return unless feature_state.multivariate_feature_state_values&.any?
feature_hash[:variants] = feature_state.multivariate_feature_state_values.map do |mv|
{
value: mv.multivariate_feature_option.value,
weight: mv.percentage_allocation,
priority: mv.id || uuid_to_big_int(mv.mv_fs_value_uuid)
}
end
end
def self.uuid_to_big_int(uuid)
uuid.gsub('-', '').to_i(16)
end
def self.add_priority_to_feature(feature_hash, feature_state)
priority = feature_state.feature_segment&.priority
feature_hash[:priority] = priority unless priority.nil?
end
end
end
end
end