8000 feat(Decide): Add Optimizely User Context by oakbani · Pull Request #273 · optimizely/ruby-sdk · GitHub
[go: up one dir, main page]

Skip to content

feat(Decide): Add Optimizely User Context #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require_relative 'optimizely/logger'
require_relative 'optimizely/notification_center'
require_relative 'optimizely/optimizely_config'
require_relative 'optimizely/optimizely_user_context'

module Optimizely
class Project
Expand Down Expand Up @@ -107,6 +108,24 @@ def initialize(
end
end

def create_user_context(user_id, attributes = nil)
# We do not check for is_valid here as a user context can be created successfully
# even when the SDK is not fully configured.

# validate user_id
return nil unless Optimizely::Helpers::Validator.inputs_valid?(
{
user_id: user_id
}, @logger, Logger::ERROR
)

# validate attributes
return nil unless user_inputs_valid?(attributes)

user_context = OptimizelyUserContext.new(self, user_id, attributes)
user_context
end

# Buckets visitor and sends impression event to Optimizely.
#
# @param experiment_key - Experiment which needs to be activated.
Expand Down
51 changes: 51 additions & 0 deletions lib/optimizely/optimizely_user_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

#
# Copyright 2020, 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.
#

module Optimizely
class OptimizelyUserContext
# Representation of an Optimizely User Context using which APIs are to be called.

def initialize(optimizely_client, user_id, user_attributes)
@optimizely_client = optimizely_client
@user_id = user_id
@user_attributes = user_attributes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this create a copy of the passed user_attributes, so it's not reflected when the client copy is changed later?


@user_attributes = {} if @user_attributes.nil?
end

def set_attribute(attribute_key, attribute_value)
@user_attributes[attribute_key] = attribute_value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"set_attribute" can be called concurrently with get-attributes from the userContext. Don't we need to synchronization for attributes read/write?

end

def decide(key, options = nil)
# TODO: call decide API in Optimizely class.
end

def decide_for_keys(keys, options = nil)
# TODO: call decideForKeys in Optimizely class.
end

def decide_all(options = nil)
# TODO: call decideForAll in optimizely class.
end

def track_event(event_key, event_tags = nil)
@optimizely_client.track(event_key, @user_id, @user_attributes, event_tags)
end
end
end
71 changes: 71 additions & 0 deletions spec/optimizely_user_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

#
# Copyright 2020, 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 'spec_helper'
require 'optimizely'
require 'optimizely/optimizely_user_context'

describe 'Optimizely' do
let(:config_body) { OptimizelySpec::VALID_CONFIG_BODY }
let(:config_body_JSON) { OptimizelySpec::VALID_CONFIG_BODY_JSON }
let(:config_body_invalid_JSON) { OptimizelySpec::INVALID_CONFIG_BODY_JSON }
let(:error_handler) { Optimizely::RaiseErrorHandler.new }
let(:spy_logger) { spy('logger') }
let(:project_instance) { Optimizely::Project.new(config_body_JSON, nil, spy_logger, error_handler) }

describe '#initialize' do
it 'should set passed value as expected' do
user_id = 'test_user'
attributes = {' browser' => 'firefox'}
user_context_obj = Optimizely::OptimizelyUserContext.new(project_instance, user_id, attributes)

expect(user_context_obj.instance_variable_get(:@optimizely_client)). to eq(project_instance)
expect(user_context_obj.instance_variable_get(:@user_id)). to eq(user_id)
expect(user_context_obj.instance_variable_get(:@user_attributes)). to eq(attributes)
end

it 'should set user attributes to empty hash when passed nil' do
user_context_obj = Optimizely::OptimizelyUserContext.new(project_instance, 'test_user', nil)
expect(user_context_obj.instance_variable_get(:@user_attributes)). to eq({})
end
end

describe '#set_attribute' do
it 'should add attribute key and value is attributes hash' do
user_id = 'test_user'
attributes = {' browser' => 'firefox'}
user_context_obj = Optimizely::OptimizelyUserContext.new(project_instance, user_id, attributes)
user_context_obj.set_attribute('id', 49)

expected_attributes = attributes
expected_attributes['id'] = 49
expect(user_context_obj.instance_variable_get(:@user_attributes)). to eq(expected_attributes)
end

it 'should override attribute value if key already exists in hash' do
user_id = 'test_user'
attributes = {' browser' => 'firefox', 'color' => ' red'}
user_context_obj = Optimizely::OptimizelyUserContext.new(project_instance, user_id, attributes)
user_context_obj.set_attribute('browser', 'chrome')

expected_attributes = attributes
expected_attributes['browser'] = 'chrome'

expect(user_context_obj.instance_variable_get(:@user_attributes)). to eq(expected_attributes)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test - when the caller change its attribute copy after userContext is created, it should not be reflected to userContext?

end
end
30 changes: 30 additions & 0 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require 'optimizely/event/batch_event_processor'
require 'optimizely/exceptions'
require 'optimizely/helpers/validator'
require 'optimizely/optimizely_user_context'
require 'optimizely/version'

describe 'Optimizely' do
Expand Down Expand Up @@ -135,6 +136,35 @@ class InvalidErrorHandler; end
end
end

describe '#create_user_context' do
it 'should log and return nil when user ID is non string' do
expect(project_instance.create_user_context(nil)).to eq(nil)
expect(project_instance.create_user_context(5)).to eq(nil)
expect(project_instance.create_user_context(5.5)).to eq(nil)
expect(project_instance.create_user_context(true)).to eq(nil)
expect(project_instance.create_user_context({})).to eq(nil)
expect(project_instance.create_user_context([])).to eq(nil)
expect(spy_logger).to have_received(:log).with(Logger::ERROR, 'User ID is invalid').exactly(6).times
end

it 'should return nil when attributes are invalid' do
expect(Optimizely::Helpers::Validator).to receive(:attributes_valid?).once.with('invalid')
expect(error_handler).to receive(:handle_error).once.with(Optimizely::InvalidAttributeFormatError)
expect(project_instance.create_user_context(
'test_user',
'invalid'
)).to eq(nil)
expect(spy_logger).to have_received(:log).once.with(Logger::ERROR, 'Provided attributes are in an invalid format.')
end

it 'should return OptimizelyUserContext with valid user ID and attributes' do
expect(project_instance.create_user_context(
'test_user',
'browser' => 'chrome'
)).to be_instance_of(Optimizely::OptimizelyUserContext)
end
end

describe '#activate' do
before(:example) do
allow(Time).to receive(:now).and_return(time_now)
Expand Down
0