-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from dhruvCW/config_refactor
Refactor ABSmartly configuration setup to allow for all the configuration to be specified through the ABSmartlyConfig object
- Loading branch information
Showing
18 changed files
with
180 additions
and
198 deletions.
There are no files selected for viewing
This file contains 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 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 +1 @@ | ||
2.7.6 | ||
3.0.6 |
This file contains 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 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 |
---|---|---|
|
@@ -82,4 +82,4 @@ | |
} | ||
ctx2.track("payment", properties) | ||
|
||
ctx2.close | ||
ctx2.close |
This file contains 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,92 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
require "time" | ||
require "singleton" | ||
require "forwardable" | ||
require_relative "context" | ||
require_relative "audience_matcher" | ||
require_relative "default_context_data_provider" | ||
require_relative "default_context_event_handler" | ||
require_relative "default_variable_parser" | ||
require_relative "default_audience_deserializer" | ||
require_relative "scheduled_thread_pool_executor" | ||
require_relative "a_b_smartly_config" | ||
require_relative "absmartly/version" | ||
|
||
class ABSmartly | ||
attr_accessor :context_data_provider, :context_event_handler, | ||
:variable_parser, :scheduler, :context_event_logger, | ||
:audience_deserializer, :client | ||
extend Forwardable | ||
|
||
def self.configure_client(&block) | ||
@@init_http = block | ||
end | ||
attr_reader :config | ||
|
||
def_delegators :@config, :context_data_provider, :context_event_handler, :variable_parser, :context_event_logger, | ||
:audience_deserializer, :client | ||
|
||
def_delegators :@config, :endpoint, :api_key, :application, :environment | ||
|
||
def self.create(config) | ||
ABSmartly.new(config) | ||
new(config) | ||
end | ||
|
||
def initialize(config) | ||
@@init_http = nil | ||
@context_data_provider = config.context_data_provider | ||
@context_event_handler = config.context_event_handler | ||
@context_event_logger = config.context_event_logger | ||
@variable_parser = config.variable_parser | ||
@audience_deserializer = config.audience_deserializer | ||
@scheduler = config.scheduler | ||
|
||
if @context_data_provider.nil? || @context_event_handler.nil? | ||
@client = config.client | ||
raise ArgumentError.new("Missing Client instance configuration") if @client.nil? | ||
|
||
if @context_data_provider.nil? | ||
@context_data_provider = DefaultContextDataProvider.new(@client) | ||
end | ||
|
||
if @context_event_handler.nil? | ||
@context_event_handler = DefaultContextEventHandler.new(@client) | ||
end | ||
end | ||
config.validate! | ||
|
||
if @variable_parser.nil? | ||
@variable_parser = DefaultVariableParser.new | ||
end | ||
|
||
if @audience_deserializer.nil? | ||
@audience_deserializer = DefaultAudienceDeserializer.new | ||
end | ||
if @scheduler.nil? | ||
@scheduler = ScheduledThreadPoolExecutor.new(1) | ||
end | ||
@config = config | ||
end | ||
|
||
def create_context(config) | ||
validate_params(config) | ||
Context.create(get_utc_format, config, @context_data_provider.context_data, | ||
@context_data_provider, @context_event_handler, @context_event_logger, @variable_parser, | ||
AudienceMatcher.new(@audience_deserializer)) | ||
def create_context(context_config) | ||
Context.create(get_utc_format, context_config, context_data, | ||
context_data_provider, context_event_handler, context_event_logger, variable_parser, | ||
AudienceMatcher.new(audience_deserializer)) | ||
end | ||
|
||
def create_context_with(config, data) | ||
Context.create(get_utc_format, config, data, | ||
@context_data_provider, @context_event_handler, @context_event_logger, @variable_parser, | ||
AudienceMatcher.new(@audience_deserializer)) | ||
def create_context_with(context_config, data) | ||
Context.create(get_utc_format, context_config, data, | ||
context_data_provider, context_event_handler, context_event_logger, variable_parser, | ||
AudienceMatcher.new(audience_deserializer)) | ||
end | ||
|
||
def context_data | ||
@context_data_provider.context_data | ||
context_data_provider.context_data | ||
end | ||
|
||
private | ||
def get_utc_format | ||
Time.now.utc.iso8601(3) | ||
end | ||
|
||
def validate_params(params) | ||
params.units.each do |key, value| | ||
unless value.is_a?(String) || value.is_a?(Numeric) | ||
raise ArgumentError.new("Unit '#{key}' UID is of unsupported type '#{value.class}'. UID must be one of ['string', 'number']") | ||
end | ||
|
||
if value.to_s.size.zero? | ||
raise ArgumentError.new("Unit '#{key}' UID length must be >= 1") | ||
end | ||
end | ||
end | ||
end |
This file contains 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,49 +1,65 @@ | ||
# frozen_string_literal: true | ||
|
||
require "forwardable" | ||
|
||
require_relative "client" | ||
require_relative "client_config" | ||
require_relative "default_context_data_provider" | ||
require_relative "default_context_event_handler" | ||
require_relative "default_variable_parser" | ||
require_relative "default_audience_deserializer" | ||
|
||
class ABSmartlyConfig | ||
attr_accessor :context_data_provider, :context_event_handler, | ||
:variable_parser, :scheduler, :context_event_logger, | ||
:client, :audience_deserializer | ||
extend Forwardable | ||
|
||
attr_accessor :scheduler | ||
|
||
attr_writer :context_data_provider, :context_event_handler, :audience_deserializer, :variable_parser, :client | ||
|
||
attr_reader :client_config, :context_event_logger | ||
|
||
def_delegators :@client_config, :endpoint, :api_key, :application, :environment | ||
def_delegators :@client_config, :connect_timeout, :connection_request_timeout, :retry_interval, :max_retries | ||
|
||
def self.create | ||
ABSmartlyConfig.new | ||
new | ||
end | ||
|
||
def context_data_provider=(context_data_provider) | ||
@context_data_provider = context_data_provider | ||
self | ||
def initialize | ||
@client_config = ClientConfig.new | ||
end | ||
|
||
def context_event_handler=(context_event_handler) | ||
@context_event_handler = context_event_handler | ||
self | ||
def validate! | ||
raise ArgumentError.new("event logger not configured") if context_event_logger.nil? | ||
raise ArgumentError.new("failed to initialize client") if client.nil? | ||
raise ArgumentError.new("failed to initialize context_data_provider") if context_data_provider.nil? | ||
end | ||
|
||
def context_data_provide | ||
@context_event_handler | ||
def context_event_logger=(context_event_logger) | ||
if context_event_logger.is_a?(Proc) | ||
@context_event_logger = ContextEventLoggerCallback.new(context_event_logger) | ||
else | ||
@context_event_logger = context_event_logger | ||
end | ||
end | ||
|
||
def variable_parser=(variable_parser) | ||
@variable_parser = variable_parser | ||
self | ||
def variable_parser | ||
@variable_parser ||= DefaultVariableParser.new | ||
end | ||
|
||
def scheduler=(scheduler) | ||
@scheduler = scheduler | ||
self | ||
def audience_deserializer | ||
@audience_deserializer ||= DefaultAudienceDeserializer.new | ||
end | ||
|
||
def context_event_logger=(context_event_logger) | ||
@context_event_logger = context_event_logger | ||
self | ||
def context_data_provider | ||
@context_data_provider ||= DefaultContextDataProvider.new(client) | ||
end | ||
|
||
def audience_deserializer=(audience_deserializer) | ||
@audience_deserializer = audience_deserializer | ||
self | ||
def context_event_handler | ||
@context_event_handler ||= DefaultContextEventHandler.new(client) | ||
end | ||
|
||
def client=(client) | ||
@client = client | ||
self | ||
def client | ||
@client ||= Client.new(client_config) | ||
end | ||
end |
This file contains 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 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,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Absmartly | ||
VERSION = "1.1.2" | ||
VERSION = "1.2.0" | ||
end |
Oops, something went wrong.