Module: FtGem::Controllers::Helpers

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

Constant Summary collapse

'mnft'.freeze
'mnbeta'.freeze

Instance Method Summary collapse

Instance Method Details

#api_feature_toggle_status(name:) ⇒ Object



58
59
60
# File 'lib/ft_gem/controllers/helpers.rb', line 58

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

#calc_client_percentage(decoded_cookie:, toggle_name:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/ft_gem/controllers/helpers.rb', line 47

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:, user_id: nil, url: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ft_gem/controllers/helpers.rb', line 9

def feature_toggle_status(name:, user_id: nil, url: nil)
  toggles = FtShared.new.all_toggles
  index = toggles.index { |toggle| toggle['attributes']['name'] == name }
  return false if index.nil?

  whitelisted = FtShared.new.whitelisted?(url: url, whitelist_urls: toggles[index.to_i]['attributes']['whitelist-urls'])
  return false unless whitelisted

  return true if FtGem::Services::BetaService.public_beta?(cookie: cookies[PUBLICBETA_COOKIE_NAME], toggles: toggles, index: index)
  return true if FtGem::Services::BetaService.private_beta?(user_id: user_id, toggles: toggles, index: index)

  update_feature_toggles(toggles: toggles)
  decoded_cookie = Base64.decode64(cookies[TOGGLE_COOKIE_NAME])

  client_percentage = calc_client_percentage(decoded_cookie: decoded_cookie, toggle_name: name)
  toggle_percentage = toggles[index.to_i]['attributes']['percentage']

  return client_percentage.to_i <= toggle_percentage.to_i && whitelisted
rescue StandardError => e
  Rails.logger.error "FtGem::feature_toggle_status error #{e.backtrace}"
  false
end

#update_feature_toggles(toggles:) ⇒ Object



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

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.delete TOGGLE_COOKIE_NAME, domain: ENV['COOKIE_DOMAIN']
  cookies[TOGGLE_COOKIE_NAME] = { value: Base64.strict_encode64(new_cookie.join(',')).to_s, secure: true }
rescue StandardError => e
  Rails.logger.error "FTApiGem::update_feature_toggles error #{e.backtrace}"
  nil
end