Class: WingifyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/wingify/wingify_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WingifyBuilder

Initialize the VWOBuilder with the given options

Parameters:

  • options (Hash)

    The options for the VWOBuilder



36
37
38
39
40
41
42
43
44
# File 'lib/wingify/wingify_builder.rb', line 36

def initialize(options)
  @options = options
  @settings = nil
  @storage = nil
  @log_manager = nil
  @is_settings_fetch_in_progress = false
  @is_valid_poll_interval_passed_from_init = false
  @wingify_instance = nil
end

Instance Attribute Details

#is_settings_fetch_in_progressObject (readonly)

Returns the value of attribute is_settings_fetch_in_progress.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def is_settings_fetch_in_progress
  @is_settings_fetch_in_progress
end

#is_valid_poll_interval_passed_from_initObject (readonly)

Returns the value of attribute is_valid_poll_interval_passed_from_init.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def is_valid_poll_interval_passed_from_init
  @is_valid_poll_interval_passed_from_init
end

#log_managerObject (readonly)

Returns the value of attribute log_manager.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def log_manager
  @log_manager
end

#settingsObject (readonly)

Returns the value of attribute settings.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def settings
  @settings
end

#storageObject (readonly)

Returns the value of attribute storage.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def storage
  @storage
end

#wingify_instanceObject (readonly)

Returns the value of attribute wingify_instance.



32
33
34
# File 'lib/wingify/wingify_builder.rb', line 32

def wingify_instance
  @wingify_instance
end

Instance Method Details

#build(settings) ⇒ WingifyClient

Build the WingifyClient instance

Parameters:

  • settings (Hash)

    The settings for the client instance

Returns:



193
194
195
196
197
198
# File 'lib/wingify/wingify_builder.rb', line 193

def build(settings)
  @wingify_instance = WingifyClient.new(settings, @options)
  # if poll_interval is not present in options, set it to the pollInterval from settings
  update_poll_interval_and_check_and_poll(settings)
  @wingify_instance
end

#check_and_pollVWOBuilder

This method is used to check and poll the settings from the server

Returns:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wingify/wingify_builder.rb', line 218

def check_and_poll
  @thread_pool = NetworkManager.instance.get_client.get_thread_pool
  @thread_pool.post do
    loop do
      sleep(@options[:poll_interval]/ 1000.0)
      begin
        latest_settings = fetch_settings(true)
        if latest_settings && latest_settings.to_json != @settings.to_json
          @settings = latest_settings
          LoggerService.log(LogLevelEnum::INFO, "POLLING_SET_SETTINGS")
          @wingify_instance.update_settings(latest_settings.clone, false) if @wingify_instance
          update_poll_interval_and_check_and_poll(latest_settings, false)
        elsif latest_settings
          LoggerService.log(LogLevelEnum::INFO, "POLLING_NO_CHANGE_IN_SETTINGS")
        end
      rescue StandardError => e
        LoggerService.log(LogLevelEnum::ERROR, "ERROR_UPDATING_SETTINGS", { err: e.message, an: ApiEnum::INIT})
      end
    end
  end
end

#dispatcher(events, callback) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/wingify/wingify_builder.rb', line 240

def dispatcher(events, callback)
  BatchEventDispatcherUtil.dispatch(
    {
      ev: events
    },
    callback,
    {
      a: @options[:account_id],
      env: @options[:sdk_key]
    }
  )
end

#fetch_settings(force = false) ⇒ Hash

Fetch the settings from the server

Parameters:

  • force (Boolean) (defaults to: false)

    Whether to force the fetch of settings

Returns:

  • (Hash)

    The settings



114
115
116
117
118
119
120
121
122
# File 'lib/wingify/wingify_builder.rb', line 114

def fetch_settings(force = false)
  return @settings if !force && @settings
  
  @is_settings_fetch_in_progress = true
  settings = SettingsService.new(@options).get_settings()
  @is_settings_fetch_in_progress = false
  @settings = settings unless force
  settings
end

#get_settings(force = false) ⇒ Hash

Get the settings from the server

Parameters:

  • force (Boolean) (defaults to: false)

    Whether to force the fetch of settings

Returns:

  • (Hash)

    The settings



127
128
129
130
131
132
133
134
135
136
# File 'lib/wingify/wingify_builder.rb', line 127

def get_settings(force = false)
  return @settings if !force && @settings
  
  begin
    fetch_settings(force)
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "ERROR_FETCHING_SETTINGS", { err: e.message, an: ApiEnum::INIT})
    {}
  end
end

#init_batchObject

Initializes the batch event processing system Validates batch event settings and configures the BatchEventsQueue Sets up event dispatcher and flushes any existing events

Raises:

  • (StandardError)

    If batch event configuration is invalid



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/wingify/wingify_builder.rb', line 50

def init_batch
  # if gateway service is configured, then do not initialize batch event queue
  if SettingsService.instance.is_gateway_service_provided
    LoggerService.log(LogLevelEnum::INFO, "GATEWAY_AND_BATCH_EVENTS_CONFIG_MISMATCH")
    return self
  end
  begin
    if @options.key?(:batch_event_data)
      if @options[:batch_event_data].is_a?(Hash)
        # Validate batch event parameters
        events_per_request = @options[:batch_event_data][:events_per_request]
        request_time_interval = @options[:batch_event_data][:request_time_interval]
        
        if (!events_per_request.is_a?(Numeric) || events_per_request <= 0) &&
           (!request_time_interval.is_a?(Numeric) || request_time_interval <= 0)
          LoggerService.log(LogLevelEnum::ERROR, "INVALID_BATCH_EVENTS_CONFIG", { an: ApiEnum::INIT})
        end
        
        BatchEventsQueue.configure(
          @options[:batch_event_data].merge(
            {
              account_id: @options[:account_id],
              dispatcher: method(:dispatcher)
            }
          )
        )
        @batch_event_data = @options[:batch_event_data]
      else
        LoggerService.log(LogLevelEnum::ERROR, "INVALID_BATCH_EVENTS_CONFIG", { an: ApiEnum::INIT})
      end
    end
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "FAILED_TO_INITIALIZE_SERVICE", { service: 'Batch Event Queue', err: e.message, an: ApiEnum::INIT})
  end
end

#init_pollingVWOBuilder

Initialize the polling

Returns:



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/wingify/wingify_builder.rb', line 172

def init_polling
  poll_interval = @options[:poll_interval]
  
  if poll_interval && poll_interval.is_a?(Numeric) && poll_interval >= 1000
    # this is to check if the poll_interval passed in options is valid
    @is_valid_poll_interval_passed_from_init = true
    check_and_poll
  elsif poll_interval
    # only log error if poll_interval is present in options
    LoggerService.log(LogLevelEnum::ERROR, "INVALID_POLLING_CONFIGURATION", {
      key: 'poll_interval',
      correctType: 'number >= 1000',
      an: ApiEnum::INIT
    })
  end
  self
end

#init_usage_statsVWOBuilder

Initialize the usage stats

Returns:



255
256
257
258
259
260
# File 'lib/wingify/wingify_builder.rb', line 255

def init_usage_stats
  return self if @options[:is_usage_stats_disabled]

  UsageStatsUtil.set_usage_stats(@options)
  self
end

#set_loggerVWOBuilder

Set the logger

Returns:



160
161
162
163
164
165
166
167
168
# File 'lib/wingify/wingify_builder.rb', line 160

def set_logger
  begin
    @log_manager = LoggerService.new(@options[:logger] || {})
    LoggerService.log(LogLevelEnum::DEBUG, "SERVICE_INITIALIZED", {service: "Logger"})
  rescue => e
    puts "Got error while setting logger: #{e.message}"
  end
  self
end

#set_network_managerVWOBuilder

Set the network manager

Returns:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/wingify/wingify_builder.rb', line 88

def set_network_manager
  begin
    network_instance = NetworkManager.instance(@options[:threading] || {})
    retry_config = @options[:retry_config]
    client = (@options[:network] && @options[:network][:client]) ? @options[:network][:client] : nil
    network_instance.attach_client(client, retry_config)
    LoggerService.log(LogLevelEnum::DEBUG, "SERVICE_INITIALIZED", {service: "Network Layer"})
    network_instance.get_config.set_development_mode(@options[:is_development_mode]) if @options[:is_development_mode]
    self
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "FAILED_TO_INITIALIZE_SERVICE", { service: 'Network Manager', err: e.message, an: ApiEnum::INIT})
    self
  end
end

#set_segmentationVWOBuilder

Set the segmentation manager

Returns:



105
106
107
108
109
# File 'lib/wingify/wingify_builder.rb', line 105

def set_segmentation
  SegmentationManager.instance.attach_evaluator(@options[:segmentation]) if @options[:segmentation]
  LoggerService.log(LogLevelEnum::DEBUG, "SERVICE_INITIALIZED", {service: "Segmentation Evaluator"})
  self
end

#set_settings_serviceVWOBuilder

Set the settings service

Returns:



153
154
155
156
# File 'lib/wingify/wingify_builder.rb', line 153

def set_settings_service
  @settings_service = SettingsService.new(@options)
  self
end

#set_storageVWOBuilder

Set the storage

Returns:



140
141
142
143
144
145
146
147
148
149
# File 'lib/wingify/wingify_builder.rb', line 140

def set_storage
  if @options[:storage]
    @storage = Storage.instance.attach_connector(@options[:storage])
    Storage.instance.is_storage_enabled = true
  else
    @storage = nil
  end
  LoggerService.log(LogLevelEnum::DEBUG, "SERVICE_INITIALIZED", {service: "Storage"})
  self
end

#update_poll_interval_and_check_and_poll(settings, should_check_and_poll = true) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/wingify/wingify_builder.rb', line 200

def update_poll_interval_and_check_and_poll(settings, should_check_and_poll = true)
  # only update the poll_interval if it poll_interval is not valid or not present in options
  if !@is_valid_poll_interval_passed_from_init
    @options[:poll_interval] = settings["pollInterval"] || Constants::POLLING_INTERVAL
    LoggerService.log(LogLevelEnum::DEBUG, "USING_POLL_INTERVAL_FROM_SETTINGS", {
      source: settings["pollInterval"] ? 'settings' : 'default',
      pollInterval: @options[:poll_interval]
    })
  end
  # should_check_and_poll will be true only when we are updating the poll_interval first time from self.build method
  # if we are updating the poll_interval already running polling, we don't need to check and poll again
  if should_check_and_poll && !@is_valid_poll_interval_passed_from_init
    check_and_poll
  end
end