-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
@user_attributes = {} if @user_attributes.nil? | ||
end | ||
|
||
def set_attribute(attribute_key, attribute_value) | ||
@user_attributes[attribute_key] = attribute_value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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?