Class: Rox::Core::Core

Inherits:
Object
  • Object
show all
Includes:
Helpers::ApiKey
Defined in:
lib/rox/core/core.rb

Constant Summary

Constants included from Helpers::ApiKey

Helpers::ApiKey::MONGO_API_KEY_PATTERN, Helpers::ApiKey::UUID_API_KEY_PATTERN

Instance Method Summary collapse

Methods included from Helpers::ApiKey

#is_cbp?, #validate_api_key

Constructor Details

#initializeCore

Returns a new instance of Core.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rox/core/core.rb', line 36

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
  @analytics_client = nil
end

Instance Method Details

#add_custom_property(property) ⇒ Object



193
194
195
# File 'lib/rox/core/core.rb', line 193

def add_custom_property(property)
  @custom_property_repository.add_custom_property(property)
end

#add_custom_property_if_not_exists(property) ⇒ Object



197
198
199
# File 'lib/rox/core/core.rb', line 197

def add_custom_property_if_not_exists(property)
  @custom_property_repository.add_custom_property_if_not_exists(property)
end

#context=(context) ⇒ Object



189
190
191
# File 'lib/rox/core/core.rb', line 189

def context=(context)
  @parser.global_context = context
end

#dump_stateObject



222
223
224
# File 'lib/rox/core/core.rb', line 222

def dump_state
  @state_sender.dump_state
end

#dynamic_api(entities_provider) ⇒ Object



218
219
220
# File 'lib/rox/core/core.rb', line 218

def dynamic_api(entities_provider)
  Rox::Core::DynamicApi.new(@flag_repository, entities_provider)
end

#fetchObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/rox/core/core.rb', line 153

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, @rox_options)
  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) ⇒ Object



183
184
185
186
187
# File 'lib/rox/core/core.rb', line 183

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) ⇒ Object



179
180
181
# File 'lib/rox/core/core.rb', line 179

def register_with_namespace(namespace, rox_container)
  @registerer.register_instance(rox_container, namespace)
end

#reset_environment!(api_key, rox_options) ⇒ Object

Raises:

  • (ArgumentError)


226
227
228
229
230
231
232
233
234
# File 'lib/rox/core/core.rb', line 226

def reset_environment!(api_key, rox_options)
  raise ArgumentError, 'Blank Feature Flag API key - must be specified' unless api_key

  if is_cbp?(api_key)
    Rox::Core::Environment.set_platform(rox_options)
  else
    Rox::Core::Environment.reset(rox_options)
  end
end

#setup(sdk_settings, device_properties) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rox/core/core.rb', line 62

def setup(sdk_settings, device_properties)
  @sdk_settings = sdk_settings
  @rox_options = device_properties.rox_options
  reset_environment!(sdk_settings&.api_key, @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?

  @internal_flags = InternalFlags.new(@experiment_repository, @parser, @rox_options)

  if roxy_path.nil?
    @analytics_client = create_analytics_client(device_properties)
  end

  @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_invoker.register_start_stop_push(proc do |args|
    start_or_stop_push_updated_listener unless args.fetcher_status == FetcherStatus::ERROR_FETCHED_FAILED
  end)

  if !@rox_options.nil? && !@rox_options.configuration_fetched_handler.nil?
    @configuration_fetched_invoker.register_fetched_handler(&@rox_options.configuration_fetched_handler)
  end

  @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

#shutdownObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rox/core/core.rb', line 137

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_listenerObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rox/core/core.rb', line 201

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) ⇒ Object



58
59
60
# File 'lib/rox/core/core.rb', line 58

def userspace_unhandled_error_handler=(handler)
  @user_unhandled_error_invoker.handler = handler
end