pioneer_ruby_sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e909be278c86086ed47667127df968e8b75e1dc639dd718a1344b11e5ba96334
4
+ data.tar.gz: a88077557732acb48a49354805fe4d08d2f2ea0ba4db82ca8771bd8c465b6672
5
+ SHA512:
6
+ metadata.gz: c31dfe2719112b871537b9b9e9649ce2b22775561bfe98964f123ab2014db724782433362075b7fb67089a730c7c4dc9813d0ef04bc1dda8f9b564b8b57c63bc
7
+ data.tar.gz: 8347f79aa3645304ebd8f1748787704e833330968e083972fb8541e38d3b8e72361f204d83c805a56f03cb92ea2bfa364c47b841012b9add596d836dc95f1a8d
@@ -0,0 +1,18 @@
1
+ require_relative 'helper_methods/handle_undefined_feature.rb'
2
+
3
+ class Client_With_Context
4
+ def initialize(context_obj)
5
+ @context = context_obj[:context]
6
+ @client = context_obj[:client]
7
+ @config = context_obj[:config]
8
+ end
9
+
10
+ def get_feature(key, default_value)
11
+ feature_state = @client.get_feature_state(key)
12
+ if !feature_state
13
+ return handle_undefined_feature(key, default_value)
14
+ end
15
+
16
+ return feature_state.strategy.calculate(@context)
17
+ end
18
+ end
data/lib/context.rb ADDED
@@ -0,0 +1,9 @@
1
+ class Context
2
+ def initialize(user_key)
3
+ @user_key = user_key
4
+ end
5
+
6
+ def get_key()
7
+ return @user_key
8
+ end
9
+ end
@@ -0,0 +1,85 @@
1
+ require 'ld-eventsource'
2
+ require 'json'
3
+ require_relative 'feature_state'
4
+ require_relative 'helper_methods/handle_undefined_feature'
5
+
6
+ # class for event source client instance
7
+ class Event_Source_Client
8
+ attr_reader :has_data
9
+
10
+ def initialize(config)
11
+ @config = config
12
+ @features = {}
13
+ @has_data = false
14
+ options = {
15
+ headers: { Authorization: @config[:sdk_key]}
16
+ }
17
+
18
+ sse_client = SSE::Client.new("localhost:3030/features", headers: options[:headers])
19
+
20
+ @api_client = sse_client
21
+ end
22
+
23
+ def start
24
+ handle_events()
25
+ handle_errors()
26
+ end
27
+
28
+ def handle_events
29
+ event_type = nil
30
+
31
+ @api_client.on_event do |event|
32
+ data = JSON.parse(event[:data])
33
+ event_type = data[:type]
34
+ payload = data[:data]
35
+ end
36
+
37
+ puts event_type
38
+ case event_type
39
+ when "ALL_FEATURES"
40
+ handle_all_features(payload)
41
+ when "CREATE_CONNECTION"
42
+ return
43
+ end
44
+ end
45
+
46
+ def handle_errors()
47
+ @api_client.on_error do |error|
48
+ puts "Event Source Failed: ", error
49
+ end
50
+ end
51
+
52
+
53
+ def handle_all_features(all_features)
54
+ all_features = JSON.parse(all_features)
55
+ feature_states = {}
56
+
57
+ all_features.each do |feature|
58
+ modified_feature_state_params = {
59
+ title: feature[:title],
60
+ value: feature[:is_active],
61
+ strategy: {percentage: feature[:rollout]}
62
+ }
63
+ feature_states[feature[:title]] = FeatureState.new(modified_feature_state_params)
64
+ end
65
+
66
+ @features = feature_states
67
+ @has_data = true
68
+ end
69
+
70
+ def get_feature(key, default_value)
71
+ feature_state = get_feature_state(key)
72
+ if !feature_state
73
+ handle_undefined_feature(key, default_value)
74
+ return
75
+ end
76
+
77
+ value = feature_state[:value]
78
+ return value
79
+ end
80
+
81
+
82
+ def get_feature_state(key)
83
+ return @features[:key]
84
+ end
85
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'strategy'
2
+
3
+ class FeatureState
4
+ def initialize(feature_state_object)
5
+ @title = feature_state_object[:title],
6
+ @value = feature_state_object[:value],
7
+ if feature_state_object[:strategy]
8
+ @strategy = Strategy.new(feature_state_object[:strategy][:percentage])
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ def handle_undefined_feature(key, default_value)
2
+ if !default_value.nil?
3
+ puts("Warning: Could not get #{key} from features, using provided default value!")
4
+ return default_value
5
+ end
6
+ raise("Error: #{key} does not exist, cannot get get feature!");
7
+ end
@@ -0,0 +1,50 @@
1
+ require_relative 'event_source_client'
2
+ require_relative 'context'
3
+ require_relative 'client_with_context'
4
+
5
+ class Pioneer_Ruby_Sdk
6
+ def initialize(server_address, sdk_key)
7
+ @server_address = server_address
8
+ @sdk_key = sdk_key
9
+ @configs = {
10
+ server_address: @server_address,
11
+ sdk_key: @sdk_key
12
+ }
13
+ end
14
+
15
+ def connect()
16
+ @client = Event_Source_Client.new(@configs)
17
+ @client.start()
18
+ return self
19
+ end
20
+
21
+ def with_wait_for_data(time_out = 1, polling_attempts = 5)
22
+ attempts = 0
23
+
24
+ until @client.has_data() do
25
+ attempts += 1
26
+ if attempts > polling_attempts
27
+ puts "Cannot connect to Scout, connection timed out"
28
+ break
29
+ end
30
+
31
+ sleep(time_out)
32
+ end
33
+ return self
34
+ end
35
+
36
+ def with_context(user_key)
37
+ user_key = user_key
38
+ context_obj = {
39
+ context: Context.new(user_key),
40
+ client: @client,
41
+ config: @configs
42
+ }
43
+
44
+ return Client_With_Context.new(context_obj)
45
+ end
46
+
47
+ def get_server_address()
48
+ return @server_address
49
+ end
50
+ end
data/lib/strategy.rb ADDED
@@ -0,0 +1,18 @@
1
+ class Strategy
2
+ def initialize(percentage)
3
+ @percentage = percentage
4
+ @modulus = 100
5
+ end
6
+
7
+
8
+ def calculate(context)
9
+ key = context.get_key()
10
+ hashed_percentage = get_hash_based_percentage(key)
11
+ return hashed_percentage <= @percentage
12
+ end
13
+
14
+ def get_hash_based_percentage(string)
15
+ sum = string.bytes.sum()
16
+ return ((sum % @modulus) + 1) / @modulus
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pioneer_ruby_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Zheng
8
+ - Kyle Ledoux
9
+ - Laura Davies
10
+ - Elizabeth Tackett
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2021-07-19 00:00:00.000000000 Z
15
+ dependencies: []
16
+ description:
17
+ email:
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/client_with_context.rb
23
+ - lib/context.rb
24
+ - lib/event_source_client.rb
25
+ - lib/feature_state.rb
26
+ - lib/helper_methods/handle_undefined_feature.rb
27
+ - lib/pioneer_ruby_sdk.rb
28
+ - lib/strategy.rb
29
+ homepage:
30
+ licenses: []
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.1.4
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: SDK wrapper for using Pioneer feature flag application
51
+ test_files: []