Class: Optimizely::OptimizelyFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/optimizely_factory.rb

Class Method Summary collapse

Class Method Details

.blocking_timeout(blocking_timeout) ⇒ Object

Convenience method for setting timeout to block the config call until config has been initialized.

Parameters:

  • blocking_timeout

    Numeric - Time in seconds.



82
83
84
# File 'lib/optimizely/optimizely_factory.rb', line 82

def self.blocking_timeout(blocking_timeout)
  @blocking_timeout = blocking_timeout
end

.custom_instance(sdk_key, datafile = nil, event_dispatcher = nil, logger = nil, error_handler = nil, skip_json_validation = false, user_profile_service = nil, config_manager = nil, notification_center = nil, settings = nil) ⇒ Object

Returns a new optimizely instance.

if @max_event_batch_size and @max_event_flush_interval are nil then default batchsize and flush_interval will be used to setup batchEventProcessor.

Parameters:

  • fallback

    datafile - Optional JSON string datafile.

  • event_dispatcher (defaults to: nil)
    • Optional EventDispatcherInterface Provides a dispatch_event method which if given a URL and params sends a request to it.

  • logger (defaults to: nil)
    • Optional LoggerInterface Provides a log method to log messages. By default nothing would be logged.

  • error_handler (defaults to: nil)
    • Optional ErrorHandlerInterface which provides a handle_error method to handle exceptions.

    By default all exceptions will be suppressed.

  • skip_json_validation (defaults to: false)
    • Optional Boolean param to skip JSON schema validation of the provided datafile.

  • user_profile_service (defaults to: nil)
    • Optional UserProfileServiceInterface Provides methods to store and retreive user profiles.

  • config_manager (defaults to: nil)
    • Optional ConfigManagerInterface Responds to ‘config’ method.

  • notification_center (defaults to: nil)
    • Optional Instance of NotificationCenter.

  • settings:

    Optional instance of OptimizelySdkSettings for sdk configuration.



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
178
179
180
181
182
# File 'lib/optimizely/optimizely_factory.rb', line 133

def self.custom_instance( # rubocop:disable Metrics/ParameterLists
  sdk_key,
  datafile = nil,
  event_dispatcher = nil,
  logger = nil,
  error_handler = nil,
  skip_json_validation = false, # rubocop:disable Style/OptionalBooleanParameter
   = nil,
  config_manager = nil,
  notification_center = nil,
  settings = nil
)

  error_handler ||= NoOpErrorHandler.new
  logger ||= NoOpLogger.new
  notification_center = notification_center.is_a?(Optimizely::NotificationCenter) ? notification_center : NotificationCenter.new(logger, error_handler)

  event_processor = BatchEventProcessor.new(
    event_dispatcher: event_dispatcher || EventDispatcher.new,
    batch_size: @max_event_batch_size,
    flush_interval: @max_event_flush_interval,
    logger: logger,
    notification_center: notification_center
  )

  config_manager ||= Optimizely::HTTPProjectConfigManager.new(
    sdk_key: sdk_key,
    polling_interval: @polling_interval,
    blocking_timeout: @blocking_timeout,
    datafile: datafile,
    logger: logger,
    error_handler: error_handler,
    skip_json_validation: skip_json_validation,
    notification_center: notification_center
  )

  Optimizely::Project.new(
    datafile: datafile,
    event_dispatcher: event_dispatcher,
    logger: logger,
    error_handler: error_handler,
    skip_json_validation: skip_json_validation,
    user_profile_service: ,
    sdk_key: sdk_key,
    config_manager: config_manager,
    notification_center: notification_center,
    event_processor: event_processor,
    settings: settings
  )
end

.default_instance(sdk_key, datafile = nil) ⇒ Object

Returns a new optimizely instance.

Parameters:

  • fallback

    datafile - Optional JSON string datafile.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/optimizely/optimizely_factory.rb', line 90

def self.default_instance(sdk_key, datafile = nil)
  error_handler = NoOpErrorHandler.new
  logger = NoOpLogger.new
  notification_center = NotificationCenter.new(logger, error_handler)

  config_manager = Optimizely::HTTPProjectConfigManager.new(
    sdk_key: sdk_key,
    polling_interval: @polling_interval,
    blocking_timeout: @blocking_timeout,
    datafile: datafile,
    logger: logger,
    error_handler: error_handler,
    notification_center: notification_center
  )

  Optimizely::Project.new(
    datafile: datafile, logger: logger, error_handler: error_handler, sdk_key: sdk_key, config_manager: config_manager, notification_center: notification_center
  )
end

.default_instance_with_config_manager(config_manager) ⇒ Object

Returns a new optimizely instance.

Parameters:

  • config_manager
    • Required ConfigManagerInterface Responds to ‘config’ method.



113
114
115
# File 'lib/optimizely/optimizely_factory.rb', line 113

def self.default_instance_with_config_manager(config_manager)
  Optimizely::Project.new(config_manager: config_manager)
end

.max_event_batch_size(batch_size, logger = NoOpLogger.new) ⇒ Object

Convenience method for setting the maximum number of events contained within a batch.

Parameters:

  • batch_size

    Integer - Sets size of EventQueue.

  • logger (defaults to: NoOpLogger.new)
    • Optional LoggerInterface Provides a log method to log messages.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/optimizely/optimizely_factory.rb', line 31

def self.max_event_batch_size(batch_size, logger = NoOpLogger.new)
  unless batch_size.is_a? Integer
    logger.log(
      Logger::ERROR,
      "Batch size is invalid, setting to default batch size #{BatchEventProcessor::DEFAULT_BATCH_SIZE}."
    )
    return
  end

  unless batch_size.positive?
    logger.log(
      Logger::ERROR,
      "Batch size is negative, setting to default batch size #{BatchEventProcessor::DEFAULT_BATCH_SIZE}."
    )
    return
  end
  @max_event_batch_size = batch_size
end

.max_event_flush_interval(flush_interval, logger = NoOpLogger.new) ⇒ Object

Convenience method for setting the maximum time interval in milliseconds between event dispatches.

Parameters:

  • flush_interval

    Numeric - Time interval between event dispatches.

  • logger (defaults to: NoOpLogger.new)
    • Optional LoggerInterface Provides a log method to log messages.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/optimizely/optimizely_factory.rb', line 53

def self.max_event_flush_interval(flush_interval, logger = NoOpLogger.new)
  unless flush_interval.is_a? Numeric
    logger.log(
      Logger::ERROR,
      "Flush interval is invalid, setting to default flush interval #{BatchEventProcessor::DEFAULT_BATCH_INTERVAL}."
    )
    return
  end

  unless flush_interval.positive?
    logger.log(
      Logger::ERROR,
      "Flush interval is negative, setting to default flush interval #{BatchEventProcessor::DEFAULT_BATCH_INTERVAL}."
    )
    return
  end
  @max_event_flush_interval = flush_interval
end

.polling_interval(polling_interval) ⇒ Object

Convenience method for setting frequency at which datafile has to be polled and ProjectConfig updated.

Parameters:

  • polling_interval

    Numeric - Time in seconds after which to update datafile.



75
76
77
# File 'lib/optimizely/optimizely_factory.rb', line 75

def self.polling_interval(polling_interval)
  @polling_interval = polling_interval
end