diff --git a/ext/mkrf_conf.rb b/ext/mkrf_conf.rb deleted file mode 100644 index 23c2c7b6..00000000 --- a/ext/mkrf_conf.rb +++ /dev/null @@ -1,11 +0,0 @@ -require "rubygems" - - -# From http://stackoverflow.com/questions/5830835/how-to-add-openssl-dependency-to-gemspec -# the whole reason this file exists: to return an error if openssl -# isn't installed. -require "openssl" - -f = File.open(File.join(File.dirname(__FILE__), "Rakefile"), "w") # create dummy rakefile to indicate success -f.write("task :default\n") -f.close diff --git a/launchdarkly-server-sdk.gemspec b/launchdarkly-server-sdk.gemspec index 237474ef..67b39daf 100644 --- a/launchdarkly-server-sdk.gemspec +++ b/launchdarkly-server-sdk.gemspec @@ -19,7 +19,6 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.extensions = 'ext/mkrf_conf.rb' spec.add_development_dependency "aws-sdk-dynamodb", "~> 1.18" spec.add_development_dependency "bundler", "~> 1.7" diff --git a/lib/ldclient-rb/evaluation.rb b/lib/ldclient-rb/evaluation.rb index d0d2aa38..3c18e7ff 100644 --- a/lib/ldclient-rb/evaluation.rb +++ b/lib/ldclient-rb/evaluation.rb @@ -140,35 +140,44 @@ def self.comparator(converter) end, endsWith: lambda do |a, b| - (a.is_a? String) && (a.end_with? b) + (a.is_a? String) && (b.is_a? String) && (a.end_with? b) end, startsWith: lambda do |a, b| - (a.is_a? String) && (a.start_with? b) + (a.is_a? String) && (b.is_a? String) && (a.start_with? b) end, matches: lambda do |a, b| - (b.is_a? String) && !(Regexp.new b).match(a).nil? + if (b.is_a? String) && (b.is_a? String) + begin + re = Regexp.new b + !re.match(a).nil? + rescue + false + end + else + false + end end, contains: lambda do |a, b| - (a.is_a? String) && (a.include? b) + (a.is_a? String) && (b.is_a? String) && (a.include? b) end, lessThan: lambda do |a, b| - (a.is_a? Numeric) && (a < b) + (a.is_a? Numeric) && (b.is_a? Numeric) && (a < b) end, lessThanOrEqual: lambda do |a, b| - (a.is_a? Numeric) && (a <= b) + (a.is_a? Numeric) && (b.is_a? Numeric) && (a <= b) end, greaterThan: lambda do |a, b| - (a.is_a? Numeric) && (a > b) + (a.is_a? Numeric) && (b.is_a? Numeric) && (a > b) end, greaterThanOrEqual: lambda do |a, b| - (a.is_a? Numeric) && (a >= b) + (a.is_a? Numeric) && (b.is_a? Numeric) && (a >= b) end, before: comparator(DATE_OPERAND) { |n| n < 0 }, diff --git a/lib/ldclient-rb/events.rb b/lib/ldclient-rb/events.rb index 0b65f3d5..a5352a0b 100644 --- a/lib/ldclient-rb/events.rb +++ b/lib/ldclient-rb/events.rb @@ -91,6 +91,7 @@ class StopMessage < SynchronousMessage # @private class EventProcessor def initialize(sdk_key, config, client = nil, diagnostic_accumulator = nil, test_properties = nil) + raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? # see LDClient constructor comment on sdk_key @logger = config.logger @inbox = SizedQueue.new(config.capacity < 100 ? 100 : config.capacity) @flush_task = Concurrent::TimerTask.new(execution_interval: config.flush_interval) do diff --git a/lib/ldclient-rb/ldclient.rb b/lib/ldclient-rb/ldclient.rb index ed0a724e..1dc0cc25 100644 --- a/lib/ldclient-rb/ldclient.rb +++ b/lib/ldclient-rb/ldclient.rb @@ -33,6 +33,16 @@ class LDClient # @return [LDClient] The LaunchDarkly client instance # def initialize(sdk_key, config = Config.default, wait_for_sec = 5) + # Note that sdk_key is normally a required parameter, and a nil value would cause the SDK to + # fail in most configurations. However, there are some configurations where it would be OK + # (offline = true, *or* we are using LDD mode or the file data source and events are disabled + # so we're not connecting to any LD services) so rather than try to check for all of those + # up front, we will let the constructors for the data source implementations implement this + # fail-fast as appropriate, and just check here for the part regarding events. + if !config.offline? && config.send_events + raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? + end + @sdk_key = sdk_key @event_factory_default = EventFactory.new(false) @@ -352,6 +362,7 @@ def create_default_data_source(sdk_key, config, diagnostic_accumulator) if config.offline? return NullUpdateProcessor.new end + raise ArgumentError, "sdk_key must not be nil" if sdk_key.nil? # see LDClient constructor comment on sdk_key requestor = Requestor.new(sdk_key, config) if config.stream? StreamProcessor.new(sdk_key, config, requestor, diagnostic_accumulator) diff --git a/spec/evaluation_spec.rb b/spec/evaluation_spec.rb index 14d5ed80..b8bed817 100644 --- a/spec/evaluation_spec.rb +++ b/spec/evaluation_spec.rb @@ -495,13 +495,13 @@ def boolean_flag_with_clauses(clauses) # mixed strings and numbers [ :in, "99", 99, false ], [ :in, 99, "99", false ], - #[ :contains, "99", 99, false ], # currently throws exception - would return false in Java SDK - #[ :startsWith, "99", 99, false ], # currently throws exception - would return false in Java SDK - #[ :endsWith, "99", 99, false ] # currently throws exception - would return false in Java SDK + [ :contains, "99", 99, false ], + [ :startsWith, "99", 99, false ], + [ :endsWith, "99", 99, false ], [ :lessThanOrEqual, "99", 99, false ], - #[ :lessThanOrEqual, 99, "99", false ], # currently throws exception - would return false in Java SDK + [ :lessThanOrEqual, 99, "99", false ], [ :greaterThanOrEqual, "99", 99, false ], - #[ :greaterThanOrEqual, 99, "99", false ], # currently throws exception - would return false in Java SDK + [ :greaterThanOrEqual, 99, "99", false ], # regex [ :matches, "hello world", "hello.*rld", true ], @@ -509,7 +509,7 @@ def boolean_flag_with_clauses(clauses) [ :matches, "hello world", "l+", true ], [ :matches, "hello world", "(world|planet)", true ], [ :matches, "hello world", "aloha", false ], - #[ :matches, "hello world", "***not a regex", false ] # currently throws exception - same as Java SDK + [ :matches, "hello world", "***not a regex", false ], # dates [ :before, dateStr1, dateStr2, true ], diff --git a/spec/ldclient_spec.rb b/spec/ldclient_spec.rb index 1d3bb506..40ce5a1d 100644 --- a/spec/ldclient_spec.rb +++ b/spec/ldclient_spec.rb @@ -49,6 +49,46 @@ def event_processor client.instance_variable_get(:@event_processor) end + describe "constructor requirement of non-nil sdk key" do + it "is not enforced when offline" do + subject.new(nil, offline_config) + end + + it "is not enforced if use_ldd is true and send_events is false" do + subject.new(nil, LaunchDarkly::Config.new({ use_ldd: true, send_events: false })) + end + + it "is not enforced if using file data and send_events is false" do + source = LaunchDarkly::FileDataSource.factory({}) + subject.new(nil, LaunchDarkly::Config.new({ data_source: source, send_events: false })) + end + + it "is enforced in streaming mode even if send_events is false" do + expect { + subject.new(nil, LaunchDarkly::Config.new({ send_events: false })) + }.to raise_error(ArgumentError) + end + + it "is enforced in polling mode even if send_events is false" do + expect { + subject.new(nil, LaunchDarkly::Config.new({ stream: false, send_events: false })) + }.to raise_error(ArgumentError) + end + + it "is enforced if use_ldd is true and send_events is true" do + expect { + subject.new(nil, LaunchDarkly::Config.new({ use_ldd: true })) + }.to raise_error(ArgumentError) + end + + it "is enforced if using file data and send_events is true" do + source = LaunchDarkly::FileDataSource.factory({}) + expect { + subject.new(nil, LaunchDarkly::Config.new({ data_source: source })) + }.to raise_error(ArgumentError) + end + end + describe '#variation' do feature_with_value = { key: "key", on: false, offVariation: 0, variations: ["value"], version: 100, trackEvents: true, debugEventsUntilDate: 1000 }