class Rox::Core::Core

Public Class Methods

new() click to toggle source
# File lib/rox/core/core.rb, line 33
def initialize
  @flag_repository = FlagRepository.new
  @custom_property_repository = CustomPropertyRepository.new
  @target_group_repository = TargetGroupRepository.new
  @experiment_repository = ExperimentRepository.new
  @user_unhandled_error_invoker = Rox::Core::UserspaceUnhandledErrorInvoker.new
  @parser = Parser.new(@user_unhandled_error_invoker)

  @configuration_fetched_invoker = ConfigurationFetchedInvoker.new(@user_unhandled_error_invoker)
  @registerer = Registerer.new(@flag_repository)

  @sdk_settings = nil
  @impression_invoker = nil
  @flag_setter = nil
  @error_reporter = nil
  @configuration_fetcher = nil
  @last_configurations = nil
  @internal_flags = nil
  @push_updates_listener = nil
end

Public Instance Methods

add_custom_property(property) click to toggle source
# File lib/rox/core/core.rb, line 184
def add_custom_property(property)
  @custom_property_repository.add_custom_property(property)
end
add_custom_property_if_not_exists(property) click to toggle source
# File lib/rox/core/core.rb, line 188
def add_custom_property_if_not_exists(property)
  @custom_property_repository.add_custom_property_if_not_exists(property)
end
context=(context) click to toggle source
# File lib/rox/core/core.rb, line 180
def context=(context)
  @parser.global_context = context
end
dump_state() click to toggle source
# File lib/rox/core/core.rb, line 220
def dump_state
  @state_sender.dump_state
end
dynamic_api(entities_provider) click to toggle source
# File lib/rox/core/core.rb, line 216
def dynamic_api(entities_provider)
  Rox::Core::DynamicApi.new(@flag_repository, entities_provider)
end
fetch() click to toggle source
# File lib/rox/core/core.rb, line 144
def fetch
  return if @configuration_fetcher.nil?

  signature_verifier = if @rox_options.self_managed?
                         SignatureVerifierMock.new
                       else
                         SignatureVerifier.new
                       end
  configuration_parser = ConfigurationParser.new(signature_verifier, @error_reporter,
                                                 @configuration_fetched_invoker)
  result = @configuration_fetcher.fetch
  return if result.nil?

  configuration = configuration_parser.parse(result, @sdk_settings)
  return if configuration.nil?

  @experiment_repository.experiments = configuration.experiments
  @target_group_repository.target_groups = configuration.target_groups
  @flag_setter.set_experiments

  has_changes = @last_configurations.nil? || @last_configurations.data != result.data
  @last_configurations = result
  @configuration_fetched_invoker.invoke(FetcherStatus::APPLIED_FROM_NETWORK, configuration.signature_date,
                                        has_changes)
end
register(*args) click to toggle source
# File lib/rox/core/core.rb, line 174
def register(*args)
  rox_container = args.pop
  namespace = args.length == 1 ? args.pop : ''
  @registerer.register_instance(rox_container, namespace)
end
register_with_namespace(namespace, rox_container) click to toggle source
# File lib/rox/core/core.rb, line 170
def register_with_namespace(namespace, rox_container)
  @registerer.register_instance(rox_container, namespace)
end
setup(sdk_settings, device_properties) click to toggle source
# File lib/rox/core/core.rb, line 58
def setup(sdk_settings, device_properties)
  @sdk_settings = sdk_settings
  @rox_options = device_properties.rox_options

  experiments_extensions = ExperimentsExtensions.new(@parser, @target_group_repository, @flag_repository,
                                                     @experiment_repository)
  properties_extensions = PropertiesExtensions.new(@parser, @custom_property_repository, @rox_options.dynamic_property_rule_handler)
  experiments_extensions.extend
  properties_extensions.extend

  roxy_path = @rox_options.nil? || @rox_options.roxy_url.nil? ? nil : @rox_options.roxy_url

  validate_api_key(sdk_settings&.api_key) if roxy_path.nil?

  # TODO: Analytics.Analytics.Initialize(deviceProperties.RolloutKey, deviceProperties)
  @internal_flags = InternalFlags.new(@experiment_repository, @parser, @rox_options)

  @analytics_client = Analytics.new(sdk_settings.api_key).client
  # TODO: impressionInvoker = new ImpressionInvoker(internalFlags, customPropertyRepository, deviceProperties, Analytics.Analytics.Client, roxyPath != null);
  @impression_invoker = ImpressionInvoker.new(@internal_flags, @custom_property_repository, device_properties,
                                              @analytics_client, !roxy_path.nil?, @user_unhandled_error_invoker)
  @flag_setter = FlagSetter.new(@flag_repository, @parser, @experiment_repository, @impression_invoker)
  buid = BUID.new(sdk_settings, device_properties, @flag_repository, @custom_property_repository)

  request_config_builder = RequestConfigurationBuilder.new(sdk_settings, buid, device_properties)

  client_request = Request.new
  err_reporter_request = Request.new

  @error_reporter = ErrorReporter.new(err_reporter_request, device_properties, buid)

  if @rox_options.self_managed?
    @configuration_fetcher = ConfigurationFetcherSelfManaged.new(request_config_builder, client_request,
                                                                 @configuration_fetched_invoker)
    @state_sender = StateSender.new(@sdk_settings, device_properties, @flag_repository,
                                    @custom_property_repository)
    @flag_repository.register_flag_added_handler { @state_sender.delayed_send }
    @custom_property_repository.register_property_added_handler { @state_sender.delayed_send }
  elsif roxy_path.nil?
    @configuration_fetcher = ConfigurationFetcher.new(request_config_builder, client_request,
                                                      @configuration_fetched_invoker)
    @state_sender = StateSender.new(@sdk_settings, device_properties, @flag_repository,
                                    @custom_property_repository)
    @flag_repository.register_flag_added_handler { @state_sender.delayed_send }
    @custom_property_repository.register_property_added_handler { @state_sender.delayed_send }
  else
    @configuration_fetcher = ConfigurationFetcherRoxy.new(request_config_builder, client_request,
                                                          @configuration_fetched_invoker)
  end

  configuration_fetched_handler = nil
  configuration_fetched_handler = @rox_options.configuration_fetched_handler unless @rox_options.nil?

  @configuration_fetched_invoker.register_fetched_handler(&wrap_configuration_fetched_handler(&configuration_fetched_handler))

  @thread = Thread.new do
    Thread.current.report_on_exception = false if Thread.current.respond_to?(:report_on_exception)
    fetch
    @state_sender&.send

    if !@rox_options.nil? && !@rox_options.impression_handler.nil?
      @impression_invoker.register_impression_handler(&@rox_options.impression_handler)
    end

    if !@rox_options.nil? && !@rox_options.fetch_interval.nil?
      PeriodicTask.run(@rox_options.fetch_interval) { fetch }
    end
  end
end
shutdown() click to toggle source
# File lib/rox/core/core.rb, line 128
def shutdown
  return if @thread.nil?

  Thread.kill(@thread)
  @thread = nil

  unless @push_updates_listener.nil?
    @push_updates_listener.stop
    @push_updates_listener = nil
  end

  return if @analytics_client.nil?

  @analytics_client.flush
end
start_or_stop_push_updated_listener() click to toggle source
# File lib/rox/core/core.rb, line 199
def start_or_stop_push_updated_listener
  if @internal_flags.enabled?('rox.internal.pushUpdates')
    if @push_updates_listener.nil?
      @push_updates_listener = NotificationListener.new(Environment.notifications_path, @sdk_settings.api_key)
      @push_updates_listener.on 'changed' do |_data|
        fetch
      end
      @push_updates_listener.start
    end
  else
    unless @push_updates_listener.nil?
      @push_updates_listener.stop
      @push_updates_listener = nil
    end
  end
end
userspace_unhandled_error_handler=(handler) click to toggle source
# File lib/rox/core/core.rb, line 54
def userspace_unhandled_error_handler=(handler)
  @user_unhandled_error_invoker.handler = handler
end
validate_api_key(api_key) click to toggle source
# File lib/rox/core/core.rb, line 224
def validate_api_key(api_key)
  valid_api_key_pattern = /^[a-f\d]{24}$/
  if api_key&.strip&.empty?
    raise ArgumentError, 'Blank Rollout api key - must be specified'
  elsif !valid_api_key_pattern.match(api_key)
    raise ArgumentError, 'Illegal Rollout api key'
  end
end
wrap_configuration_fetched_handler(&handler) click to toggle source
# File lib/rox/core/core.rb, line 192
def wrap_configuration_fetched_handler(&handler)
  lambda do |args|
    start_or_stop_push_updated_listener unless args.fetcher_status == FetcherStatus::ERROR_FETCHED_FAILED
    handler&.call(args)
  end
end