Class: SplitIoClient::Validators

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/validators.rb

Constant Summary collapse

Flagset_regex =
/^[a-z0-9][_a-z0-9]{0,49}$/
Fallback_treatment_regex =
/^[0-9]+[.a-zA-Z0-9_-]*$|^[a-zA-Z]+[a-zA-Z0-9_-]*$/
Fallback_treatment_size =
100

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Validators

Returns a new instance of Validators.



10
11
12
# File 'lib/splitclient-rb/validators.rb', line 10

def initialize(config)
  @config = config
end

Instance Method Details

#valid_flag_sets(method, flag_sets) ⇒ Object



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
# File 'lib/splitclient-rb/validators.rb', line 49

def valid_flag_sets(method, flag_sets)
  if flag_sets.nil? || !flag_sets.is_a?(Array)
    @config.logger.error("#{method}: FlagSets must be a non-empty list.")
    return []
  end
  if flag_sets.empty?
    @config.logger.error("#{method}: FlagSets must be a non-empty list.")
    return []
  end
  without_nil = Array.new
  flag_sets.each { |flag_set|
    if !flag_set.nil?
      without_nil.push(flag_set) 
      next
    end
    
    log_nil("flag set", method)
  }
  if without_nil.length() == 0
    log_invalid_flag_set_type(method)
    return []
  end
  valid_flag_sets = Set[]
  without_nil.compact.uniq.select do |flag_set|
    if flag_set.nil? || !flag_set.is_a?(String)
      log_invalid_flag_set_type(method)
    elsif flag_set.is_a?(String) && flag_set.empty?
      log_invalid_flag_set_type(method)
    elsif !flag_set.empty? && string_match?(flag_set.strip.downcase, method, Flagset_regex, :log_invalid_match)
      valid_flag_sets.add(flag_set.strip.downcase)
    else
      log_invalid_flag_set_type(method)
    end
  end
  !valid_flag_sets.empty? ? valid_flag_sets.to_a.sort :  []
end

#valid_get_treatment_parameters(method, key, split_name, matching_key, bucketing_key, attributes) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/splitclient-rb/validators.rb', line 14

def valid_get_treatment_parameters(method, key, split_name, matching_key, bucketing_key, attributes)
  valid_key?(method, key) &&
    valid_split_name?(method, split_name) &&
    valid_matching_key?(method, matching_key) &&
    valid_bucketing_key?(method, key, bucketing_key) &&
    valid_attributes?(method, attributes)
end

#valid_get_treatments_parameters(method, key, split_names, matching_key, bucketing_key, attributes) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/splitclient-rb/validators.rb', line 22

def valid_get_treatments_parameters(method, key, split_names, matching_key, bucketing_key, attributes)
  valid_key?(method, key) &&
    valid_split_names?(method, split_names)
    valid_matching_key?(method, matching_key) &&
    valid_bucketing_key?(method, key, bucketing_key) &&
    valid_attributes?(method, attributes)
end

#valid_matcher_arguments(args) ⇒ Object



42
43
44
45
46
47
# File 'lib/splitclient-rb/validators.rb', line 42

def valid_matcher_arguments(args)
  return false if !args.key?(:attributes) && !args.key?(:value)
  return false if args.key?(:value) && args[:value].nil?
  return false if args.key?(:attributes) && args[:attributes].nil?
  true
end

#valid_split_name?(method, split_name) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/splitclient-rb/validators.rb', line 107

def valid_split_name?(method, split_name)
  if split_name.nil?
    log_nil(:split_name, method)
    return false
  end

  unless string?(split_name)
    log_invalid_type(:split_name, method)
    return false
  end

  if empty_string?(split_name)
    log_empty_string(:split_name, method)
    return false
  end

  true
end

#valid_split_parameters(split_name) ⇒ Object



38
39
40
# File 'lib/splitclient-rb/validators.rb', line 38

def valid_split_parameters(split_name)
  valid_split_name?(:split, split_name)
end

#valid_track_parameters(key, traffic_type_name, event_type, value, properties) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/splitclient-rb/validators.rb', line 30

def valid_track_parameters(key, traffic_type_name, event_type, value, properties)
  valid_track_key?(key) &&
    valid_traffic_type_name?(traffic_type_name) &&
    valid_event_type?(event_type) &&
    valid_value?(value) &&
    valid_properties?(properties)
end

#validate_fallback_treatment(method, fallback_treatment) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/splitclient-rb/validators.rb', line 86

def validate_fallback_treatment(method, fallback_treatment)
  if !fallback_treatment.is_a? Engine::Models::FallbackTreatment
      @config.logger.warn("#{method}: Fallback treatment instance should be FallbackTreatment, input is discarded")
      return false
  end

  if !fallback_treatment.treatment.is_a? String
      @config.logger.warn("#{method}: Fallback treatment value should be str type, input is discarded")
      return false
  end    

  return false unless string_match?(fallback_treatment.treatment, method, Fallback_treatment_regex, :log_invalid_fallback_treatment)

  if fallback_treatment.treatment.size > Fallback_treatment_size
      @config.logger.warn("#{method}: Fallback treatment size should not exceed #{Fallback_treatment_size} characters")
      return false
  end
  
  true
end