ft_gem 1.2.1 → 2.0.0

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: 44e2fce5acf4a16f5e02555bdcde2bb3c6e8e1ff4613ed7ccb0d39e573122289
4
- data.tar.gz: da78f2742daaa51f0f48b59fb8f2d88aee9bcd7bdad18aaf6c6489c04339a82c
3
+ metadata.gz: ddd59a69fd56a0a0f2a4842df86b70fd7aeb4c152623162cf6d6c436f846dfbc
4
+ data.tar.gz: 829afb380c5249301db17e5afe19987c04b68c4a47ba5ae24182cf1ac194b446
5
5
  SHA512:
6
- metadata.gz: ac836fae0a59a88dea6b02057f0210ec33c6704d9d1f4bb3f8064109742e5dace304dac19f248a4a77b732455c906083150740f91b2b4b21998576ddb5f67eb3
7
- data.tar.gz: ac6e021812d9a094332d65b4282f01a2ec201cd2693062c01e53aa480c874f04d1593ea001a1513e53542151cf55084c8f243d0ab9368ba2302b6333a0461423
6
+ metadata.gz: 9e78b4a561caf5a6812d4a5fdc25182561c5b90d0a9b7276613bef35d8af3364fb666a2a4f460dd96efd812a9c09f8d1f5b58e7aa4be560cfbd0e29e665bdab7
7
+ data.tar.gz: 2a4d4fdb811e851a27238759413f05ca2c89a18b7408aa50e2d525ecbc66e74ec30cf2e0ff3fc5674e6091940ccf3385fc1a6ad405b7c394263e1dee4010f902
@@ -0,0 +1,54 @@
1
+ module FtGem
2
+ module Controllers
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+ TOGGLE_COOKIE_NAME = 'mnft'.freeze
6
+
7
+ def feature_toggle_status(name:)
8
+ cookies[TOGGLE_COOKIE_NAME] = '' if cookies[TOGGLE_COOKIE_NAME].nil?
9
+ add_feature_toggle(name: name) unless cookies[TOGGLE_COOKIE_NAME].include?("#{name} ")
10
+ update_feature_toggle(name: name) if cookies[TOGGLE_COOKIE_NAME].include?("#{name} ")
11
+ toggles = cookies[TOGGLE_COOKIE_NAME].split('|')
12
+ index = toggles.index { |toggle| toggle.include?("#{name} ") }
13
+ client_percentage = toggles[index].split(' ')[1]
14
+ toggle_percentage = toggles[index].split(' ')[2]
15
+ client_percentage.to_i <= toggle_percentage.to_i
16
+ rescue StandardError
17
+ Rails.logger.error "FtGem::feature_toggle_status error #{e.backtrace}"
18
+ false
19
+ end
20
+
21
+ def add_feature_toggle(name:)
22
+ data = FtShared.fetch(name: name)
23
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
24
+ client_percentage = data['feature_toggle']['client_percentage']
25
+ cookies[TOGGLE_COOKIE_NAME] = "#{cookies[TOGGLE_COOKIE_NAME]}|#{name} #{client_percentage} #{toggle_percentage}"
26
+ rescue StandardError
27
+ Rails.logger.error "FtGem::add_feature_toggle error #{e.backtrace}"
28
+ nil
29
+ end
30
+
31
+ def update_feature_toggle(name:)
32
+ data = FtShared.fetch(name: name)
33
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
34
+ toggles = cookies[TOGGLE_COOKIE_NAME].split('|')
35
+ index = toggles.index { |toggle| toggle.include?("#{name} ") }
36
+ client_percentage = toggles[index].split(' ')[1]
37
+ toggles[index] = "#{name} #{client_percentage} #{toggle_percentage}"
38
+ cookies[TOGGLE_COOKIE_NAME] = toggles.join('|')
39
+ rescue StandardError
40
+ Rails.logger.error "FTApiGem::update_feature_toggle error #{e.backtrace}"
41
+ nil
42
+ end
43
+
44
+ def api_feature_toggle_status(name:)
45
+ toggle = FtShared.fetch(name: name)
46
+ return false if toggle.nil?
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
+ end
53
+ end
54
+ end
@@ -0,0 +1,18 @@
1
+ module FtShared
2
+ class <<self
3
+ def fetch(name:)
4
+ Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
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
+ end
7
+ rescue StandardError => e
8
+ Rails.logger.error "FtHelper::fetch error #{e.backtrace}"
9
+ nil
10
+ end
11
+
12
+ private
13
+
14
+ def authentication_header
15
+ { Authorization: "Bearer #{MumsnetJWT.tokenify}" }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module FtGem
2
+ module View
3
+ module Helpers
4
+ def self.status(name:)
5
+ toggle = FtShared.fetch(name: name)
6
+ return false if toggle.nil?
7
+ toggle['feature_toggle']['toggle_percentage'].to_i == 100
8
+ rescue StandardError => e
9
+ Rails.logger.error "FtHelper::status error #{e.backtrace}"
10
+ false
11
+ end
12
+ end
13
+ end
14
+ end
data/lib/ft_gem.rb CHANGED
@@ -5,7 +5,7 @@ module FtGem
5
5
  end
6
6
 
7
7
  class Railtie < Rails::Railtie
8
- initializer 'body_class.helper' do |_app|
8
+ initializer 'ft_gem.view.helpers' do |_app|
9
9
  ActionView::Base.send :include, FtGem::View::Helper
10
10
  end
11
11
  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.2.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murray Catto
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: http
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email:
29
43
  executables: []
@@ -31,6 +45,9 @@ extensions: []
31
45
  extra_rdoc_files: []
32
46
  files:
33
47
  - lib/ft_gem.rb
48
+ - lib/ft_gem/controllers/helpers.rb
49
+ - lib/ft_gem/shared/ft_shared.rb
50
+ - lib/ft_gem/view/helpers.rb
34
51
  homepage:
35
52
  licenses: []
36
53
  metadata: {}