File: variant_override.rb

package info (click to toggle)
ruby-unleash 3.2.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 252 kB
  • sloc: ruby: 1,098; makefile: 10; sh: 4
file content (44 lines) | stat: -rw-r--r-- 1,255 bytes parent folder | download | duplicates (2)
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
module Unleash
  class VariantOverride
    attr_accessor :context_name, :values

    def initialize(context_name, values = [])
      self.context_name = context_name
      self.values = values || []

      validate
    end

    def to_s
      "<VariantOverride: context_name=#{self.context_name},values=#{self.values}>"
    end

    def matches_context?(context)
      raise ArgumentError, 'context must be of class Unleash::Context' unless context.class.name == 'Unleash::Context'

      context_value =
        case self.context_name
        when 'userId'
          context.user_id
        when 'sessionId'
          context.session_id
        when 'remoteAddress'
          context.remote_address
        else
          context.properties.fetch(self.context_name, nil)
        end

      Unleash.logger.debug "VariantOverride: context_name: #{context_name} context_value: #{context_value}"

      self.values.include? context_value.to_s
    end

    private

    def validate
      raise ArgumentError, 'context_name must be a String' unless self.context_name.is_a?(String)
      raise ArgumentError, 'values must be an Array of strings' unless self.values.is_a?(Array) \
          && self.values.reject{ |v| v.is_a?(String) }.empty?
    end
  end
end