Class: WingifyClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, options) ⇒ WingifyClient

Returns a new instance of WingifyClient.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/wingify/wingify_client.rb', line 33

def initialize(settings, options)
  @options = options
  @settings = settings
  @original_settings = settings.dup
  
  begin
    set_settings_and_add_campaigns_to_rules(settings, self)
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "ERROR_ADDING_CAMPAIGNS_TO_RULES", { err: e.message, an: ApiEnum::INIT})
  end
  LoggerService.log(LogLevelEnum::INFO, "CLIENT_INITIALIZED")
  self
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



31
32
33
# File 'lib/wingify/wingify_client.rb', line 31

def options
  @options
end

#original_settingsObject

Returns the value of attribute original_settings.



30
31
32
# File 'lib/wingify/wingify_client.rb', line 30

def original_settings
  @original_settings
end

#settingsObject

Returns the value of attribute settings.



30
31
32
# File 'lib/wingify/wingify_client.rb', line 30

def settings
  @settings
end

Instance Method Details

#flush_eventsvoid

This method returns an undefined value.

Flushes the batch events queue



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/wingify/wingify_client.rb', line 213

def flush_events
  api_name = 'flush_events'
  begin
    LoggerService.log(LogLevelEnum::DEBUG, "API_CALLED", {apiName: api_name})
    if BatchEventsQueue.instance.nil?
      raise StandardError, "Batch events queue is not initialized"
    end
    # flush the batch events queue
    @response = BatchEventsQueue.instance.flush(true)
    @response
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "EXECUTION_FAILED", {apiName: api_name, err: e.message, an: ApiEnum::FLUSH_EVENTS})
  end
end

#get_flag(feature_key, context) ⇒ GetFlagResponse

Get the flag for a given feature key and context

Parameters:

  • feature_key (String)

    The key of the feature to get the flag for

  • context (Hash)

    The context of the user

Returns:



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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/wingify/wingify_client.rb', line 51

def get_flag(feature_key, context)
  api_name = 'get_flag'
  uuid = nil
  session_id = nil

  # check if sessionId is present in context and should be non null and non empty
  if context.is_a?(Hash) && context.key?(:sessionId) && !context[:sessionId].nil? && !context[:sessionId].to_s.empty?
    session_id = context[:sessionId].to_i
  else
    session_id = Time.now.to_i
  end

  begin
    hooks_service = HooksService.new(@options)
    LoggerService.log(LogLevelEnum::DEBUG, "API_CALLED", {apiName: api_name})
    unless context.is_a?(Hash)
      raise TypeError, 'Invalid context'
    end
    unless context[:id].is_a?(String) && !context[:id].empty?
      raise TypeError, 'Invalid context, id should be a non-empty string'
    end
    # make a copy of the context to avoid modifying the original context
    context_copy = context.dup
    uuid = UUIDUtil.get_uuid_from_context(@settings, context_copy, api_name)
    # add the uuid to the context copy
    context_copy[:uuid] = uuid

    # Validate bucketingSeed: must be a non-empty, non-whitespace-only string
    if context_copy.key?(:bucketingSeed)
      seed = context_copy[:bucketingSeed]
      if seed.nil? || seed.is_a?(Numeric) || seed.is_a?(Hash) || seed.is_a?(Array) || (seed.is_a?(String) && seed.strip.empty?)
        LoggerService.log(LogLevelEnum::ERROR, "INVALID_BUCKETING_SEED", { apiName: api_name, type: seed.class.name })
        context_copy.delete(:bucketingSeed)
      end
    end

    unless feature_key.is_a?(String) && !feature_key.empty?
      raise TypeError, 'feature_key should be a non-empty string'
    end
    unless SettingsService.instance.is_settings_valid
      raise TypeError, 'Invalid Settings'
    end
    
    context_model = ContextModel.new.model_from_dictionary(context_copy)
    FlagApi.new.get(feature_key, @settings, context_model, hooks_service)
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "EXECUTION_FAILED", {apiName: api_name, err: e.message, an: ApiEnum::GET_FLAG})
    GetFlagResponse.new(false, [], uuid, session_id)
  end
end

#set_attribute(attributes, context = nil) ⇒ Hash

Set attributes for a given context

Parameters:

  • attributes (Hash)

    The attributes to set

  • context (Hash) (defaults to: nil)

    The context of the user

Returns:

  • (Hash)

    The result of the attribute setting



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
# File 'lib/wingify/wingify_client.rb', line 143

def set_attribute(attributes, context = nil)
  api_name = 'set_attribute'
  
  begin
    LoggerService.log(LogLevelEnum::DEBUG, "API_CALLED", {apiName: api_name})
    unless context.is_a?(Hash)
      raise TypeError, 'Invalid context'
    end
    unless context[:id].is_a?(String) && !context[:id].empty?
      raise TypeError, 'Invalid context, id should be a non-empty string'
    end
    # make a copy of the context to avoid modifying the original context
    context_copy = context.dup
    context_copy[:uuid] = UUIDUtil.get_uuid_from_context(@settings, context_copy, api_name)

    unless attributes.is_a?(Hash) && !attributes.empty?
      raise TypeError, 'Attributes should be a hash with key-value pairs and non-empty'
    end
    unless SettingsService.instance.is_settings_valid
      raise TypeError, 'Invalid Settings'
    end
    
    context_model = ContextModel.new.model_from_dictionary(context_copy)
    SetAttributeApi.new.set_attribute(attributes, context_model)
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "EXECUTION_FAILED", {apiName: api_name, err: e.message, an: ApiEnum::SET_ATTRIBUTE})
  end
end

#track_event(event_name, context, event_properties = {}) ⇒ Hash

Track an event with given properties and context

Parameters:

  • event_name (String)

    The name of the event to track

  • context (Hash)

    The context of the user

  • event_properties (Hash) (defaults to: {})

    The properties of the event

Returns:

  • (Hash)

    The result of the event tracking



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
136
137
# File 'lib/wingify/wingify_client.rb', line 107

def track_event(event_name, context, event_properties = {})
  api_name = 'track_event'
  
  begin
    hooks_service = HooksService.new(@options)
    LoggerService.log(LogLevelEnum::DEBUG, "API_CALLED", {apiName: api_name})

    unless context.is_a?(Hash) && context[:id].is_a?(String) && !context[:id].empty?
      raise TypeError, 'Invalid context, id should be a non-empty string'
    end
    # make a copy of the context to avoid modifying the original context
    context_copy = context.dup
    context_copy[:uuid] = UUIDUtil.get_uuid_from_context(@settings, context_copy, api_name)

    unless event_name.is_a?(String) && !event_name.empty?
      raise TypeError, 'event_name should be a non-empty string'
    end
    unless event_properties.is_a?(Hash)
      raise TypeError, 'event_properties should be a hash'
    end
    unless SettingsService.instance.is_settings_valid
      raise TypeError, 'Invalid Settings'
    end
    
    context_model = ContextModel.new.model_from_dictionary(context_copy)
    TrackApi.new.track(@settings, event_name, context_model, event_properties, hooks_service)
  rescue StandardError => e
    LoggerService.log(LogLevelEnum::ERROR, "EXECUTION_FAILED", {apiName: api_name, err: e.message, an: ApiEnum::TRACK_EVENT})
    { event_name: false }
  end
end

#update_settings(settings = nil, is_via_webhook = true) ⇒ void

This method returns an undefined value.

Update the settings of the VWO client instance

Parameters:

  • settings (Hash) (defaults to: nil)

    The settings to update with (optional)

  • is_via_webhook (Boolean) (defaults to: true)

    Whether the update is via webhook (default: true)



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/wingify/wingify_client.rb', line 176

def update_settings(settings = nil, is_via_webhook = true)
  api_name = 'update_settings'
  
  begin
    LoggerService.log(LogLevelEnum::DEBUG, "API_CALLED", {apiName: api_name})
    
    # Fetch settings from server or use provided settings if not empty
    settings_to_update = if settings.nil? || settings.empty?
      SettingsService.instance.fetch_settings(is_via_webhook)
    else
      settings
    end

    # Validate settings schema
    unless SettingsSchema.new.is_settings_valid(settings_to_update)
      raise TypeError, "Got Invalid Settings, settings: #{settings_to_update}"
    end

    # Set the settings on the client instance
    set_settings_and_add_campaigns_to_rules(settings_to_update, self)
    LoggerService.log(LogLevelEnum::INFO, "SETTINGS_UPDATED", {apiName: api_name, isViaWebhook: is_via_webhook})
  rescue StandardError => e
    LoggerService.log(
      LogLevelEnum::ERROR,
      "UPDATING_CLIENT_INSTANCE_FAILED_WHEN_WEBHOOK_TRIGGERED",
      {
        apiName: api_name,
        isViaWebhook: is_via_webhook,
        err: e.message,
        an: ApiEnum::UPDATE_SETTINGS
      }, false
    )
  end
end