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 +4 -4
- data/lib/ft_gem/controllers/helpers.rb +32 -4
- data/lib/ft_gem/shared/ft_shared.rb +10 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 79e10fb49eafa5f0693c8ca7d6198cb66fcbc3f5404d388ae2332fd38d808e51
|
4
|
+
data.tar.gz: 8b7b582fefdb9161507249942425d5b6a2e2a16d1d9ecdca30739ed1e0a79329
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|