Class: StorageDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/decorators/storage_decorator.rb

Instance Method Summary collapse

Instance Method Details

#get_feature_from_storage(feature_key, context, storage_service) ⇒ Hash, StorageEnum

Retrieves a feature from storage based on the feature key and user.

Parameters:

  • feature_key (String)

    The key of the feature to retrieve.

  • context (ContextModel)

    The user object.

  • storage_service (StorageService)

    The storage service instance.

Returns:

  • (Hash, StorageEnum)

    The retrieved feature or relevant status.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/wingify/decorators/storage_decorator.rb', line 28

def get_feature_from_storage(feature_key, context, storage_service)
  campaign_map = storage_service.get_data_from_storage(feature_key, context)

  case campaign_map
  when StorageEnum::STORAGE_UNDEFINED, StorageEnum::NO_DATA_FOUND,
       StorageEnum::CAMPAIGN_PAUSED, StorageEnum::WHITELISTED_VARIATION
    nil # Return nil when there's no valid data
  when StorageEnum::INCORRECT_DATA, StorageEnum::VARIATION_NOT_FOUND
    campaign_map # Return the relevant error constant
  else
    campaign_map # Return valid stored data
  end
end

#set_data_in_storage(data, storage_service) ⇒ Object

Sets data in storage based on the provided data object.

Parameters:

  • data (Hash)

    The data to be stored, including feature key and user details.

  • storage_service (StorageService)

    The storage service instance.



45
46
47
48
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
# File 'lib/wingify/decorators/storage_decorator.rb', line 45

def set_data_in_storage(data, storage_service)
  feature_key = data[:feature_key]
  context = data[:context]
  rollout_id = data[:rollout_id]
  rollout_key = data[:rollout_key]
  rollout_variation_id = data[:rollout_variation_id]
  experiment_id = data[:experiment_id]
  experiment_key = data[:experiment_key]
  experiment_variation_id = data[:experiment_variation_id]

  if feature_key.nil?
    raise 'Feature key is missing'
  end

  if context.nil? || context.id.nil?
    raise 'Context ID is missing'
  end

  if rollout_key && !experiment_key && !rollout_variation_id
    raise 'Invalid rollout variation'
  end

  if experiment_key && !experiment_variation_id
    raise 'Invalid experiment variation'
  end

  storage_service.set_data_in_storage({
    feature_key: feature_key,
    user_id: context.id,
    rollout_id: rollout_id,
    rollout_key: rollout_key,
    rollout_variation_id: rollout_variation_id,
    experiment_id: experiment_id,
    experiment_key: experiment_key,
    experiment_variation_id: experiment_variation_id
  }, context)
end