vwo-sdk 1.36.0 → 1.37.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/vwo/enums.rb CHANGED
@@ -12,8 +12,6 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- # rubocop:disable Metrics/LineLength
16
-
17
15
  require 'logger'
18
16
 
19
17
  class VWO
@@ -64,23 +62,23 @@ class VWO
64
62
  VWO_PATH = 'vwo'
65
63
  UTIL_PATH = 'vwo/utils'
66
64
 
67
- VWO = VWO_PATH + '/vwo'
68
- Bucketer = VWO_PATH + '/core/bucketer'
69
- VariationDecider = VWO_PATH + '/core/variation_decider'
70
- EventDispatcher = VWO_PATH + '/services/event_dispatcher'
71
- SegmentEvaluator = VWO_PATH + '/services/segment_evaluator'
72
- Logger = VWO_PATH + '/logger'
73
- SettingsFileProcessor = VWO_PATH + '/services/settings_file_processor'
74
- BatchEventsQueue = VWO_PATH + '/services/batch_events_queue'
75
- BatchEventsDispatcher = VWO_PATH + '/services/batch_events_dispatcher'
65
+ VWO = "#{VWO_PATH}/vwo"
66
+ BUCKETER = "#{VWO_PATH}/core/bucketer"
67
+ VARIATION_DECIDER = "#{VWO_PATH}/core/variation_decider"
68
+ EVENT_DISPATCHER = "#{VWO_PATH}/services/event_dispatcher"
69
+ SEGMENT_EVALUATOR = "#{VWO_PATH}/services/segment_evaluator"
70
+ LOGGER = "#{VWO_PATH}/logger"
71
+ SETTINGS_FILE_PROCESSOR = "#{VWO_PATH}/services/settings_file_processor"
72
+ BATCH_EVENTS_QUEUE = "#{VWO_PATH}/services/batch_events_queue"
73
+ BATCH_EVENTS_DISPATCHER = "#{VWO_PATH}/services/batch_events_dispatcher"
76
74
 
77
- CampaignUtil = UTIL_PATH + '/campaign'
78
- FunctionUtil = UTIL_PATH + '/function'
79
- FeatureUtil = UTIL_PATH + '/feature'
80
- ImpressionUtil = UTIL_PATH + '/impression'
81
- UuidUtil = UTIL_PATH + '/uuid'
82
- ValidateUtil = UTIL_PATH + '/validations'
83
- CustomDimensionsUtil = UTIL_PATH + '/custom_dimensions_util'
75
+ CAMPAIGN_UTIL = "#{UTIL_PATH}/campaign"
76
+ FUNCTION_UTIL = "#{UTIL_PATH}/function"
77
+ FEATURE_UTIL = "#{UTIL_PATH}/feature"
78
+ IMPRESSION_UTIL = "#{UTIL_PATH}/impression"
79
+ UUID_UTIL = "#{UTIL_PATH}/uuid"
80
+ VALIDATE_UTIL = "#{UTIL_PATH}/validations"
81
+ CUSTOM_DIMENSTIONS_UTIL = "#{UTIL_PATH}/custom_dimensions_util"
84
82
  end
85
83
 
86
84
  module LogLevelEnum
@@ -91,4 +89,3 @@ class VWO
91
89
  end
92
90
  end
93
91
  end
94
- # rubocop:enable Metrics/LineLength
data/lib/vwo/logger.rb CHANGED
@@ -16,7 +16,6 @@ require 'logger'
16
16
 
17
17
  class VWO
18
18
  class Logger
19
-
20
19
  DEBUG = ::Logger::DEBUG
21
20
  INFO = ::Logger::INFO
22
21
  ERROR = ::Logger::ERROR
@@ -30,12 +29,12 @@ class VWO
30
29
  end
31
30
 
32
31
  def initialize(logger_instance)
33
- @@logger_instance = logger_instance || ::Logger.new(STDOUT)
32
+ @@logger_instance = logger_instance || ::Logger.new($stdout)
34
33
  end
35
34
 
36
35
  # Override this method to handle logs in a custom manner
37
36
  def log(level, message)
38
- @@logger_instance.log(level, message)
37
+ @@logger_instance.log(level, message)
39
38
  end
40
39
 
41
40
  def instance
@@ -61,12 +61,12 @@ class VWO
61
61
  weight: {
62
62
  type: %w[number string]
63
63
  },
64
- variables: {
65
- type: 'array',
66
- items: {
67
- '$ref' => '#/definitions/variables_schema'
68
- }
69
- }
64
+ variables: {
65
+ type: 'array',
66
+ items: {
67
+ '$ref' => '#/definitions/variables_schema'
68
+ }
69
+ }
70
70
  },
71
71
  required: %w[id name weight]
72
72
  },
@@ -42,25 +42,26 @@ class VWO
42
42
  url = CONSTANTS::HTTPS_PROTOCOL + get_url(CONSTANTS::ENDPOINTS::BATCH_EVENTS)
43
43
  account_id = query_params[:a]
44
44
  resp = VWO::Utils::Request.post(url, query_params, impression)
45
- if resp.code == '200'
45
+ case resp.code
46
+ when '200'
46
47
  @logger.log(
47
48
  LogLevelEnum::INFO,
48
49
  'IMPRESSION_BATCH_SUCCESS',
49
50
  {
50
51
  '{file}' => FILE,
51
52
  '{endPoint}' => url,
52
- '{accountId}' => account_id,
53
+ '{accountId}' => account_id
53
54
  }
54
55
  )
55
56
  message = nil
56
- elsif resp.code == '413'
57
+ when '413'
57
58
  @logger.log(
58
59
  LogLevelEnum::DEBUG,
59
60
  'CONFIG_BATCH_EVENT_LIMIT_EXCEEDED',
60
61
  {
61
- '{file}' => FileNameEnum::BatchEventsDispatcher,
62
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
62
63
  '{endPoint}' => url,
63
- '{eventsPerRequest}' => impression.length(),
64
+ '{eventsPerRequest}' => impression.length,
64
65
  '{accountId}' => impression[:a]
65
66
  }
66
67
  )
@@ -69,7 +70,7 @@ class VWO
69
70
  LogLevelEnum::ERROR,
70
71
  'IMPRESSION_FAILED',
71
72
  {
72
- '{file}' => FileNameEnum::BatchEventsDispatcher,
73
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
73
74
  '{err}' => resp.message,
74
75
  '{endPoint}' => url
75
76
  }
@@ -79,43 +80,40 @@ class VWO
79
80
  @logger.log(
80
81
  LogLevelEnum::INFO,
81
82
  'IMPRESSION_BATCH_FAILED',
82
- {'{file}' => FileNameEnum::BatchEventsDispatcher}
83
+ { '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER }
83
84
  )
84
85
 
85
86
  @logger.log(
86
87
  LogLevelEnum::ERROR,
87
88
  'IMPRESSION_FAILED',
88
89
  {
89
- '{file}' => FileNameEnum::BatchEventsDispatcher,
90
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
90
91
  '{err}' => resp.message,
91
92
  '{endPoint}' => url
92
93
  }
93
94
  )
94
95
  message = resp.message
95
96
  end
96
- if callback
97
- callback.call(message, impression)
98
- end
97
+ callback&.call(message, impression)
99
98
  true
100
99
  rescue StandardError => e
101
100
  @logger.log(
102
101
  LogLevelEnum::DEBUG,
103
102
  'IMPRESSION_BATCH_FAILED',
104
- {'{file}' => FileNameEnum::BatchEventsDispatcher}
103
+ { '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER }
105
104
  )
106
105
 
107
106
  @logger.log(
108
- LogLevelEnum::ERROR,
109
- 'IMPRESSION_FAILED',
110
- {
111
- '{file}' => FileNameEnum::BatchEventsDispatcher,
112
- '{err}' => e.message,
113
- '{endPoint}' => url
114
- }
115
- )
107
+ LogLevelEnum::ERROR,
108
+ 'IMPRESSION_FAILED',
109
+ {
110
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
111
+ '{err}' => e.message,
112
+ '{endPoint}' => url
113
+ }
114
+ )
116
115
  false
117
116
  end
118
-
119
117
  end
120
118
  end
121
119
  end
@@ -36,9 +36,9 @@ class VWO
36
36
  LogLevelEnum::INFO,
37
37
  'EVENT_BATCH_DEFAULTS',
38
38
  {
39
- '{file}' => FileNameEnum::BatchEventsQueue,
39
+ '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
40
40
  '{parameter}' => 'request_time_interval',
41
- '{defaultValue}' => @request_time_interval.to_s + ' ms'
41
+ '{defaultValue}' => "#{@request_time_interval} ms"
42
42
  }
43
43
  )
44
44
  end
@@ -51,7 +51,7 @@ class VWO
51
51
  LogLevelEnum::INFO,
52
52
  'EVENT_BATCH_DEFAULTS',
53
53
  {
54
- '{file}' => FileNameEnum::BatchEventsQueue,
54
+ '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
55
55
  '{parameter}' => 'events_per_request',
56
56
  '{defaultValue}' => @events_per_request.to_s
57
57
  }
@@ -59,9 +59,7 @@ class VWO
59
59
  end
60
60
 
61
61
  @flush_callback = nil
62
- if batch_config.key?(:flushCallback) && batch_config[:flushCallback].is_a?(Method)
63
- @flush_callback = batch_config[:flushCallback]
64
- end
62
+ @flush_callback = batch_config[:flushCallback] if batch_config.key?(:flushCallback) && batch_config[:flushCallback].is_a?(Method)
65
63
 
66
64
  @dispatcher = batch_config[:dispatcher]
67
65
  end
@@ -72,35 +70,34 @@ class VWO
72
70
 
73
71
  def enqueue(event)
74
72
  return true if @is_development_mode
73
+
75
74
  @queue.push(event)
76
75
  update_queue_metadata(event)
77
76
  unless @timer
78
77
  create_new_batch_timer
79
- @thread = Thread.new{flush_when_request_times_up}
80
- end
81
- if @events_per_request === @queue.length()
82
- flush
83
- kill_old_thread
78
+ @thread = Thread.new { flush_when_request_times_up }
84
79
  end
80
+ return unless @events_per_request == @queue.length
81
+
82
+ flush
83
+ kill_old_thread
85
84
  end
86
85
 
87
86
  def flush_when_request_times_up
88
- while @timer > Time.now
89
- sleep(1)
90
- end
87
+ sleep(1) while @timer > Time.now
91
88
  flush
92
89
  kill_old_thread
93
90
  end
94
91
 
95
92
  def flush(manual = false)
96
- if @queue.length() > 0
93
+ if @queue.length > 0
97
94
  @logger.log(
98
95
  LogLevelEnum::DEBUG,
99
96
  'EVENT_BATCH_BEFORE_FLUSHING',
100
97
  {
101
- '{file}' => FileNameEnum::BatchEventsQueue,
98
+ '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
102
99
  '{manually}' => manual ? 'manually' : '',
103
- '{length}' => @queue.length(),
100
+ '{length}' => @queue.length,
104
101
  '{timer}' => manual ? 'Timer will be cleared and registered again,' : '',
105
102
  '{accountId}' => @batch_config[:account_id]
106
103
  }
@@ -112,9 +109,9 @@ class VWO
112
109
  LogLevelEnum::INFO,
113
110
  'EVENT_BATCH_After_FLUSHING',
114
111
  {
115
- '{file}' => FileNameEnum::BatchEventsQueue,
112
+ '{file}' => FileNameEnum::BATCH_EVENTS_QUEUE,
116
113
  '{manually}' => manual ? 'manually,' : '',
117
- '{length}' => @queue.length()
114
+ '{length}' => @queue.length
118
115
  }
119
116
  )
120
117
  @queue_metadata = {}
@@ -123,16 +120,12 @@ class VWO
123
120
  @logger.log(
124
121
  LogLevelEnum::INFO,
125
122
  'Batch queue is empty. Nothing to flush.',
126
- {'{file}' => FILE}
123
+ { '{file}' => FILE }
127
124
  )
128
125
  end
129
126
 
130
127
  clear_request_timer
131
- unless manual
132
- if @thread
133
- @old_thread = @thread
134
- end
135
- end
128
+ @old_thread = @thread if !manual && @thread
136
129
  true
137
130
  end
138
131
 
@@ -141,32 +134,23 @@ class VWO
141
134
  end
142
135
 
143
136
  def kill_thread
144
- if @thread
145
- @thread.kill
146
- end
137
+ @thread&.kill
147
138
  end
148
139
 
149
140
  def kill_old_thread
150
- if @old_thread
151
- @old_thread.kill
152
- end
141
+ @old_thread&.kill
153
142
  end
154
143
 
155
144
  def update_queue_metadata(event)
156
- if event[:eT] == 1
157
- unless @queue_metadata.key?(VWO::EVENTS::TRACK_USER)
158
- @queue_metadata[VWO::EVENTS::TRACK_USER] = 0
159
- end
145
+ case event[:eT]
146
+ when 1
147
+ @queue_metadata[VWO::EVENTS::TRACK_USER] = 0 unless @queue_metadata.key?(VWO::EVENTS::TRACK_USER)
160
148
  @queue_metadata[VWO::EVENTS::TRACK_USER] = @queue_metadata[VWO::EVENTS::TRACK_USER] + 1
161
- elsif event[:eT] == 2
162
- unless @queue_metadata.key?(VWO::EVENTS::TRACK_GOAL)
163
- @queue_metadata[VWO::EVENTS::TRACK_GOAL] = 0
164
- end
149
+ when 2
150
+ @queue_metadata[VWO::EVENTS::TRACK_GOAL] = 0 unless @queue_metadata.key?(VWO::EVENTS::TRACK_GOAL)
165
151
  @queue_metadata[VWO::EVENTS::TRACK_GOAL] = @queue_metadata[VWO::EVENTS::TRACK_GOAL] + 1
166
- elsif event[:eT] == 3
167
- unless @queue_metadata.key?(VWO::EVENTS::PUSH)
168
- @queue_metadata[VWO::EVENTS::PUSH] = 0
169
- end
152
+ when 3
153
+ @queue_metadata[VWO::EVENTS::PUSH] = 0 unless @queue_metadata.key?(VWO::EVENTS::PUSH)
170
154
  @queue_metadata[VWO::EVENTS::PUSH] = @queue_metadata[VWO::EVENTS::PUSH] + 1
171
155
  end
172
156
  end
@@ -43,7 +43,7 @@ class VWO
43
43
  # the request to be dispatched to the VWO server
44
44
  # @return[Boolean]
45
45
  #
46
- def dispatch(impression)
46
+ def dispatch(impression, main_keys, end_point)
47
47
  return true if @is_development_mode
48
48
 
49
49
  modified_event = impression.reject do |key, _value|
@@ -52,13 +52,23 @@ class VWO
52
52
 
53
53
  resp = VWO::Utils::Request.get(impression['url'], modified_event)
54
54
  if resp.code == '200'
55
+ @logger.log(
56
+ LogLevelEnum::INFO,
57
+ 'IMPRESSION_SUCCESS',
58
+ {
59
+ '{file}' => FILE,
60
+ '{endPoint}' => end_point,
61
+ '{accountId}' => impression['account_id'] || impression[:account_id],
62
+ '{mainKeys}' => JSON.generate(main_keys)
63
+ }
64
+ )
55
65
  true
56
66
  else
57
67
  @logger.log(
58
68
  LogLevelEnum::ERROR,
59
69
  'IMPRESSION_FAILED',
60
70
  {
61
- '{file}' => FileNameEnum::BatchEventsDispatcher,
71
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
62
72
  '{err}' => resp.message,
63
73
  '{endPoint}' => impression['url']
64
74
  }
@@ -67,14 +77,14 @@ class VWO
67
77
  end
68
78
  rescue StandardError => e
69
79
  @logger.log(
70
- LogLevelEnum::ERROR,
71
- 'IMPRESSION_FAILED',
72
- {
73
- '{file}' => FileNameEnum::BatchEventsDispatcher,
74
- '{err}' => e.message,
75
- '{endPoint}' => impression['url']
76
- }
77
- )
80
+ LogLevelEnum::ERROR,
81
+ 'IMPRESSION_FAILED',
82
+ {
83
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
84
+ '{err}' => e.message,
85
+ '{endPoint}' => impression['url']
86
+ }
87
+ )
78
88
  false
79
89
  end
80
90
 
@@ -88,8 +98,8 @@ class VWO
88
98
  LogLevelEnum::INFO,
89
99
  'IMPRESSION_SUCCESS_FOR_EVENT_ARCH',
90
100
  {
91
- '{file}' => FileNameEnum::BatchEventsDispatcher,
92
- '{event}' => 'visitor property:' + JSON.generate(post_data[:d][:visitor][:props]),
101
+ '{file}' => FileNameEnum::BATCH_EVENTS_DISPATCHER,
102
+ '{event}' => "visitor property:#{JSON.generate(post_data[:d][:visitor][:props])}",
93
103
  '{endPoint}' => url,
94
104
  '{accountId}' => params[:a]
95
105
  }
@@ -100,7 +110,7 @@ class VWO
100
110
  LogLevelEnum::ERROR,
101
111
  'IMPRESSION_FAILED',
102
112
  {
103
- '{file}' => FileNameEnum::EventDispatcher,
113
+ '{file}' => FileNameEnum::EVENT_DISPATCHER,
104
114
  '{err}' => resp.message,
105
115
  '{endPoint}' => url
106
116
  }
@@ -109,14 +119,14 @@ class VWO
109
119
  end
110
120
  rescue StandardError => e
111
121
  @logger.log(
112
- LogLevelEnum::ERROR,
113
- 'IMPRESSION_FAILED',
114
- {
115
- '{file}' => FileNameEnum::EventDispatcher,
116
- '{err}' => e.message,
117
- '{endPoint}' => url
118
- }
119
- )
122
+ LogLevelEnum::ERROR,
123
+ 'IMPRESSION_FAILED',
124
+ {
125
+ '{file}' => FileNameEnum::EVENT_DISPATCHER,
126
+ '{err}' => e.message,
127
+ '{endPoint}' => url
128
+ }
129
+ )
120
130
  false
121
131
  end
122
132
  end
@@ -15,22 +15,18 @@
15
15
  class VWO
16
16
  module Services
17
17
  class HooksManager
18
- # Hooks Manager is responsible for triggering callbacks useful to the end-user based on certain lifecycle events.
19
- # Possible use with integrations when the user intends to send an event when a visitor is part of the experiment.
18
+ # Hooks Manager is responsible for triggering callbacks useful to the end-user based on certain lifecycle events.
19
+ # Possible use with integrations when the user intends to send an event when a visitor is part of the experiment.
20
20
  def initialize(config)
21
21
  @logger = VWO::Logger.get_instance
22
- if config.key?(:integrations) && config[:integrations].key?(:callback) && config[:integrations][:callback].is_a?(Method)
23
- @callback = config[:integrations][:callback]
24
- end
22
+ @callback = config[:integrations][:callback] if config.key?(:integrations) && config[:integrations].key?(:callback) && config[:integrations][:callback].is_a?(Method)
25
23
  end
26
24
 
27
- # Executes the callback
28
- # @param[Hash] properties Properties from the callback
29
- def execute(properties)
30
- if @callback
31
- @callback.call(properties)
32
- end
33
- end
25
+ # Executes the callback
26
+ # @param[Hash] properties Properties from the callback
27
+ def execute(properties)
28
+ @callback&.call(properties)
29
+ end
34
30
  end
35
31
  end
36
32
  end
@@ -43,15 +43,16 @@ class VWO
43
43
  #
44
44
  def evaluate_util(dsl, custom_variables)
45
45
  operator, sub_dsl = get_key_value(dsl)
46
- if operator == OperatorTypes::NOT
46
+ case operator
47
+ when OperatorTypes::NOT
47
48
  !evaluate_util(sub_dsl, custom_variables)
48
- elsif operator == OperatorTypes::AND
49
+ when OperatorTypes::AND
49
50
  sub_dsl.all? { |y| evaluate_util(y, custom_variables) }
50
- elsif operator == OperatorTypes::OR
51
+ when OperatorTypes::OR
51
52
  sub_dsl.any? { |y| evaluate_util(y, custom_variables) }
52
- elsif operator == OperandTypes::CUSTOM_VARIABLE
53
+ when OperandTypes::CUSTOM_VARIABLE
53
54
  @operand_evaluator.evaluate_custom_variable?(sub_dsl, custom_variables)
54
- elsif operator == OperandTypes::USER
55
+ when OperandTypes::USER
55
56
  @operand_evaluator.evaluate_user?(sub_dsl, custom_variables)
56
57
  end
57
58
  end
@@ -75,7 +76,7 @@ class VWO
75
76
  LogLevelEnum::ERROR,
76
77
  'SEGMENTATION_ERROR',
77
78
  {
78
- '{file}' => FileNameEnum::SegmentEvaluator,
79
+ '{file}' => FileNameEnum::SEGMENT_EVALUATOR,
79
80
  '{userId}' => user_id,
80
81
  '{campaignKey}' => campaign_key,
81
82
  '{variation}' => '',
@@ -47,11 +47,11 @@ class VWO
47
47
  return '{}'
48
48
  end
49
49
 
50
- if is_via_webhook
51
- path = ::VWO::CONSTANTS::ENDPOINTS::WEBHOOK_SETTINGS_URL
52
- else
53
- path = ::VWO::CONSTANTS::ENDPOINTS::SETTINGS_URL
54
- end
50
+ path = if is_via_webhook
51
+ ::VWO::CONSTANTS::ENDPOINTS::WEBHOOK_SETTINGS_URL
52
+ else
53
+ ::VWO::CONSTANTS::ENDPOINTS::SETTINGS_URL
54
+ end
55
55
  vwo_server_url = "#{PROTOCOL}://#{HOSTNAME}#{path}"
56
56
 
57
57
  settings_file_response = ::VWO::Utils::Request.get(vwo_server_url, params)
@@ -41,7 +41,7 @@ class VWO
41
41
  LogLevelEnum::DEBUG,
42
42
  'SETTINGS_FILE_PROCESSED',
43
43
  {
44
- '{file}' => FileNameEnum::SettingsFileProcessor,
44
+ '{file}' => FileNameEnum::SETTINGS_FILE_PROCESSOR,
45
45
  '{accountId}' => @settings_file['accountId']
46
46
  }
47
47
  )
@@ -16,13 +16,14 @@ class VWO
16
16
  module Services
17
17
  class UsageStats
18
18
  attr_reader :usage_stats
19
+
19
20
  # Initialize the UsageStats
20
21
  def initialize(stats, is_development_mode = false)
21
22
  @usage_stats = {}
22
- unless is_development_mode
23
- @usage_stats = stats
24
- @usage_stats[:_l] = 1 if @usage_stats.length > 0
25
- end
23
+ return if is_development_mode
24
+
25
+ @usage_stats = stats
26
+ @usage_stats[:_l] = 1 if @usage_stats.length > 0
26
27
  end
27
28
  end
28
29
  end