-
Notifications
You must be signed in to change notification settings - Fork 28
Add method and URL to Event, add method to EventDispatcher #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc9eab2
Duck type EventDispatcher
a58a4f2
Add method to Events, rename EventBuilder > EventBuilderV1
440e9e3
fix tests
b8a7dac
Add method to NoOpEventDispatcher
4b299b3
Lint
a93f803
Respond to CR feedback
f28c165
Respond to CR feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
< 8000 details-menu class="select-menu-modal position-absolute" style="z-index: 99;" src="/optimizely/ruby-sdk/pull/1/show_toc?base_sha=2b6d0d8db4a1fe4a01c0f1f1449004a1246ac11e&sha1=2b6d0d8db4a1fe4a01c0f1f1449004a1246ac11e&sha2=f28c16544993c02ee60e00345383b7f57be7a3dc" preload>
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,28 @@ | ||
require 'httparty' | ||
|
||
module Optimizely | ||
class BaseEventDispatcher | ||
# Class encapsulating event dispatching functionality. | ||
# Override with your own EventDispatcher providing dispatch_event method. | ||
|
||
def dispatch_event(_url, _params) | ||
end | ||
end | ||
|
||
class NoOpEventDispatcher < BaseEventDispatcher | ||
class NoOpEventDispatcher | ||
# Class providing dispatch_event method which does nothing. | ||
|
||
def dispatch_event(_url, _params) | ||
def dispatch_event(event) | ||
end | ||
end | ||
|
||
class EventDispatcher < BaseEventDispatcher | ||
class EventDispatcher | ||
REQUEST_TIMEOUT = 10 | ||
|
||
def dispatch_event(url, params) | ||
def dispatch_event(event) | ||
67E6 td> | # Dispatch the event being represented by the Event object. | |
# | ||
# url - URL to send impression/conversion event to. | ||
# params - Params to be sent to the impression/conversion event. | ||
# event - Event object | ||
|
||
HTTParty.get(url, query: params, timeout: REQUEST_TIMEOUT) | ||
rescue Timeout::Error => e | ||
return e | ||
if event.http_verb == :get | ||
begin | ||
HTTParty.get(event.url, query: event.params, timeout: REQUEST_TIMEOUT) | ||
rescue Timeout::Error => e | ||
return e | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,13 +14,9 @@ | |
A3E2 } | ||
@event = Optimizely::Event.new(@params) | ||
end | ||
|
||
it 'should return URL when url is called' do | ||
expect(@event.url).to eq('https://111001.log.optimizely.com/event') | ||
end | ||
end | ||
|
||
describe Optimizely::EventBuilder do | ||
describe Optimizely::EventBuilderV1 do | ||
before(:context) do | ||
@version = Optimizely::VERSION | ||
@config_body = OptimizelySpec::CONFIG_BODY | ||
|
@@ -32,7 +28,7 @@ | |
before(:example) do | ||
config = Optimizely::ProjectConfig.new(@config_body_JSON, @logger, @error_handler) | ||
bucketer = Optimizely::Bucketer.new(config) | ||
@event_builder = Optimizely::EventBuilder.new(config, bucketer) | ||
@event_builder = Optimizely::EventBuilderV1.new(config, bucketer) | ||
end | ||
|
||
it 'should create Event object with right params when create_impression_event is called' do | ||
|
@@ -52,7 +48,7 @@ | |
|
||
expect(@event_builder).to receive(:create_impression_event) | ||
.with('test_experiment', 'test_user') | ||
.and_return(Optimizely::Event.new(expected_params)) | ||
.and_return(Optimizely::Event.new(:get, '', expected_params)) | ||
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. for completeness, we should also have a test case to check that the event is built with the correct method and url |
||
impression_event = @event_builder.create_impression_event('test_experiment', 'test_user') | ||
expect(impression_event.params).to eq(expected_params) | ||
end | ||
|
@@ -75,7 +71,7 @@ | |
|
||
expect(@event_builder).to receive(:create_impression_event) | ||
.with('test_experiment', 'test_user', {'browser_type' => 'firefox'}) | ||
.and_return(Optimizely::Event.new(expected_params)) | ||
.and_return(Optimizely::Event.new(:get, '', expected_params)) | ||
impression_event = @event_builder.create_impression_event('test_experiment', | ||
'test_user', | ||
{'browser_type' => 'firefox'}) | ||
|
@@ -99,7 +95,7 @@ | |
|
||
expect(@event_builder).to receive(:create_conversion_event) | ||
.with('test_event', 'test_user') | ||
.and_return(Optimizely::Event.new(expected_params)) | ||
.and_return(Optimizely::Event.new(:get, '', expected_params)) | ||
conversion_event = @event_builder.create_conversion_event('test_event', 'test_user') | ||
expect(conversion_event.params).to eq(expected_params) | ||
end | ||
|
@@ -122,7 +118,7 @@ | |
|
||
expect(@event_builder).to receive(:create_conversion_event) | ||
.with('test_event', 'test_user', {'browser_type' => 'firefox'}) | ||
.and_return(Optimizely::Event.new(expected_params)) | ||
.and_return(Optimizely::Event.new(:get, '', expected_params)) | ||
conversion_event = @event_builder.create_conversion_event('test_event', 'test_user', {'browser_type' => 'firefox'}) | ||
expect(conversion_event.params).to eq(expected_params) | ||
end | ||
|
@@ -145,7 +141,7 @@ | |
|
||
expect(@event_builder).to receive(:create_conversion_event) | ||
.with('test_event', 'test_user', nil, 42) | ||
.and_return(Optimizely::Event.new(expected_params)) | ||
.and_return(Optimizely::Event.new(:get, '', expected_params)) | ||
conversion_event = @event_builder.create_conversion_event('test_event', 'test_user', nil, 42) | ||
expect(conversion_event.params).to eq(expected_params) | ||
end | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Instead of creating two different event builder classes, why don't we have one event builder that can build V1 events and V2 events. That way you don't have to worry about handling the instantiation of two different builder classes.
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.
Because when we eventually remove V1 support it will be easier to simply remove the
EventBuilderV1
class than to rip out the V1 logic from a larger omnibusEventBuilder
.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.
I disagree because when we rip out V1 support, we would just be changing the internals of this class and the API references. If we keep two classes around there will also be some forking logic throughout the codebase that we need to kill. Providing the event builder as some sort of facade/factory will make it much easier to deal with.
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.
I think it is way better to pick the right EventBuilder at object invocation and not have logic all over. The model here allows that.
Uh oh!
There was an error while loading. Please reload this page.
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.
How about this, since the
EventBuilder
is already initialized with the project config (which tells us whether we are in V1 or V2), why don't we just have the event builder determine whether to return a V1 Event or V2 event when we calleventBuilder.getConversionEvent
andeventBuilder.getImpressionEvent
. That event can be passed into event dispatcher, which determines based on the event whether to get or post. This way, the optimizely instance calling the event builder doesn't even have to make the decision to use EventBuilder1 or EventBuilder2