Skip to content

Commit

Permalink
Merge 3ccb318 into 6328d73
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Feb 22, 2024
2 parents 6328d73 + 3ccb318 commit c07751f
Show file tree
Hide file tree
Showing 47 changed files with 229 additions and 2,930 deletions.
2 changes: 1 addition & 1 deletion lib/unleash.rb
Expand Up @@ -9,7 +9,7 @@ module Unleash
TIME_RESOLUTION = 3

class << self
attr_accessor :configuration, :toggle_fetcher, :toggles, :toggle_metrics, :reporter, :segment_cache, :logger
attr_accessor :configuration, :toggle_fetcher, :reporter, :logger, :engine
end

self.configuration = Unleash::Configuration.new
Expand Down
44 changes: 0 additions & 44 deletions lib/unleash/activation_strategy.rb

This file was deleted.

38 changes: 24 additions & 14 deletions lib/unleash/client.rb
Expand Up @@ -2,7 +2,7 @@
require 'unleash/toggle_fetcher'
require 'unleash/metrics_reporter'
require 'unleash/scheduled_executor'
require 'unleash/feature_toggle'
require 'unleash/variant'
require 'unleash/util/http'
require 'logger'
require 'time'
Expand All @@ -11,14 +11,17 @@ module Unleash
class Client
attr_accessor :fetcher_scheduled_executor, :metrics_scheduled_executor

# rubocop:disable Metrics/AbcSize
def initialize(*opts)
Unleash.configuration = Unleash::Configuration.new(*opts) unless opts.empty?
Unleash.configuration.validate!

Unleash.logger = Unleash.configuration.logger.clone
Unleash.logger.level = Unleash.configuration.log_level
Unleash.engine = YggdrasilEngine.new
Unleash.engine.register_custom_strategies(Unleash.configuration.strategies.custom_strategies)

Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
Unleash.toggle_fetcher = Unleash::ToggleFetcher.new Unleash.engine
if Unleash.configuration.disable_client
Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!"
Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!"
Expand All @@ -30,6 +33,7 @@ def initialize(*opts)
start_toggle_fetcher
start_metrics unless Unleash.configuration.disable_metrics
end
# rubocop:enable Metrics/AbcSize

def is_enabled?(feature, context = nil, default_value_param = false, &fallback_blk)
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"
Expand All @@ -40,15 +44,16 @@ def is_enabled?(feature, context = nil, default_value_param = false, &fallback_b
default_value_param
end

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first
if toggle_as_hash.nil?
toggle_enabled = Unleash.engine.enabled?(feature, context)
if toggle_enabled.nil?
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} not found"
Unleash.engine.count_toggle(feature, false)
return default_value
end

toggle = Unleash::FeatureToggle.new(toggle_as_hash, Unleash&.segment_cache)
Unleash.engine.count_toggle(feature, toggle_enabled)

toggle.is_enabled?(context)
toggle_enabled
end

def is_disabled?(feature, context = nil, default_value_param = true, &fallback_blk)
Expand All @@ -73,21 +78,27 @@ def if_disabled(feature, context = nil, default_value = true, &blk)
def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"

toggle_as_hash = Unleash&.toggles&.select{ |toggle| toggle['name'] == feature }&.first

if toggle_as_hash.nil?
toggle_enabled = Unleash.engine.enabled?(feature, context)
if toggle_enabled.nil?
Unleash.logger.debug "Unleash::Client.get_variant feature: #{feature} not found"
Unleash.engine.count_toggle(feature, false)
return fallback_variant
end

toggle = Unleash::FeatureToggle.new(toggle_as_hash)
variant = toggle.get_variant(context, fallback_variant)
Unleash.engine.count_toggle(feature, toggle_enabled)

variant = Unleash.engine.get_variant(feature, context)

if variant.nil?
Unleash.logger.debug "Unleash::Client.get_variant variants for feature: #{feature} not found"
Unleash.engine.count_variant(feature, "disabled")
return fallback_variant
end

variant = Variant.new(variant)

Unleash.engine.count_variant(feature, variant.name)

# TODO: Add to README: name, payload, enabled (bool)

variant
Expand Down Expand Up @@ -117,7 +128,7 @@ def info
'appName': Unleash.configuration.app_name,
'instanceId': Unleash.configuration.instance_id,
'sdkVersion': "unleash-client-ruby:" + Unleash::VERSION,
'strategies': Unleash.strategies.keys,
'strategies': Unleash.strategies.known_strategies,
'started': Time.now.iso8601(Unleash::TIME_RESOLUTION),
'interval': Unleash.configuration.metrics_interval_in_millis
}
Expand All @@ -136,7 +147,6 @@ def start_toggle_fetcher
end

def start_metrics
Unleash.toggle_metrics = Unleash::Metrics.new
Unleash.reporter = Unleash::MetricsReporter.new
self.metrics_scheduled_executor = Unleash::ScheduledExecutor.new(
'MetricsReporter',
Expand All @@ -162,7 +172,7 @@ def register
end

def disabled_variant
@disabled_variant ||= Unleash::FeatureToggle.disabled_variant
@disabled_variant ||= Unleash::Variant.disabled_variant
end

def first_fetch_is_eager
Expand Down
4 changes: 2 additions & 2 deletions lib/unleash/configuration.rb
Expand Up @@ -40,9 +40,9 @@ def metrics_interval_in_millis
def validate!
return if self.disable_client

raise ArgumentError, "URL and app_name are required parameters." if self.app_name.nil? || self.url.nil?
raise ArgumentError, "app_name is a required parameter." if self.app_name.nil?

validate_custom_http_headers!(self.custom_http_headers)
validate_custom_http_headers!(self.custom_http_headers) unless self.url.nil?
end

def refresh_backup_file!
Expand Down
115 changes: 0 additions & 115 deletions lib/unleash/constraint.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/unleash/context.rb
Expand Up @@ -23,6 +23,22 @@ def to_s
",app_name=#{@app_name},environment=#{@environment}>"
end

def as_json
{
appName: self.app_name,
environment: self.environment,
userId: self.user_id,
sessionId: self.session_id,
remoteAddress: self.remote_address,
currentTime: self.current_time,
properties: self.properties
}
end

def to_json(*options)
as_json(*options).to_json(*options)
end

def to_h
ATTRS.map{ |attr| [attr, self.send(attr)] }.to_h.merge(properties: @properties)
end
Expand Down

0 comments on commit c07751f

Please sign in to comment.