Module: FtGem::Controllers::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/ft_gem/controllers/helpers.rb

Constant Summary collapse

'mnft'.freeze

Instance Method Summary collapse

Instance Method Details

#api_feature_toggle_status(name:) ⇒ Object



47
48
49
# File 'lib/ft_gem/controllers/helpers.rb', line 47

def api_feature_toggle_status(name:)
  FtGem::View::Helpers.status(name: name)
end

#calc_client_percentage(decoded_cookie:, toggle_name:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/ft_gem/controllers/helpers.rb', line 36

def calc_client_percentage(decoded_cookie:, toggle_name:)
  client_percentage = rand(0..100)
  if decoded_cookie.include?("#{toggle_name}:")
    old_toggles = decoded_cookie.split(',')
    index = old_toggles.index { |old_toggle| old_toggle.include?("#{toggle_name}:") }
    client_percentage = old_toggles[index].split(':')[1]
  end
  client_percentage = rand(0..100) if client_percentage.to_i.zero?
  client_percentage
end

#feature_toggle_status(name:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ft_gem/controllers/helpers.rb', line 8

def feature_toggle_status(name:)
  toggles = FtShared.all_toggles
  update_feature_toggles(toggles: toggles)
  decoded_cookie = Base64.decode64(cookies[TOGGLE_COOKIE_NAME])
  index = toggles.index { |toggle| toggle['attributes']['name'] == name }
  return false if index.nil?
  client_percentage = calc_client_percentage(decoded_cookie: decoded_cookie, toggle_name: name)
  toggle_percentage = toggles[index.to_i]['attributes']['percentage']
  client_percentage.to_i <= toggle_percentage.to_i
rescue StandardError => e
  Rails.logger.error "FtGem::feature_toggle_status error #{e.backtrace}"
  false
end

#update_feature_toggles(toggles:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ft_gem/controllers/helpers.rb', line 22

def update_feature_toggles(toggles:)
  cookies[TOGGLE_COOKIE_NAME] = '' if cookies[TOGGLE_COOKIE_NAME].nil?
  decoded_cookie = Base64.decode64(cookies[TOGGLE_COOKIE_NAME])
  new_cookie = []
  toggles.each do |toggle|
    client_percentage = calc_client_percentage(decoded_cookie: decoded_cookie, toggle_name: toggle['attributes']['name'])
    new_cookie.push("#{toggle['attributes']['name']}:#{client_percentage}")
  end
  cookies[TOGGLE_COOKIE_NAME] = { value: Base64.encode64(new_cookie.join(',')).to_s, domain: ENV['COOKIE_DOMAIN'] }
rescue StandardError => e
  Rails.logger.error "FTApiGem::update_feature_toggles error #{e.backtrace}"
  nil
end