Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions launchdarkly-server-sdk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "timecop", "~> 0.9"
spec.add_development_dependency "listen", "~> 3.3" # see file_data_source.rb
spec.add_development_dependency "webrick", "~> 1.7"
spec.add_development_dependency "erb", "~> 2.2.3"

# required by dynamodb
spec.add_development_dependency "oga", "~> 2.2"

Expand Down
14 changes: 13 additions & 1 deletion lib/ldclient-rb/file_data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,21 @@ module LaunchDarkly
rescue LoadError
end

@@have_erb = false
begin
require 'erb'
@@have_erb = true
rescue LoadError
end

# @private
def self.have_listen?
@@have_listen
end

def self.have_erb?
@@have_erb
end
#
# Provides a way to use local files as a source of feature flag state. This allows using a
# predetermined feature flag state without an actual LaunchDarkly connection.
Expand Down Expand Up @@ -192,7 +202,9 @@ def load_all
end

def load_file(path, all_data)
parsed = parse_content(IO.read(path))
content = IO.read(path)
content = ERB.new(content).result if LaunchDarkly.have_erb?
parsed = parse_content(content)
(parsed[:flags] || {}).each do |key, flag|
add_item(all_data, FEATURES, flag)
end
Expand Down
55 changes: 55 additions & 0 deletions spec/file_data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ def []=(key, value)
EOF
}

let(:json_with_erb) { <<-EOF
{
"flagValues": {
"onePlusOne": <%= 1 + 1 %>,
"constantTwo": 2
}
}
EOF
}


let(:all_properties_yaml) { <<-EOF
---
flags:
Expand All @@ -86,6 +97,15 @@ def []=(key, value)
EOF
}

let(:yaml_with_erb) { <<-EOF
---
flagValues:
onePlusOne: <%= 1 + 1 %>
constantTwo: 2
EOF
}


let(:unsafe_yaml) { <<-EOF
--- !ruby/hash:BadClassWeShouldNotInstantiate
foo: bar
Expand Down Expand Up @@ -132,6 +152,41 @@ def with_data_source(options)
end
end

it "evaulates erb in json if ERB is available" do
file = make_temp_file(yaml_with_erb)
with_data_source({ paths: [ file.path ] }) do |ds|
ds.start
onePlusOne = @store.all(LaunchDarkly::FEATURES).dig(:onePlusOne, :variations)
constantTwo = @store.all(LaunchDarkly::FEATURES).dig(:constantTwo, :variations)
expect(onePlusOne).to eq([2])
expect(constantTwo).to eq([2])
end
end

it "does not evaulate ERB if ERB is not available" do
expect(LaunchDarkly).to receive(:have_erb?).and_return(false)
file = make_temp_file(yaml_with_erb)
with_data_source({ paths: [ file.path ] }) do |ds|
ds.start
onePlusOne = @store.all(LaunchDarkly::FEATURES).dig(:onePlusOne, :variations)
constantTwo = @store.all(LaunchDarkly::FEATURES).dig(:constantTwo, :variations)
expect(onePlusOne).to eq(['<%= 1 + 1 %>'])
expect(constantTwo).to eq([2])
end
end

it "evaulates erb in json if ERB is available" do
file = make_temp_file(json_with_erb)
with_data_source({ paths: [ file.path ] }) do |ds|
ds.start
onePlusOne = @store.all(LaunchDarkly::FEATURES).dig(:onePlusOne, :variations)
constantTwo = @store.all(LaunchDarkly::FEATURES).dig(:constantTwo, :variations)
expect(onePlusOne).to eq([2])
expect(constantTwo).to eq([2])
end
end


it "loads flags on start - from JSON" do
file = make_temp_file(all_properties_json)
with_data_source({ paths: [ file.path ] }) do |ds|
Expand Down