Skip to content
Merged
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
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,29 @@ require 'json'
# Fetch datafile from URL
datafile_url = 'https://cdn.yoursite.com/datafile.json'
response = Net::HTTP.get_response(URI(datafile_url))
datafile_content = JSON.parse(response.body)

# Parse JSON with symbolized keys (required)
datafile_content = JSON.parse(response.body, symbolize_names: true)

# Create SDK instance
f = Featurevisor.create_instance(
datafile: datafile_content
)
```

**Important**: When parsing JSON datafiles, you must use `symbolize_names: true` to ensure proper key handling by the SDK.

Alternatively, you can pass a JSON string directly and the SDK will parse it automatically:

```ruby
# Option 1: Parse JSON yourself (recommended)
datafile_content = JSON.parse(json_string, symbolize_names: true)
f = Featurevisor.create_instance(datafile: datafile_content)

# Option 2: Pass JSON string directly (automatic parsing)
f = Featurevisor.create_instance(datafile: json_string)
```

## Evaluation types

We can evaluate 3 types of values against a particular [feature](https://featurevisor.com/docs/features/):
Expand Down Expand Up @@ -334,9 +349,16 @@ f.set_sticky({
You may also initialize the SDK without passing `datafile`, and set it later on:

```ruby
# Parse with symbolized keys before setting
datafile_content = JSON.parse(json_string, symbolize_names: true)
f.set_datafile(datafile_content)

# Or pass JSON string directly for automatic parsing
f.set_datafile(json_string)
```

**Important**: When calling `set_datafile()`, ensure JSON is parsed with `symbolize_names: true` if you're parsing it yourself.

### Updating datafile

You can set the datafile as many times as you want in your application, which will result in emitting a [`datafile_set`](#datafile_set) event that you can listen and react to accordingly.
Expand Down
26 changes: 6 additions & 20 deletions bin/commands/assess_distribution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def run

# Initialize evaluation counters
flag_evaluations = {
"enabled" => 0,
"disabled" => 0
enabled: 0,
disabled: 0
}
variation_evaluations = {}

Expand All @@ -78,9 +78,9 @@ def run
# Evaluate flag
flag_evaluation = instance.is_enabled(@options.feature, context_copy)
if flag_evaluation
flag_evaluations["enabled"] += 1
flag_evaluations[:enabled] += 1
else
flag_evaluations["disabled"] += 1
flag_evaluations[:disabled] += 1
end

# Evaluate variation if feature has variations
Expand Down Expand Up @@ -147,7 +147,7 @@ def build_datafile(environment)
end

begin
JSON.parse(stdout)
JSON.parse(stdout, symbolize_names: true)
rescue JSON::ParserError => e
puts "Error: Failed to parse datafile JSON: #{e.message}"
exit 1
Expand All @@ -160,27 +160,13 @@ def execute_command(command)
end

def create_instance(datafile)
# Convert datafile to proper format for the SDK
symbolized_datafile = symbolize_keys(datafile)

# Create SDK instance
Featurevisor.create_instance(
datafile: symbolized_datafile,
datafile: datafile,
log_level: get_logger_level
)
end

def symbolize_keys(obj)
case obj
when Hash
obj.transform_keys(&:to_sym).transform_values { |v| symbolize_keys(v) }
when Array
obj.map { |item| symbolize_keys(item) }
else
obj
end
end

def get_logger_level
if @options.verbose
"debug"
Expand Down
18 changes: 2 additions & 16 deletions bin/commands/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def build_datafile(environment)

# Parse the JSON output
begin
JSON.parse(datafile_output)
JSON.parse(datafile_output, symbolize_names: true)
rescue JSON::ParserError => e
puts "Error: Failed to parse datafile JSON: #{e.message}"
puts "Command output: #{datafile_output}"
Expand All @@ -143,16 +143,13 @@ def execute_command(command)
end

def create_instance(datafile)
# Convert string keys to symbols for the SDK
symbolized_datafile = symbolize_keys(datafile)

# Create a real Featurevisor instance
instance = Featurevisor.create_instance(
log_level: get_logger_level
)

# Explicitly set the datafile
instance.set_datafile(symbolized_datafile)
instance.set_datafile(datafile)

instance
end
Expand All @@ -167,17 +164,6 @@ def get_logger_level
end
end

def symbolize_keys(obj)
case obj
when Hash
obj.transform_keys(&:to_sym).transform_values { |v| symbolize_keys(v) }
when Array
obj.map { |item| symbolize_keys(item) }
else
obj
end
end

def benchmark_feature_flag(instance, feature_key, context, n)
start_time = Time.now

Expand Down
Loading