Skip to content

Commit

Permalink
Merge pull request #27 from dhruvCW/config_refactor
Browse files Browse the repository at this point in the history
Refactor ABSmartly configuration setup to allow for all the configuration to be specified through the ABSmartlyConfig object
  • Loading branch information
hermeswaldemarin authored Sep 4, 2024
2 parents dea8e3e + 3f946e1 commit e7777fd
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 198 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: [ '3.0', '3.1', '3.2', '3.3', '3.4' ]
steps:
- uses: actions/checkout@v3

- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.0.0
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true

- name: Bundle install
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run RSpec
run: COVERAGE=true bundle exec rspec
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.7.6
3.0.6
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ GEM
reline (0.1.5)
io-console (~> 0.5)
rexml (3.2.8)
strscan (>= 3.0.9)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
Expand Down Expand Up @@ -60,7 +59,6 @@ GEM
parser (>= 3.1.1.0)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
strscan (3.1.0)
unicode-display_width (2.2.0)

PLATFORMS
Expand Down
2 changes: 1 addition & 1 deletion example/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@
}
ctx2.track("payment", properties)

ctx2.close
ctx2.close
87 changes: 23 additions & 64 deletions lib/a_b_smartly.rb
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
70 changes: 43 additions & 27 deletions lib/a_b_smartly_config.rb
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
25 changes: 8 additions & 17 deletions lib/absmartly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
require_relative "context_config"

module Absmartly
@@init_config = nil

class Error < StandardError
end

class << self
attr_accessor :endpoint, :api_key, :application, :environment
MUTEX = Thread::Mutex.new

def configure_client
yield self
yield sdk_config

sdk_config.validate!
end

def create
Expand All @@ -40,24 +40,15 @@ def context_data
sdk.context_data
end

private
def client_config
@client_config = ClientConfig.create
@client_config.endpoint = @endpoint
@client_config.api_key = @api_key
@client_config.application = @application
@client_config.environment = @environment
@client_config
end
private_constant :MUTEX

private
def sdk_config
@sdk_config = ABSmartlyConfig.create
@sdk_config.client = Client.create(client_config)
@sdk_config
MUTEX.synchronize { @sdk_config ||= ABSmartlyConfig.create }
end

def sdk
@sdk ||= create
MUTEX.synchronize { @sdk ||= create }
end
end
end
2 changes: 1 addition & 1 deletion lib/absmartly/version.rb
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
Loading

0 comments on commit e7777fd

Please sign in to comment.