-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsegments.rb
More file actions
66 lines (59 loc) · 2.06 KB
/
segments.rb
File metadata and controls
66 lines (59 loc) · 2.06 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
# frozen_string_literal: true
module Flagsmith
module Engine
module Mappers
# Handles segment and rule mapping
module Segments
def self.build_segments_context(project_segments)
segments = {}
project_segments.each do |segment|
segments[segment.id.to_s] = build_segment_hash(segment)
end
segments
end
def self.build_segment_hash(segment)
{
key: segment.id.to_s,
name: segment.name,
rules: segment.rules.map { |rule| map_rule(rule) },
overrides: build_overrides(segment.feature_states),
metadata: {
source: 'API',
flagsmith_id: segment.id
}
}
end
def self.build_overrides(feature_states) # rubocop:disable Metrics/MethodLength
feature_states.map do |feature_state|
override_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_priority_to_override(override_hash, feature_state)
override_hash
end
end
def self.add_priority_to_override(override_hash, feature_state)
return unless feature_state.feature_segment&.priority
override_hash[:priority] = feature_state.feature_segment.priority
end
def self.map_rule(rule)
{
type: rule.type,
conditions: map_conditions(rule.conditions),
rules: (rule.rules || []).map { |nested_rule| map_rule(nested_rule) }
}
end
def self.map_conditions(conditions)
(conditions || []).map do |condition|
{ property: condition.property, operator: condition.operator, value: condition.value }
end
end
end
end
end
end