From 53deab003b6bdf1824483b579b2cb1ff58db22e4 Mon Sep 17 00:00:00 2001 From: rashidsp Date: Fri, 19 Jul 2019 19:25:40 +0500 Subject: [PATCH 1/3] feat(epmodel): Event Processor datamodel --- .rubocop_todo.yml | 1 + .../event/entity/conversion_event.rb | 24 +++ lib/optimizely/event/entity/decision.rb | 37 ++++ lib/optimizely/event/entity/event_batch.rb | 88 +++++++++ lib/optimizely/event/entity/event_context.rb | 35 ++++ .../event/entity/impression_event.rb | 24 +++ lib/optimizely/event/entity/snapshot.rb | 36 ++++ lib/optimizely/event/entity/snapshot_event.rb | 54 ++++++ lib/optimizely/event/entity/user_event.rb | 22 +++ lib/optimizely/event/entity/visitor.rb | 35 ++++ .../event/entity/visitor_attribute.rb | 37 ++++ spec/event/event_entities_spec.rb | 170 ++++++++++++++++++ 12 files changed, 563 insertions(+) create mode 100644 lib/optimizely/event/entity/conversion_event.rb create mode 100644 lib/optimizely/event/entity/decision.rb create mode 100644 lib/optimizely/event/entity/event_batch.rb create mode 100644 lib/optimizely/event/entity/event_context.rb create mode 100644 lib/optimizely/event/entity/impression_event.rb create mode 100644 lib/optimizely/event/entity/snapshot.rb create mode 100644 lib/optimizely/event/entity/snapshot_event.rb create mode 100644 lib/optimizely/event/entity/user_event.rb create mode 100644 lib/optimizely/event/entity/visitor.rb create mode 100644 lib/optimizely/event/entity/visitor_attribute.rb create mode 100644 spec/event/event_entities_spec.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7b39a311..ebbce513 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -24,6 +24,7 @@ Metrics/ParameterLists: - 'lib/optimizely/config_manager/http_project_config_manager.rb' - 'lib/optimizely.rb' - 'lib/optimizely/optimizely_factory.rb' + - 'lib/optimizely/event/entity/snapshot_event.rb' Naming/AccessorMethodName: Exclude: diff --git a/lib/optimizely/event/entity/conversion_event.rb b/lib/optimizely/event/entity/conversion_event.rb new file mode 100644 index 00000000..8a39966b --- /dev/null +++ b/lib/optimizely/event/entity/conversion_event.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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_relative 'user_event' +module Optimizely + class ConversionEvent < UserEvent + attr_accessor :event, :event_tags + attr_reader :user_id, :user_attributes + end +end diff --git a/lib/optimizely/event/entity/decision.rb b/lib/optimizely/event/entity/decision.rb new file mode 100644 index 00000000..064b17ea --- /dev/null +++ b/lib/optimizely/event/entity/decision.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 Decision + attr_reader :campaign_id, :experiment_id, :variation_id + + def initialize(campaign_id, experiment_id, variation_id) + @campaign_id = campaign_id + @experiment_id = experiment_id + @variation_id = variation_id + end + + def as_json(_options = {}) + { + campaign_id: @campaign_id, + experiment_id: @experiment_id, + variation_id: @variation_id + } + end + end +end diff --git a/lib/optimizely/event/entity/event_batch.rb b/lib/optimizely/event/entity/event_batch.rb new file mode 100644 index 00000000..5711e663 --- /dev/null +++ b/lib/optimizely/event/entity/event_batch.rb @@ -0,0 +1,88 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 EventBatch + attr_accessor :account_id, :project_id, :revision, :client_name, :client_version, + :anonymize_ip, :enrich_decisions, :visitors + + def as_json(_options = {}) + { + account_id: @account_id, + project_id: @project_id, + revision: @revision, + client_name: @client_name, + client_version: @client_version, + anonymize_ip: @anonymize_ip, + enrich_decisions: @enrich_decisions, + visitors: @visitors + } + end + + class Builder + attr_reader :account_id, :project_id, :revision, :client_name, :client_version, + :anonymize_ip, :enrich_decisions, :visitors + + def build + event_batch = EventBatch.new + event_batch.account_id = @account_id + event_batch.project_id = @project_id + event_batch.revision = @revision + event_batch.client_name = @client_name + event_batch.client_version = @client_version + event_batch.anonymize_ip = @anonymize_ip + event_batch.enrich_decisions = @enrich_decisions + event_batch.visitors = @visitors + event_batch + end + + def with_account_id(account_id) + @account_id = account_id + end + + def with_project_id(project_id) + @project_id = project_id + end + + def with_revision(revision) + @revision = revision + end + + def with_client_name(client_name) + @client_name = client_name + end + + def with_client_version(client_version) + @client_version = client_version + end + + def with_anonymize_ip(anonymize_ip) + @anonymize_ip = anonymize_ip + end + + def with_enrich_decisions(enrich_decisions) + @enrich_decisions = enrich_decisions + end + + def with_visitors(visitors) + @visitors = visitors + end + end + end +end diff --git a/lib/optimizely/event/entity/event_context.rb b/lib/optimizely/event/entity/event_context.rb new file mode 100644 index 00000000..25392ccf --- /dev/null +++ b/lib/optimizely/event/entity/event_context.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 EventContext + attr_reader :account_id, :project_id, :revision, :client_name, + :client_version, :anonymize_ip + end + + def as_json(_options = {}) + { + account_id: @account_id, + project_id: @project_id, + revision: @revision, + client_name: @client_name, + client_version: @client_version, + anonymize_ip: @anonymize_ip + } + end +end diff --git a/lib/optimizely/event/entity/impression_event.rb b/lib/optimizely/event/entity/impression_event.rb new file mode 100644 index 00000000..eeb6c2f5 --- /dev/null +++ b/lib/optimizely/event/entity/impression_event.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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_relative 'user_event' +module Optimizely + class ImpressionEvent < UserEvent + attr_reader :user_id, :user_attributes + attr_accessor :experiment, :variation + end +end diff --git a/lib/optimizely/event/entity/snapshot.rb b/lib/optimizely/event/entity/snapshot.rb new file mode 100644 index 00000000..ce2381b9 --- /dev/null +++ b/lib/optimizely/event/entity/snapshot.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 Snapshot + attr_reader :decisions, :events + + def initialize(events, decisions = nil) + @decisions = decisions + @events = events + end + + def as_json(_options = {}) + hash = { + events: @events, + decisions: @decisions + } + hash.delete_if { |_key, value| value.nil? } + hash + end + end +end diff --git a/lib/optimizely/event/entity/snapshot_event.rb b/lib/optimizely/event/entity/snapshot_event.rb new file mode 100644 index 00000000..e5f03071 --- /dev/null +++ b/lib/optimizely/event/entity/snapshot_event.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 SnapshotEvent + attr_reader :entity_id, :uuid, :key, :timestamp, :revenue, :value, :event_tags + + def initialize( + entity_id: nil, + uuid: nil, + key: nil, + timestamp: nil, + revenue: nil, + value: nil, + event_tags: nil + ) + @entity_id = entity_id + @uuid = uuid + @key = key + @timestamp = timestamp + @revenue = revenue + @value = value + @event_tags = event_tags + end + + def as_json(_options = {}) + hash = { + entity_id: @entity_id, + uuid: @uuid, + key: @key, + timestamp: @timestamp, + revenue: @revenue, + value: @value, + event_tags: @event_tags + } + hash.delete_if { |_key, value| value.nil? } + hash + end + end +end diff --git a/lib/optimizely/event/entity/user_event.rb b/lib/optimizely/event/entity/user_event.rb new file mode 100644 index 00000000..15a53382 --- /dev/null +++ b/lib/optimizely/event/entity/user_event.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 UserEvent + attr_reader :context, :uuid, :timestamp + end +end diff --git a/lib/optimizely/event/entity/visitor.rb b/lib/optimizely/event/entity/visitor.rb new file mode 100644 index 00000000..685b1326 --- /dev/null +++ b/lib/optimizely/event/entity/visitor.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 Visitor + attr_reader :snapshots, :attributes, :visitor_id + def initialize(snapshots, attributes, visitor_id) + @snapshots = snapshots + @attributes = attributes + @visitor_id = visitor_id + end + + def as_json(_options = {}) + { + snapshots: @snapshots, + attributes: @attributes, + visitor_id: @visitor_id + } + end + end +end diff --git a/lib/optimizely/event/entity/visitor_attribute.rb b/lib/optimizely/event/entity/visitor_attribute.rb new file mode 100644 index 00000000..306d4994 --- /dev/null +++ b/lib/optimizely/event/entity/visitor_attribute.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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 VisitorAttribute + attr_reader :entity_id, :key, :type, :value + def initialize(entity_id, key, type, value) + @entity_id = entity_id + @key = key + @type = type + @value = value + end + + def as_json(_options = {}) + { + entity_id: @entity_id, + key: @key, + type: @type, + value: @value + } + end + end +end diff --git a/spec/event/event_entities_spec.rb b/spec/event/event_entities_spec.rb new file mode 100644 index 00000000..f76f580d --- /dev/null +++ b/spec/event/event_entities_spec.rb @@ -0,0 +1,170 @@ +# frozen_string_literal: true + +# +# Copyright 2019, 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/helpers/constants' +require 'optimizely/event/entity/event_batch' +require 'optimizely/event/entity/visitor_attribute' +require 'optimizely/event/entity/snapshot_event' +require 'optimizely/event/entity/decision' +require 'optimizely/event/entity/snapshot' +require 'optimizely/event/entity/visitor' +describe Optimizely::EventBatch do + before(:example) do + @time_now = Time.now + allow(Time).to receive(:now).and_return(@time_now) + allow(SecureRandom).to receive(:uuid).and_return('a68cf1ad-0393-4e18-af87-efe8f01a7c9c') + + @expected_impression_payload = { + account_id: '12001', + project_id: '111001', + visitors: [{ + attributes: [{ + entity_id: '7723280020', + key: 'device_type', + type: 'custom', + value: 'iPhone' + }, { + entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], + key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], + type: 'custom', + value: true + }], + visitor_id: 'test_user', + snapshots: [{ + decisions: [{ + campaign_id: '7719770039', + experiment_id: '111127', + variation_id: '111128' + }], + events: [{ + entity_id: '7719770039', + timestamp: (@time_now.to_f * 1000).to_i, + uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + key: 'campaign_activated' + }] + }] + }], + anonymize_ip: false, + revision: '42', + client_name: Optimizely::CLIENT_ENGINE, + enrich_decisions: true, + client_version: Optimizely::VERSION + } + + @expected_conversion_payload = { + account_id: '12001', + project_id: '111001', + visitors: [{ + attributes: [{ + entity_id: '111094', + key: 'test_value', + type: 'custom', + value: 'test_attribute' + }], + snapshots: [{ + events: [{ + entity_id: '111095', + timestamp: (@time_now.to_f * 1000).to_i, + uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + key: 'test_event', + value: 1.5, + revenue: 42, + event_tags: { + 'revenue' => 42, + 'non-revenue' => 42, + 'value': 1.5 + } + }] + }], + visitor_id: 'test_user' + }], + anonymize_ip: false, + revision: '42', + client_name: Optimizely::CLIENT_ENGINE, + enrich_decisions: true, + client_version: Optimizely::VERSION + } + end + + it 'should return impression event equal to serialized payload' do + builder = Optimizely::EventBatch::Builder.new + builder.with_account_id('12001') + builder.with_project_id('111001') + builder.with_client_version(Optimizely::VERSION) + builder.with_revision('42') + builder.with_client_name(Optimizely::CLIENT_ENGINE) + builder.with_anonymize_ip(false) + builder.with_enrich_decisions(true) + + visitor_attribute_1 = Optimizely::VisitorAttribute.new('7723280020', 'device_type', 'custom', 'iPhone') + visitor_attribute_2 = Optimizely::VisitorAttribute.new( + Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], + Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'], + 'custom', + true + ) + + snapshot_event = Optimizely::SnapshotEvent.new( + entity_id: '7719770039', + timestamp: (@time_now.to_f * 1000).to_i, + uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + key: 'campaign_activated' + ) + + decision = Optimizely::Decision.new('7719770039', '111127', '111128') + snapshot = Optimizely::Snapshot.new([snapshot_event.as_json], [decision.as_json]) + visitor = Optimizely::Visitor.new([snapshot.as_json], [visitor_attribute_1.as_json, visitor_attribute_2.as_json], 'test_user') + builder.with_visitors([visitor.as_json]) + event_batch = builder.build + + expect(event_batch.as_json).to eq(@expected_impression_payload) + end + + it 'should return conversion event equal to serialized payload' do + builder = Optimizely::EventBatch::Builder.new + builder.with_account_id('12001') + builder.with_project_id('111001') + builder.with_client_version(Optimizely::VERSION) + builder.with_revision('42') + builder.with_client_name(Optimizely::CLIENT_ENGINE) + builder.with_anonymize_ip(false) + builder.with_enrich_decisions(true) + visitor_attribute = Optimizely::VisitorAttribute.new('111094', 'test_value', 'custom', 'test_attribute') + + snapshot_event = Optimizely::SnapshotEvent.new( + entity_id: '111095', + timestamp: (@time_now.to_f * 1000).to_i, + uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c', + key: 'test_event', + value: 1.5, + revenue: 42, + event_tags: { + 'revenue' => 42, + 'non-revenue' => 42, + 'value': 1.5 + } + ) + + snapshot = Optimizely::Snapshot.new([snapshot_event.as_json]) + visitor = Optimizely::Visitor.new([snapshot.as_json], [visitor_attribute.as_json], 'test_user') + builder.with_visitors([visitor.as_json]) + event_batch = builder.build + + expect(event_batch.as_json).to eq(@expected_conversion_payload) + end +end From 0d5be251c788e21700ae8a680a17bab45fd5c450 Mon Sep 17 00:00:00 2001 From: rashidsp Date: Mon, 22 Jul 2019 09:48:09 +0500 Subject: [PATCH 2/3] addresses review --- lib/optimizely/event/entity/conversion_event.rb | 3 +-- lib/optimizely/event/entity/event_context.rb | 15 +++------------ lib/optimizely/event/entity/impression_event.rb | 4 ++-- lib/optimizely/event/entity/snapshot_event.rb | 8 ++++---- lib/optimizely/event/entity/user_event.rb | 4 +++- spec/event/event_entities_spec.rb | 4 ++-- 6 files changed, 15 insertions(+), 23 deletions(-) diff --git a/lib/optimizely/event/entity/conversion_event.rb b/lib/optimizely/event/entity/conversion_event.rb index 8a39966b..0f997091 100644 --- a/lib/optimizely/event/entity/conversion_event.rb +++ b/lib/optimizely/event/entity/conversion_event.rb @@ -18,7 +18,6 @@ require_relative 'user_event' module Optimizely class ConversionEvent < UserEvent - attr_accessor :event, :event_tags - attr_reader :user_id, :user_attributes + attr_reader :event, :event_tags, :user_id, :visitor_attributes, :bot_filtering end end diff --git a/lib/optimizely/event/entity/event_context.rb b/lib/optimizely/event/entity/event_context.rb index 25392ccf..5daad9ce 100644 --- a/lib/optimizely/event/entity/event_context.rb +++ b/lib/optimizely/event/entity/event_context.rb @@ -18,18 +18,9 @@ module Optimizely class EventContext - attr_reader :account_id, :project_id, :revision, :client_name, - :client_version, :anonymize_ip - end + protected - def as_json(_options = {}) - { - account_id: @account_id, - project_id: @project_id, - revision: @revision, - client_name: @client_name, - client_version: @client_version, - anonymize_ip: @anonymize_ip - } + attr_accessor :account_id, :project_id, :revision, :client_name, + :client_version, :anonymize_ip end end diff --git a/lib/optimizely/event/entity/impression_event.rb b/lib/optimizely/event/entity/impression_event.rb index eeb6c2f5..b34ced5b 100644 --- a/lib/optimizely/event/entity/impression_event.rb +++ b/lib/optimizely/event/entity/impression_event.rb @@ -18,7 +18,7 @@ require_relative 'user_event' module Optimizely class ImpressionEvent < UserEvent - attr_reader :user_id, :user_attributes - attr_accessor :experiment, :variation + attr_reader :user_id, :visitor_attributes + attr_accessor :experiment, :variation, :bot_filtering end end diff --git a/lib/optimizely/event/entity/snapshot_event.rb b/lib/optimizely/event/entity/snapshot_event.rb index e5f03071..174d7bbe 100644 --- a/lib/optimizely/event/entity/snapshot_event.rb +++ b/lib/optimizely/event/entity/snapshot_event.rb @@ -17,7 +17,7 @@ # module Optimizely class SnapshotEvent - attr_reader :entity_id, :uuid, :key, :timestamp, :revenue, :value, :event_tags + attr_reader :entity_id, :uuid, :key, :timestamp, :revenue, :value, :tags def initialize( entity_id: nil, @@ -26,7 +26,7 @@ def initialize( timestamp: nil, revenue: nil, value: nil, - event_tags: nil + tags: nil ) @entity_id = entity_id @uuid = uuid @@ -34,7 +34,7 @@ def initialize( @timestamp = timestamp @revenue = revenue @value = value - @event_tags = event_tags + @tags = tags end def as_json(_options = {}) @@ -45,7 +45,7 @@ def as_json(_options = {}) timestamp: @timestamp, revenue: @revenue, value: @value, - event_tags: @event_tags + tags: @tags } hash.delete_if { |_key, value| value.nil? } hash diff --git a/lib/optimizely/event/entity/user_event.rb b/lib/optimizely/event/entity/user_event.rb index 15a53382..f7ab2482 100644 --- a/lib/optimizely/event/entity/user_event.rb +++ b/lib/optimizely/event/entity/user_event.rb @@ -17,6 +17,8 @@ # module Optimizely class UserEvent - attr_reader :context, :uuid, :timestamp + protected + + attr_accessor :event_context, :uuid, :timestamp end end diff --git a/spec/event/event_entities_spec.rb b/spec/event/event_entities_spec.rb index f76f580d..b58fae16 100644 --- a/spec/event/event_entities_spec.rb +++ b/spec/event/event_entities_spec.rb @@ -84,7 +84,7 @@ key: 'test_event', value: 1.5, revenue: 42, - event_tags: { + tags: { 'revenue' => 42, 'non-revenue' => 42, 'value': 1.5 @@ -153,7 +153,7 @@ key: 'test_event', value: 1.5, revenue: 42, - event_tags: { + tags: { 'revenue' => 42, 'non-revenue' => 42, 'value': 1.5 From f3df881135082f424d6ac58a49b0ed77f11322b9 Mon Sep 17 00:00:00 2001 From: rashidsp Date: Mon, 22 Jul 2019 13:45:45 +0500 Subject: [PATCH 3/3] changes attributes --- .../event/entity/conversion_event.rb | 18 ++++++++++++++- lib/optimizely/event/entity/decision.rb | 3 +-- lib/optimizely/event/entity/event_batch.rb | 4 +--- lib/optimizely/event/entity/event_context.rb | 22 ++++++++++++++----- .../event/entity/impression_event.rb | 20 +++++++++++++++-- lib/optimizely/event/entity/snapshot.rb | 2 +- lib/optimizely/event/entity/snapshot_event.rb | 2 +- lib/optimizely/event/entity/user_event.rb | 4 +--- lib/optimizely/event/entity/visitor.rb | 2 +- .../event/entity/visitor_attribute.rb | 2 +- 10 files changed, 59 insertions(+), 20 deletions(-) diff --git a/lib/optimizely/event/entity/conversion_event.rb b/lib/optimizely/event/entity/conversion_event.rb index 0f997091..8c5ea55b 100644 --- a/lib/optimizely/event/entity/conversion_event.rb +++ b/lib/optimizely/event/entity/conversion_event.rb @@ -18,6 +18,22 @@ require_relative 'user_event' module Optimizely class ConversionEvent < UserEvent - attr_reader :event, :event_tags, :user_id, :visitor_attributes, :bot_filtering + attr_reader :event_context, :event, :user_id, :visitor_attributes, :tags, :bot_filtering + + def initialize( + event_context, + event, + user_id, + visitor_attributes, + tags, + bot_filtering = nil + ) + @event_context = event_context + @event = event + @user_id = user_id + @visitor_attributes = visitor_attributes + @tags = tags + @bot_filtering = bot_filtering + end end end diff --git a/lib/optimizely/event/entity/decision.rb b/lib/optimizely/event/entity/decision.rb index 064b17ea..015f82f6 100644 --- a/lib/optimizely/event/entity/decision.rb +++ b/lib/optimizely/event/entity/decision.rb @@ -15,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - module Optimizely class Decision attr_reader :campaign_id, :experiment_id, :variation_id @@ -26,7 +25,7 @@ def initialize(campaign_id, experiment_id, variation_id) @variation_id = variation_id end - def as_json(_options = {}) + def as_json { campaign_id: @campaign_id, experiment_id: @experiment_id, diff --git a/lib/optimizely/event/entity/event_batch.rb b/lib/optimizely/event/entity/event_batch.rb index 5711e663..70aca415 100644 --- a/lib/optimizely/event/entity/event_batch.rb +++ b/lib/optimizely/event/entity/event_batch.rb @@ -15,14 +15,12 @@ # See the License for the specific language governing permissions and # limitations under the License. # - -require 'json' module Optimizely class EventBatch attr_accessor :account_id, :project_id, :revision, :client_name, :client_version, :anonymize_ip, :enrich_decisions, :visitors - def as_json(_options = {}) + def as_json { account_id: @account_id, project_id: @project_id, diff --git a/lib/optimizely/event/entity/event_context.rb b/lib/optimizely/event/entity/event_context.rb index 5daad9ce..75ff6eb1 100644 --- a/lib/optimizely/event/entity/event_context.rb +++ b/lib/optimizely/event/entity/event_context.rb @@ -15,12 +15,24 @@ # See the License for the specific language governing permissions and # limitations under the License. # - module Optimizely class EventContext - protected - - attr_accessor :account_id, :project_id, :revision, :client_name, - :client_version, :anonymize_ip + attr_reader :account_id, :project_id, :revision, :client_name, + :client_version, :anonymize_ip + def initialize( + account_id, + project_id, + revision, + client_name, + client_version, + anonymize_ip + ) + @account_id = account_id + @project_id = project_id + @revision = revision + @client_name = client_name + @client_version = client_version + @anonymize_ip = anonymize_ip + end end end diff --git a/lib/optimizely/event/entity/impression_event.rb b/lib/optimizely/event/entity/impression_event.rb index b34ced5b..71c5234f 100644 --- a/lib/optimizely/event/entity/impression_event.rb +++ b/lib/optimizely/event/entity/impression_event.rb @@ -18,7 +18,23 @@ require_relative 'user_event' module Optimizely class ImpressionEvent < UserEvent - attr_reader :user_id, :visitor_attributes - attr_accessor :experiment, :variation, :bot_filtering + attr_reader :event_context, :user_id, :experiment, :variation, :visitor_attributes, + :bot_filtering + + def initialize( + event_context, + user_id, + experiment, + variation, + visitor_attributes, + bot_filtering + ) + @event_context = event_context + @user_id = user_id + @experiment = experiment + @variation = variation + @visitor_attributes = visitor_attributes + @bot_filtering = bot_filtering + end end end diff --git a/lib/optimizely/event/entity/snapshot.rb b/lib/optimizely/event/entity/snapshot.rb index ce2381b9..a5e26c44 100644 --- a/lib/optimizely/event/entity/snapshot.rb +++ b/lib/optimizely/event/entity/snapshot.rb @@ -24,7 +24,7 @@ def initialize(events, decisions = nil) @events = events end - def as_json(_options = {}) + def as_json hash = { events: @events, decisions: @decisions diff --git a/lib/optimizely/event/entity/snapshot_event.rb b/lib/optimizely/event/entity/snapshot_event.rb index 174d7bbe..234b9037 100644 --- a/lib/optimizely/event/entity/snapshot_event.rb +++ b/lib/optimizely/event/entity/snapshot_event.rb @@ -37,7 +37,7 @@ def initialize( @tags = tags end - def as_json(_options = {}) + def as_json hash = { entity_id: @entity_id, uuid: @uuid, diff --git a/lib/optimizely/event/entity/user_event.rb b/lib/optimizely/event/entity/user_event.rb index f7ab2482..2613a14d 100644 --- a/lib/optimizely/event/entity/user_event.rb +++ b/lib/optimizely/event/entity/user_event.rb @@ -17,8 +17,6 @@ # module Optimizely class UserEvent - protected - - attr_accessor :event_context, :uuid, :timestamp + attr_reader :event_context, :uuid, :timestamp end end diff --git a/lib/optimizely/event/entity/visitor.rb b/lib/optimizely/event/entity/visitor.rb index 685b1326..8084e550 100644 --- a/lib/optimizely/event/entity/visitor.rb +++ b/lib/optimizely/event/entity/visitor.rb @@ -24,7 +24,7 @@ def initialize(snapshots, attributes, visitor_id) @visitor_id = visitor_id end - def as_json(_options = {}) + def as_json { snapshots: @snapshots, attributes: @attributes, diff --git a/lib/optimizely/event/entity/visitor_attribute.rb b/lib/optimizely/event/entity/visitor_attribute.rb index 306d4994..0d1cc75d 100644 --- a/lib/optimizely/event/entity/visitor_attribute.rb +++ b/lib/optimizely/event/entity/visitor_attribute.rb @@ -25,7 +25,7 @@ def initialize(entity_id, key, type, value) @value = value end - def as_json(_options = {}) + def as_json { entity_id: @entity_id, key: @key,