ft_gem 2.0.6 → 2.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 410802fa5b3402116ecf95b8284291f6b3d5bea88f13b88ad80b29466b788dc7
4
- data.tar.gz: c8493473d7f7eb46b7c994594490df08ad57924e3dd67d6139f05f2f41354a1e
3
+ metadata.gz: 79e10fb49eafa5f0693c8ca7d6198cb66fcbc3f5404d388ae2332fd38d808e51
4
+ data.tar.gz: 8b7b582fefdb9161507249942425d5b6a2e2a16d1d9ecdca30739ed1e0a79329
5
5
  SHA512:
6
- metadata.gz: 4c83817200a8b4b5d8f95bdd6e9eec5a2c1d816d8603ae7871d3f528825eb18d0077b1b807846df658bd924587076ca5ce0b767a68b5926b0d2979e5afe769b7
7
- data.tar.gz: 293231e004c84056ac0f7bfd3522220f5943bc4b6a5c6d27ea1f4a1853146bd147a5ce1e030aa7861a0467f2d1d15dc6c46e5c10b10a8962f7e370657614dcf4
6
+ metadata.gz: db7ac75e1ce0efc91cae3dac95cba2fba47052d0dec4fd83efbbd703ca92e31e8773944a87f480c1b216dc705ddb242bbdd0ac189a771c97f13f0e4bb0a812bd
7
+ data.tar.gz: 6ebd1e40ea57302d80e4a12590163620876fdc3cc86848d76a83fa8c5266eb14b5dbc1c9fc6146546c5784ca907b0d20951c08e27c75a96bd57a6a2777cac2e3
@@ -3,18 +3,46 @@ module FtGem
3
3
  module Helpers
4
4
  extend ActiveSupport::Concern
5
5
  TOGGLE_COOKIE_NAME = 'mnft'.freeze
6
+ require 'base64'
6
7
 
7
8
  def feature_toggle_status(name:)
8
- cookies[TOGGLE_COOKIE_NAME] = rand(0..100) if cookies[TOGGLE_COOKIE_NAME].nil? || cookies[TOGGLE_COOKIE_NAME].to_i.zero?
9
- data = FtShared.fetch(name: name)
10
- toggle_percentage = data['feature_toggle']['toggle_percentage']
11
- client_percentage = cookies[TOGGLE_COOKIE_NAME]
9
+ toggles = FtShared.all_toggles
10
+ update_feature_toggles(toggles: toggles)
11
+ decoded_cookie = Base64.decode64(cookies[TOGGLE_COOKIE_NAME])
12
+ index = toggles.index { |toggle| toggle.include?("#{name}:") }
13
+ client_percentage = calc_client_percentage(decoded_cookie:decoded_cookie, toggle_name:name)
14
+ toggle_percentage = toggles[index]['percentage']
12
15
  client_percentage.to_i <= toggle_percentage.to_i
13
16
  rescue StandardError => e
14
17
  Rails.logger.error "FtGem::feature_toggle_status error #{e.backtrace}"
15
18
  false
16
19
  end
17
20
 
21
+ def update_feature_toggles(toggles:)
22
+ cookies[TOGGLE_COOKIE_NAME] = '' if cookies[TOGGLE_COOKIE_NAME].nil?
23
+ decoded_cookie = Base64.decode64(cookies[TOGGLE_COOKIE_NAME])
24
+ new_cookie = []
25
+ toggles.each do |toggle|
26
+ client_percentage = calc_client_percentage(decoded_cookie: decoded_cookie, toggle_name: toggle['name'])
27
+ new_cookie.push("#{toggle['name']}:#{client_percentage}")
28
+ end
29
+ Base64.encode64(new_cookie.join(','))
30
+ rescue StandardError => e
31
+ Rails.logger.error "FTApiGem::update_feature_toggles error #{e.backtrace}"
32
+ nil
33
+ end
34
+
35
+ def calc_client_percentage(decoded_cookie:, toggle_name:)
36
+ client_percentage = rand(0..100)
37
+ if decoded_cookie.include?("#{toggle_name}:")
38
+ old_toggles = decoded_cookie.split(',')
39
+ index = old_toggles.index { |old_toggle| old_toggle.include?("#{toggle_name}:") }
40
+ client_percentage = old_toggles[index].split(':')[1]
41
+ end
42
+ client_percentage = rand(0..100) if client_percentage.to_i.zero?
43
+ client_percentage
44
+ end
45
+
18
46
  def api_feature_toggle_status(name:)
19
47
  toggle = FtShared.fetch(name: name)
20
48
  return false if toggle.nil?
@@ -1,7 +1,7 @@
1
1
  module FtShared
2
2
  class <<self
3
3
  def fetch(name:)
4
- Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
4
+ Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute ) do
5
5
  JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
6
6
  end
7
7
  rescue StandardError => e
@@ -9,6 +9,15 @@ module FtShared
9
9
  nil
10
10
  end
11
11
 
12
+ def all_toggles
13
+ Rails.cache.fetch("all-feature-toggles", expires_in: 1.minute ) do
14
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/"))
15
+ end
16
+ rescue StandardError => e
17
+ Rails.logger.error "FtHelper::all_toggles error #{e.backtrace}"
18
+ {}
19
+ end
20
+
12
21
  def authentication_header
13
22
  { Authorization: "Bearer #{MumsnetJWT.tokenify}" }
14
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ft_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.6
4
+ version: 2.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murray Catto