Class: EppoClient::ExperimentConfigurationRequestor

Inherits:
Object
  • Object
show all
Defined in:
lib/configuration_requestor.rb

Overview

A class for getting exp configs from the local cache or API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_client, config_store) ⇒ ExperimentConfigurationRequestor

Returns a new instance of ExperimentConfigurationRequestor.



48
49
50
51
# File 'lib/configuration_requestor.rb', line 48

def initialize(http_client, config_store)
  @http_client = http_client
  @config_store = config_store
end

Instance Attribute Details

#config_storeObject (readonly)

Returns the value of attribute config_store.



46
47
48
# File 'lib/configuration_requestor.rb', line 46

def config_store
  @config_store
end

Instance Method Details

#fetch_and_store_configurationsObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



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
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/configuration_requestor.rb', line 59

def fetch_and_store_configurations
  configs = {}
  begin
    exp_configs = @http_client.get(EppoClient::RAC_ENDPOINT).fetch('flags', {})
    exp_configs.each do |exp_key, exp_config|
      exp_config['allocations'].each do |k, v|
        exp_config['allocations'][k] = EppoClient::AllocationDto.new(
          v['percentExposure'],
          v['variations'].map do |var|
            EppoClient::VariationDto.new(
              var['name'],
              var['value'],
              EppoClient::ShardRange.new(var['shardRange']['start'], var['shardRange']['end'])
            )
          end
        )
      end
      exp_config['rules'] = exp_config['rules'].map do |rule|
        EppoClient::Rule.new(
          conditions: rule['conditions'].map do |condition|
            EppoClient::Condition.new(
              value: condition['value'],
              operator: condition['operator'],
              attribute: condition['attribute']
            )
          end,
          allocation_key: rule['allocationKey']
        )
      end
      configs[exp_key] = EppoClient::ExperimentConfigurationDto.new(exp_config)
    end
    @config_store.assign_configurations(configs)
  rescue EppoClient::HttpRequestError => e
    EppoClient.logger('err').error("Error retrieving assignment configurations: #{e}")
  end
  configs
end

#get_configuration(experiment_key) ⇒ Object



53
54
55
56
# File 'lib/configuration_requestor.rb', line 53

def get_configuration(experiment_key)
  @http_client.is_unauthorized && raise(EppoClient::UnauthorizedError, 'please check your API key')
  @config_store.retrieve_configuration(experiment_key)
end