ft_gem 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/ft_gem.rb +47 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 85ac99a4f93c0e5a2d90ea511af556b4c9f21b1202e27ac5476feb1406e5815a
4
+ data.tar.gz: eca16981030c1cd27120a589444ba93e9e41338769613f809a3d017870fa449a
5
+ SHA512:
6
+ metadata.gz: 1c30233f052ca54eb89ec8e7451f9f0acbfc90288728f1826c5377c1c00d3ef408f55ef531df45ed8794e461b9754c57ec1b578feb87c6e3484b66c2557f3ef8
7
+ data.tar.gz: 4193aea53e8b196f52039e576e1596b70bd95a9b32c04546b7ca6034d8d11f399fd5408b5bb0194cdce58dee104c7029c500018432bef5d9483fcacd1d5dac73
data/lib/ft_gem.rb ADDED
@@ -0,0 +1,47 @@
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
+ false
15
+ end
16
+
17
+ def add_feature_toggle(name:)
18
+ data = Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
19
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
20
+ end
21
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
22
+ client_percentage = data['feature_toggle']['client_percentage']
23
+ cookies[TOGGLE_COOKIE_NAME] = "#{cookies[TOGGLE_COOKIE_NAME]}|#{name} #{client_percentage} #{toggle_percentage}"
24
+ rescue StandardError
25
+ nil
26
+ end
27
+
28
+ def update_feature_toggle(name:)
29
+ data = Rails.cache.fetch("feature-toggle-#{name}", expires_in: 1.minute) do
30
+ JSON.parse(HTTP.headers(authentication_header).get("#{ENV['SITE_URL']}/service/ft/api/v1/feature-toggles/status", params: { name: name }).body.to_s)
31
+ end
32
+ toggle_percentage = data['feature_toggle']['toggle_percentage']
33
+ toggles = cookies[TOGGLE_COOKIE_NAME].split('|')
34
+ index = toggles.index { |toggle| toggle.include?("#{name} ") }
35
+ client_percentage = toggles[index].split(' ')[1]
36
+ toggles[index] = "#{name} #{client_percentage} #{toggle_percentage}"
37
+ cookies[TOGGLE_COOKIE_NAME] = toggles.join('|')
38
+ rescue StandardError
39
+ nil
40
+ end
41
+
42
+ private
43
+
44
+ def authentication_header
45
+ { Authorization: "Bearer #{MumsnetJWT.tokenify(extra_payload: extra_payload)}" }
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ft_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Murray Catto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mumsnet_jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/ft_gem.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.7.8
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Client side feature toggle
57
+ test_files: []