vwo-sdk 1.30.0 → 1.37.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,13 +15,15 @@
15
15
  require 'json'
16
16
  require 'json-schema'
17
17
  require_relative '../schemas/settings_file'
18
- require_relative '../logger'
19
18
  require_relative '../enums'
20
19
  require_relative '../constants'
20
+ require_relative './log_message'
21
21
 
22
22
  class VWO
23
23
  module Utils
24
24
  module Validations
25
+ include Enums
26
+ include CONSTANTS
25
27
  # Validates the settings_file
26
28
  # @param [Hash]: JSON object received from VWO server
27
29
  # must be JSON.
@@ -66,83 +68,156 @@ class VWO
66
68
  # Validates if the value passed batch_events has correct data type and values or not.
67
69
  #
68
70
  # Args: batch_events [Hash]: value to be tested
71
+ # api_name [String]: current api name
69
72
  #
70
73
  # @return: [Boolean]: True if all conditions are passed else False
71
- def is_valid_batch_event_settings(batch_events)
72
- logger = VWO::Logger.get_instance
74
+ def valid_batch_event_settings(batch_events, api_name)
73
75
  events_per_request = batch_events[:events_per_request]
74
76
  request_time_interval = batch_events[:request_time_interval]
75
77
 
76
78
  unless events_per_request || request_time_interval
77
- logger.log(
78
- VWO::LogLevelEnum::ERROR,
79
- format(
80
- VWO::LogMessageEnum::ErrorMessages::EVENT_BATCHING_INSUFFICIENT,
81
- file: VWO::FileNameEnum::ValidateUtil
82
- )
83
- )
79
+ invalid_config_log('batch_events', 'object', api_name)
84
80
  return false
85
81
  end
86
82
 
87
- if (request_time_interval && !valid_number?(request_time_interval))
88
- logger.log(
89
- VWO::LogLevelEnum::ERROR,
90
- format(
91
- VWO::LogMessageEnum::ErrorMessages::REQUEST_TIME_INTERVAL_INVALID,
92
- file: VWO::FileNameEnum::ValidateUtil
93
- )
94
- )
83
+ if request_time_interval && !valid_number?(request_time_interval)
84
+ invalid_config_log('batch_events', 'object', api_name)
95
85
  return false
96
86
  end
97
87
 
98
- if (events_per_request && !valid_number?(events_per_request))
99
- logger.log(
100
- VWO::LogLevelEnum::ERROR,
101
- format(
102
- VWO::LogMessageEnum::ErrorMessages::EVENTS_PER_REQUEST_INVALID,
103
- file: VWO::FileNameEnum::ValidateUtil
104
- )
105
- )
88
+ if events_per_request && !valid_number?(events_per_request)
89
+ invalid_config_log('batch_events', 'object', api_name)
106
90
  return false
107
91
  end
108
92
 
109
93
  if events_per_request && (events_per_request < VWO::MIN_EVENTS_PER_REQUEST || events_per_request > VWO::MAX_EVENTS_PER_REQUEST)
110
- logger.log(
111
- VWO::LogLevelEnum::ERROR,
112
- format(
113
- VWO::LogMessageEnum::ErrorMessages::EVENTS_PER_REQUEST_OUT_OF_BOUNDS,
114
- file: VWO::FileNameEnum::ValidateUtil,
115
- min_value: VWO::MIN_EVENTS_PER_REQUEST,
116
- max_value: VWO::MAX_EVENTS_PER_REQUEST
117
- )
118
- )
94
+ invalid_config_log('batch_events', 'object', api_name)
119
95
  return false
120
96
  end
121
97
 
122
98
  if request_time_interval && request_time_interval < VWO::MIN_REQUEST_TIME_INTERVAL
123
- logger.log(
124
- VWO::LogLevelEnum::ERROR,
125
- format(
126
- VWO::LogMessageEnum::ErrorMessages::REQUEST_TIME_INTERVAL_OUT_OF_BOUNDS,
127
- file: VWO::FileNameEnum::ValidateUtil,
128
- min_value: VWO::MIN_REQUEST_TIME_INTERVAL
129
- )
130
- )
99
+ invalid_config_log('batch_events', 'object', api_name)
131
100
  return false
132
101
  end
133
102
 
134
103
  if batch_events.key?(:flushCallback) && !batch_events[:flushCallback].is_a?(Method)
135
- logger.log(
136
- VWO::LogLevelEnum::ERROR,
137
- format(
138
- VWO::LogMessageEnum::ErrorMessages::FLUSH_CALLBACK_INVALID,
139
- file: VWO::FileNameEnum::ValidateUtil
140
- )
104
+ invalid_config_log('batch_events', 'object', api_name)
105
+ return false
106
+ end
107
+ true
108
+ end
109
+
110
+ def validate_sdk_config?(user_storage, is_development_mode, api_name)
111
+ if is_development_mode
112
+ if [true, false].include? is_development_mode
113
+ valid_config_log('isDevelopmentMode', 'boolean')
114
+ else
115
+ invalid_config_log('isDevelopmentMode', 'boolean', api_name)
116
+ return false
117
+ end
118
+ end
119
+
120
+ if user_storage
121
+ if user_storage.is_a?(UserStorage)
122
+ valid_config_log('UserStorageService', 'object')
123
+ else
124
+ invalid_config_log('UserStorageService', 'object', api_name)
125
+ return false
126
+ end
127
+ end
128
+ true
129
+ end
130
+
131
+ def valid_config_log(parameter, type)
132
+ Logger.log(
133
+ LogLevelEnum::INFO,
134
+ 'CONFIG_PARAMETER_USED',
135
+ {
136
+ '{file}' => VWO::FileNameEnum::VALIDATE_UTIL,
137
+ '{parameter}' => parameter,
138
+ '{type}' => type
139
+ }
140
+ )
141
+ end
142
+
143
+ def invalid_config_log(parameter, type, api_name)
144
+ Logger.log(
145
+ LogLevelEnum::ERROR,
146
+ 'CONFIG_PARAMETER_INVALID',
147
+ {
148
+ '{file}' => VWO::FileNameEnum::VALIDATE_UTIL,
149
+ '{parameter}' => parameter,
150
+ '{type}' => type,
151
+ '{api}' => api_name
152
+ }
153
+ )
154
+ end
155
+
156
+ def valid_goal?(goal, campaign, user_id, goal_identifier, revenue_value)
157
+ if goal.nil? || !goal['id']
158
+ Logger.log(
159
+ LogLevelEnum::ERROR,
160
+ 'TRACK_API_GOAL_NOT_FOUND',
161
+ {
162
+ '{file}' => FILE,
163
+ '{goalIdentifier}' => goal_identifier,
164
+ '{userId}' => user_id,
165
+ '{campaignKey}' => campaign['key']
166
+ }
167
+ )
168
+ return false
169
+ elsif goal['type'] == GoalTypes::REVENUE && !valid_value?(revenue_value)
170
+ Logger.log(
171
+ LogLevelEnum::ERROR,
172
+ 'TRACK_API_REVENUE_NOT_PASSED_FOR_REVENUE_GOAL',
173
+ {
174
+ '{file}' => FILE,
175
+ '{userId}' => user_id,
176
+ '{goalIdentifier}' => goal_identifier,
177
+ '{campaignKey}' => campaign['key']
178
+ }
141
179
  )
142
180
  return false
143
181
  end
144
182
  true
145
183
  end
146
184
  end
185
+
186
+ def valid_campaign_for_track_api?(user_id, campaign_key, campaign_type)
187
+ if campaign_type == CONSTANTS::CampaignTypes::FEATURE_ROLLOUT
188
+ Logger.log(
189
+ LogLevelEnum::ERROR,
190
+ 'API_NOT_APPLICABLE',
191
+ {
192
+ '{file}' => FILE,
193
+ '{api}' => ApiMethods::TRACK,
194
+ '{userId}' => user_id,
195
+ '{campaignKey}' => campaign_key,
196
+ '{campaignType}' => campaign_type
197
+ }
198
+ )
199
+ return false
200
+ end
201
+ true
202
+ end
203
+
204
+ def valid_track_api_params?(user_id, campaign_key, custom_variables, variation_targeting_variables, goal_type_to_track, goal_identifier)
205
+ unless (valid_string?(campaign_key) || campaign_key.is_a?(Array) || campaign_key.nil?) &&
206
+ valid_string?(user_id) && valid_string?(goal_identifier) &&
207
+ (custom_variables.nil? || valid_hash?(custom_variables)) &&
208
+ (variation_targeting_variables.nil? || valid_hash?(variation_targeting_variables)) && CONSTANTS::GOAL_TYPES.key?(goal_type_to_track)
209
+ # log invalid params
210
+ Logger.log(
211
+ LogLevelEnum::ERROR,
212
+ 'API_BAD_PARAMETERS',
213
+ {
214
+ '{file}' => FILE,
215
+ '{api}' => ApiMethods::TRACK
216
+ }
217
+ )
218
+ return false
219
+ end
220
+ true
221
+ end
147
222
  end
148
223
  end