Class: Featurevisor::DatafileReader
- Inherits:
-
Object
- Object
- Featurevisor::DatafileReader
- Defined in:
- lib/featurevisor/datafile_reader.rb
Overview
DatafileReader class for reading and processing Featurevisor datafiles
Instance Attribute Summary collapse
-
#features ⇒ Object
readonly
Returns the value of attribute features.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#regex_cache ⇒ Object
readonly
Returns the value of attribute regex_cache.
-
#revision ⇒ Object
readonly
Returns the value of attribute revision.
-
#schema_version ⇒ Object
readonly
Returns the value of attribute schema_version.
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Instance Method Summary collapse
-
#all_conditions_are_matched(conditions, context) ⇒ Boolean
Check if all conditions are matched against context.
-
#all_segments_are_matched(group_segments, context) ⇒ Boolean
Check if all segments are matched against context.
-
#get_feature(feature_key) ⇒ Hash?
Get a feature by key.
-
#get_feature_keys ⇒ Array<Symbol>
Get all feature keys.
-
#get_matched_allocation(traffic, bucket_value) ⇒ Hash?
Get matched allocation based on bucket value.
-
#get_matched_force(feature_key, context) ⇒ Hash
Get matched force based on context.
-
#get_matched_traffic(traffic, context) ⇒ Hash?
Get matched traffic based on context.
-
#get_regex(regex_string, regex_flags = "") ⇒ Regexp
Get a regex object with caching.
-
#get_revision ⇒ String
Get the revision of the datafile.
-
#get_schema_version ⇒ String
Get the schema version of the datafile.
-
#get_segment(segment_key) ⇒ Hash?
Get a segment by key.
-
#get_variable_keys(feature_key) ⇒ Array<String>
Get variable keys for a feature.
-
#has_variations?(feature_key) ⇒ Boolean
Check if a feature has variations.
-
#initialize(options) ⇒ DatafileReader
constructor
Initialize a new DatafileReader.
-
#parse_conditions_if_stringified(conditions) ⇒ Array, ...
Parse conditions if they are stringified.
-
#parse_segments_if_stringified(segments) ⇒ Array, ...
Parse segments if they are stringified.
-
#segment_is_matched(segment, context) ⇒ Boolean
Check if a segment is matched against context.
Constructor Details
#initialize(options) ⇒ DatafileReader
Initialize a new DatafileReader
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 |
# File 'lib/featurevisor/datafile_reader.rb', line 14 def initialize() datafile = [:datafile] @logger = [:logger] @schema_version = datafile[:schemaVersion] @revision = datafile[:revision] @segments = (datafile[:segments] || {}).transform_keys(&:to_sym) @features = (datafile[:features] || {}).transform_keys(&:to_sym) # Transform nested structures to use symbol keys @features.each do |_key, feature| if feature[:variablesSchema] feature[:variablesSchema] = feature[:variablesSchema].transform_keys(&:to_sym) end if feature[:variations] feature[:variations].each do |variation| if variation[:variables] variation[:variables] = variation[:variables].transform_keys(&:to_sym) end if variation[:variableOverrides] variation[:variableOverrides] = variation[:variableOverrides].transform_keys(&:to_sym) end end end if feature[:force] feature[:force].each do |force_rule| if force_rule[:variables] force_rule[:variables] = force_rule[:variables].transform_keys(&:to_sym) end end end if feature[:traffic] feature[:traffic].each do |traffic_rule| if traffic_rule[:variables] traffic_rule[:variables] = traffic_rule[:variables].transform_keys(&:to_sym) end end end end @regex_cache = {} end |
Instance Attribute Details
#features ⇒ Object (readonly)
Returns the value of attribute features.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def features @features end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def logger @logger end |
#regex_cache ⇒ Object (readonly)
Returns the value of attribute regex_cache.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def regex_cache @regex_cache end |
#revision ⇒ Object (readonly)
Returns the value of attribute revision.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def revision @revision end |
#schema_version ⇒ Object (readonly)
Returns the value of attribute schema_version.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def schema_version @schema_version end |
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
8 9 10 |
# File 'lib/featurevisor/datafile_reader.rb', line 8 def segments @segments end |
Instance Method Details
#all_conditions_are_matched(conditions, context) ⇒ Boolean
Check if all conditions are matched against context
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/featurevisor/datafile_reader.rb', line 134 def all_conditions_are_matched(conditions, context) if conditions.is_a?(String) return true if conditions == "*" return false end get_regex_proc = ->(regex_string, regex_flags) { get_regex(regex_string, regex_flags) } if conditions.is_a?(Hash) && (conditions[:attribute] || conditions["attribute"]) begin result = Conditions.condition_is_matched(conditions, context, get_regex_proc) return result rescue => e @logger.warn("Error in condition evaluation: #{e.}", { error: e.class.name, details: { condition: conditions, context: context } }) return false end end if conditions.is_a?(Hash) && conditions[:and] && conditions[:and].is_a?(Array) return conditions[:and].all? { |c| all_conditions_are_matched(c, context) } end if conditions.is_a?(Hash) && conditions["and"] && conditions["and"].is_a?(Array) return conditions["and"].all? { |c| all_conditions_are_matched(c, context) } end if conditions.is_a?(Hash) && conditions[:or] && conditions[:or].is_a?(Array) return conditions[:or].any? { |c| all_conditions_are_matched(c, context) } end if conditions.is_a?(Hash) && conditions["or"] && conditions["or"].is_a?(Array) return conditions["or"].any? { |c| all_conditions_are_matched(c, context) } end if conditions.is_a?(Hash) && conditions[:not] && conditions[:not].is_a?(Array) return conditions[:not].all? do all_conditions_are_matched({ and: conditions[:not] }, context) == false end end if conditions.is_a?(Hash) && conditions["not"] && conditions["not"].is_a?(Array) return conditions["not"].all? do all_conditions_are_matched({ "and" => conditions["not"] }, context) == false end end if conditions.is_a?(Array) return conditions.all? { |c| all_conditions_are_matched(c, context) } end false end |
#all_segments_are_matched(group_segments, context) ⇒ Boolean
Check if all segments are matched against context
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/featurevisor/datafile_reader.rb', line 207 def all_segments_are_matched(group_segments, context) if group_segments == "*" return true end if group_segments.is_a?(String) segment = get_segment(group_segments) if segment return segment_is_matched(segment, context) end return false end if group_segments.is_a?(Hash) if group_segments[:and] && group_segments[:and].is_a?(Array) return group_segments[:and].all? { |group_segment| all_segments_are_matched(group_segment, context) } end if group_segments["and"] && group_segments["and"].is_a?(Array) return group_segments["and"].all? { |group_segment| all_segments_are_matched(group_segment, context) } end if group_segments[:or] && group_segments[:or].is_a?(Array) return group_segments[:or].any? { |group_segment| all_segments_are_matched(group_segment, context) } end if group_segments["or"] && group_segments["or"].is_a?(Array) return group_segments["or"].any? { |group_segment| all_segments_are_matched(group_segment, context) } end if group_segments[:not] && group_segments[:not].is_a?(Array) return group_segments[:not].all? do all_segments_are_matched({ and: group_segments[:not] }, context) == false end end if group_segments["not"] && group_segments["not"].is_a?(Array) return group_segments["not"].all? do all_segments_are_matched({ "and" => group_segments["not"] }, context) == false end end end if group_segments.is_a?(Array) return group_segments.all? { |group_segment| all_segments_are_matched(group_segment, context) } end false end |
#get_feature(feature_key) ⇒ Hash?
Get a feature by key
89 90 91 |
# File 'lib/featurevisor/datafile_reader.rb', line 89 def get_feature(feature_key) @features[feature_key.to_sym] || @features[feature_key] end |
#get_feature_keys ⇒ Array<Symbol>
Get all feature keys
82 83 84 |
# File 'lib/featurevisor/datafile_reader.rb', line 82 def get_feature_keys @features.keys end |
#get_matched_allocation(traffic, bucket_value) ⇒ Hash?
Get matched allocation based on bucket value
278 279 280 281 282 283 284 285 |
# File 'lib/featurevisor/datafile_reader.rb', line 278 def get_matched_allocation(traffic, bucket_value) return nil unless traffic[:allocation] traffic[:allocation].find do |allocation| start, end_val = allocation[:range] start <= bucket_value && end_val >= bucket_value end end |
#get_matched_force(feature_key, context) ⇒ Hash
Get matched force based on context
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/featurevisor/datafile_reader.rb', line 291 def get_matched_force(feature_key, context) result = { force: nil, forceIndex: nil } feature = feature_key.is_a?(String) ? get_feature(feature_key) : feature_key return result unless feature && feature[:force] feature[:force].each_with_index do |current_force, i| if current_force[:conditions] && all_conditions_are_matched( parse_conditions_if_stringified(current_force[:conditions]), context ) result[:force] = current_force result[:forceIndex] = i break end if current_force[:segments] && all_segments_are_matched( parse_segments_if_stringified(current_force[:segments]), context ) result[:force] = current_force result[:forceIndex] = i break end end result end |
#get_matched_traffic(traffic, context) ⇒ Hash?
Get matched traffic based on context
265 266 267 268 269 270 271 272 |
# File 'lib/featurevisor/datafile_reader.rb', line 265 def get_matched_traffic(traffic, context) traffic.find do |t| segments = parse_segments_if_stringified(t[:segments]) matched = all_segments_are_matched(segments, context) next false unless matched true end end |
#get_regex(regex_string, regex_flags = "") ⇒ Regexp
Get a regex object with caching
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/featurevisor/datafile_reader.rb', line 119 def get_regex(regex_string, regex_flags = "") flags = regex_flags || "" cache_key = "#{regex_string}-#{flags}" return @regex_cache[cache_key] if @regex_cache[cache_key] regex = Regexp.new(regex_string, flags) @regex_cache[cache_key] = regex @regex_cache[cache_key] end |
#get_revision ⇒ String
Get the revision of the datafile
58 59 60 |
# File 'lib/featurevisor/datafile_reader.rb', line 58 def get_revision @revision end |
#get_schema_version ⇒ String
Get the schema version of the datafile
64 65 66 |
# File 'lib/featurevisor/datafile_reader.rb', line 64 def get_schema_version @schema_version end |
#get_segment(segment_key) ⇒ Hash?
Get a segment by key
71 72 73 74 75 76 77 78 |
# File 'lib/featurevisor/datafile_reader.rb', line 71 def get_segment(segment_key) segment = @segments[segment_key.to_sym] || @segments[segment_key] return nil unless segment segment[:conditions] = parse_conditions_if_stringified(segment[:conditions]) segment end |
#get_variable_keys(feature_key) ⇒ Array<String>
Get variable keys for a feature
96 97 98 99 100 101 102 |
# File 'lib/featurevisor/datafile_reader.rb', line 96 def get_variable_keys(feature_key) feature = get_feature(feature_key) return [] unless feature (feature[:variablesSchema] || {}).keys end |
#has_variations?(feature_key) ⇒ Boolean
Check if a feature has variations
107 108 109 110 111 112 113 |
# File 'lib/featurevisor/datafile_reader.rb', line 107 def has_variations?(feature_key) feature = get_feature(feature_key) return false unless feature feature[:variations].is_a?(Array) && feature[:variations].size > 0 end |
#parse_conditions_if_stringified(conditions) ⇒ Array, ...
Parse conditions if they are stringified
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/featurevisor/datafile_reader.rb', line 325 def parse_conditions_if_stringified(conditions) return conditions unless conditions.is_a?(String) return conditions if conditions == "*" begin JSON.parse(conditions) rescue => e @logger.error("Error parsing conditions", { error: e, details: { conditions: conditions } }) conditions end end |
#parse_segments_if_stringified(segments) ⇒ Array, ...
Parse segments if they are stringified
346 347 348 349 350 351 352 |
# File 'lib/featurevisor/datafile_reader.rb', line 346 def parse_segments_if_stringified(segments) if segments.is_a?(String) && (segments.start_with?("{") || segments.start_with?("[")) return JSON.parse(segments) end segments end |
#segment_is_matched(segment, context) ⇒ Boolean
Check if a segment is matched against context
199 200 201 |
# File 'lib/featurevisor/datafile_reader.rb', line 199 def segment_is_matched(segment, context) all_conditions_are_matched(segment[:conditions], context) end |