ft_gem 1.0.10 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ft_gem.rb +67 -0
  3. data/lib/ft_helper.rb +26 -0
  4. metadata +3 -2
  5. data/lib/ft.rb +0 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 840c4754c69387e81fcdcc5ac899976f09b7d3a5a06bb3c48765e91936bb34cf
4
- data.tar.gz: f4d602196bdfe7973dd021f7ea8639f756e16d5184f4278fd8d6e4da5fb55ec1
3
+ metadata.gz: 725f9e88aa8a957ee3d88a39481bc6772690303ca3ea4a9f3f99b900690f602e
4
+ data.tar.gz: 88dbe1d5ad0add3f660850c86fa3db4a88b6ac654be3c41d3a23393f1f86f465
5
5
  SHA512:
6
- metadata.gz: 5ed119754417344da93de612bc9bf3278867eae947558bfede7e2d210b88ab6e843844df4bf44553f57cf971bdc0eb643e17abd49ce8b23d89c62ebac2eccf81
7
- data.tar.gz: 8ac51bac3386cff3dfb423cd097dfe181d46f9cc56aae7929feae8a242bdbdd674edb1a7dee84955df3b27bd7341316109b2f48316caf935c0ea2aa4b6bcaba6
6
+ metadata.gz: 95919afbbcb722cf66cbd4db5a7ae982efafb4a03c3188e9d5f88f3e54837c6c7d90f73b0918b9c0067edab7de26707b0adda3b7d118c838683d4c7a59bb42b6
7
+ data.tar.gz: f44b0dc1ef9f017b4f58df46fc69d9c278b6af18bf5ec720b3c83cbe73ad4d00c302fbe850d51bd594c90c5fbca232b396e51afbd13169e386307337fd2e5d3d
data/lib/ft_gem.rb ADDED
@@ -0,0 +1,67 @@
1
+ module FtGem
2
+ TOGGLE_COOKIE_NAME = 'mnft'.freeze
3
+
4
+ def feature_toggle_status(name:)
5
+ cookies[TOGGLE_COOKIE_NAME] = '' if cookies[TOGGLE_COOKIE_NAME].nil?
6
+ add_feature_toggle(name: name) unless cookies[TOGGLE_COOKIE_NAME].include?("#{name} ")
7
+ update_feature_toggle(name: name) if cookies[TOGGLE_COOKIE_NAME].include?("#{name} ")
8
+ toggles = cookies[TOGGLE_COOKIE_NAME].split('|')
9
+ index = toggles.index { |toggle| toggle.include?("#{name} ") }
10
+ client_percentage = toggles[index].split(' ')[1]
11
+ toggle_percentage = toggles[index].split(' ')[2]
12
+ client_percentage.to_i <= toggle_percentage.to_i
13
+ rescue StandardError
14
+ Rails.logger.error "FtGem::feature_toggle_status error #{e.backtrace}"
15
+ false
16
+ end
17
+
18
+ def add_feature_toggle(name:)
19
+ data = Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
20
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
21
+ end
22
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
23
+ client_percentage = data['feature_toggle']['client_percentage']
24
+ cookies[TOGGLE_COOKIE_NAME] = "#{cookies[TOGGLE_COOKIE_NAME]}|#{name} #{client_percentage} #{toggle_percentage}"
25
+ rescue StandardError
26
+ Rails.logger.error "FtGem::add_feature_toggle error #{e.backtrace}"
27
+ nil
28
+ end
29
+
30
+ def update_feature_toggle(name:)
31
+ data = Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
32
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
33
+ end
34
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
35
+ toggles = cookies[TOGGLE_COOKIE_NAME].split('|')
36
+ index = toggles.index { |toggle| toggle.include?("#{name} ") }
37
+ client_percentage = toggles[index].split(' ')[1]
38
+ toggles[index] = "#{name} #{client_percentage} #{toggle_percentage}"
39
+ cookies[TOGGLE_COOKIE_NAME] = toggles.join('|')
40
+ rescue StandardError
41
+ Rails.logger.error "FTApiGem::update_feature_toggle error #{e.backtrace}"
42
+ nil
43
+ end
44
+
45
+ def api_feature_toggle_status(name:)
46
+ toggle = api_fetch_feature_toggle(name: name)
47
+ toggle['feature_toggle']['toggle_percentage'].to_i == 100
48
+ rescue StandardError => e
49
+ Rails.logger.error "FtGem::api_feature_toggle_status error #{e.backtrace}"
50
+ false
51
+ end
52
+
53
+ def api_fetch_feature_toggle(name:)
54
+ Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
55
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
56
+ end
57
+ rescue StandardError => e
58
+ Rails.logger.error "FtGem::api_fetch_feature_toggle error #{e.backtrace}"
59
+ nil
60
+ end
61
+
62
+ private
63
+
64
+ def authentication_header
65
+ { Authorization: "Bearer #{MumsnetJWT.tokenify}" }
66
+ end
67
+ end
data/lib/ft_helper.rb ADDED
@@ -0,0 +1,26 @@
1
+ module FtHelper
2
+ class <<self
3
+ def status(name:)
4
+ toggle = fetch(name: name)
5
+ toggle['feature_toggle']['toggle_percentage'].to_i == 100
6
+ rescue StandardError => e
7
+ Rails.logger.error "FtHelper::status error #{e.backtrace}"
8
+ false
9
+ end
10
+
11
+ private
12
+
13
+ def fetch(name:)
14
+ Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
15
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
16
+ end
17
+ rescue StandardError => e
18
+ Rails.logger.error "FtHelper::fetch error #{e.backtrace}"
19
+ nil
20
+ end
21
+
22
+ def authentication_header
23
+ { Authorization: "Bearer #{MumsnetJWT.tokenify}" }
24
+ end
25
+ end
26
+ 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: 1.0.10
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murray Catto
@@ -30,7 +30,8 @@ executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
- - lib/ft.rb
33
+ - lib/ft_gem.rb
34
+ - lib/ft_helper.rb
34
35
  homepage:
35
36
  licenses: []
36
37
  metadata: {}
data/lib/ft.rb DELETED
@@ -1 +0,0 @@
1
- require 'ft_gem.rb'