Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/optimizely/helpers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ module Constants
ODP_LOGS = {
FETCH_SEGMENTS_FAILED: 'Audience segments fetch failed (%s).',
ODP_EVENT_FAILED: 'ODP event send failed (%s).',
ODP_NOT_ENABLED: 'ODP is not enabled.'
ODP_NOT_ENABLED: 'ODP is not enabled.',
ODP_NOT_INTEGRATED: 'ODP is not integrated.'
}.freeze

DECISION_NOTIFICATION_TYPES = {
Expand Down Expand Up @@ -424,6 +425,19 @@ module Constants
REQUEST_TIMEOUT: 10
}.freeze

ODP_CONFIG_STATE = {
UNDETERMINED: 'UNDETERMINED',
INTEGRATED: 'INTEGRATED',
NOT_INTEGRATED: 'NOT_INTEGRATED'
}.freeze

ODP_EVENT_MANAGER = {
DEFAULT_QUEUE_CAPACITY: 10_000,
DEFAULT_BATCH_SIZE: 10,
DEFAULT_FLUSH_INTERVAL: 1,
DEFAULT_RETRY_COUNT: 3
}.freeze

HTTP_HEADERS = {
'IF_MODIFIED_SINCE' => 'If-Modified-Since',
'LAST_MODIFIED' => 'Last-Modified'
Expand Down
5 changes: 5 additions & 0 deletions lib/optimizely/helpers/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ def finite_number?(value)

value.is_a?(Numeric) && value.to_f.finite? && value.abs <= Constants::FINITE_NUMBER_LIMIT
end

def odp_data_types_valid?(data)
valid_types = [String, Float, Integer, TrueClass, FalseClass, NilClass]
data.values.all? { |e| valid_types.member? e.class }
end
end
end
end
28 changes: 18 additions & 10 deletions lib/optimizely/odp/odp_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
#

require 'optimizely/logger'
require_relative '../helpers/constants'

module Optimizely
class OdpConfig
ODP_CONFIG_STATE = Helpers::Constants::ODP_CONFIG_STATE
# Contains configuration used for ODP integration.
#
# @param api_host - The host URL for the ODP audience segments API (optional).
Expand All @@ -30,6 +32,7 @@ def initialize(api_key = nil, api_host = nil, segments_to_check = [])
@api_host = api_host
@segments_to_check = segments_to_check
@mutex = Mutex.new
@odp_state = @api_host.nil? || @api_key.nil? ? ODP_CONFIG_STATE[:UNDETERMINED] : ODP_CONFIG_STATE[:INTEGRATED]
end

# Replaces the existing configuration
Expand All @@ -41,14 +44,19 @@ def initialize(api_key = nil, api_host = nil, segments_to_check = [])
# @return - True if the provided values were different than the existing values.

def update(api_key = nil, api_host = nil, segments_to_check = [])
updated = false
@mutex.synchronize do
break false if @api_key == api_key && @api_host == api_host && @segments_to_check == segments_to_check

@api_key = api_key
@api_host = api_host
@segments_to_check = segments_to_check
break true
@odp_state = api_host.nil? || api_key.nil? ? ODP_CONFIG_STATE[:NOT_INTEGRATED] : ODP_CONFIG_STATE[:INTEGRATED]

if @api_key != api_key || @api_host != api_host || @segments_to_check != segments_to_check
@api_key = api_key
@api_host = api_host
@segments_to_check = segments_to_check
updated = true
end
end

updated
end

# Returns the api host for odp connections
Expand Down Expand Up @@ -99,12 +107,12 @@ def segments_to_check=(segments_to_check)
@mutex.synchronize { @segments_to_check = segments_to_check.clone }
end

# Returns True if odp is integrated
# Returns the state of odp integration (UNDETERMINED, INTEGRATED, NOT_INTEGRATED)
#
# @return - bool
# @return - string

def odp_integrated?
@mutex.synchronize { !@api_key.nil? && !@api_host.nil? }
def odp_state
@mutex.synchronize { @odp_state }
end
end
end
55 changes: 55 additions & 0 deletions lib/optimizely/odp/odp_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

#
# Copyright 2022, Optimizely and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'json'

module Optimizely
class OdpEvent
# Representation of an odp event which can be sent to the Optimizely odp platform.
def initialize(type:, action:, identifiers:, data:)
@type = type
@action = action
@identifiers = identifiers
@data = add_common_event_data(data)
end

def add_common_event_data(custom_data)
data = {
idempotence_id: SecureRandom.uuid,
data_source_type: 'sdk',
data_source: 'ruby-sdk',
data_source_version: VERSION
}
data.update(custom_data)
data
end

def to_json(*_args)
{
type: @type,
action: @action,
identifiers: @identifiers,
data: @data
}.to_json
end

def ==(other)
to_json == other.to_json
end
end
end
Loading