Skip to content

Commit 96a17aa

Browse files
committed
feat: support cloud eval reasons
1 parent 288c113 commit 96a17aa

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ group :development, :test do
1515
gem 'rake', '~> 13.0.1'
1616
gem 'pry-byebug'
1717
gem 'rubocop', '~> 1.57.1'
18+
gem 'webmock'
1819
end

lib/devcycle-ruby-server-sdk/api/client.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,20 @@ def variable_with_http_info(key, user, default, opts = {})
325325
if @api_client.config.debugging
326326
@api_client.config.logger.debug "API called: DevCycle::Client#variable\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
327327
end
328+
if default && data.type && data.type.to_s != default.class.name
329+
eval = { reason: DevCycle::EVAL_REASONS::DEFAULT, details: DevCycle::DEFAULT_REASON_DETAILS::TYPE_MISMATCH }
330+
return Variable.new(key: key, value: default, isDefaulted: true, eval: eval)
331+
end
328332
return data
329333
rescue ApiError => error
334+
eval = { reason: DevCycle::EVAL_REASONS::DEFAULT, details: DevCycle::DEFAULT_REASON_DETAILS::MISSING_VARIABLE }
330335
if error.code != 404
331336
@api_client.config.logger.error("Failed to retrieve variable value: #{error.message}")
337+
eval[:details] = DevCycle::DEFAULT_REASON_DETAILS::ERROR
332338
end
333339

334-
return Variable.new(key: key, value: default, isDefaulted: true)
335-
end
340+
return Variable.new(key: key, value: default, isDefaulted: true, eval: eval)
341+
end
336342
end
337343

338344
# Get all variables by key for user data

lib/devcycle-ruby-server-sdk/eval_reasons.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@ module EVAL_REASONS
99
module DEFAULT_REASON_DETAILS
1010
MISSING_CONFIG = 'Missing Config'
1111
USER_NOT_TARGETED = 'User Not Targeted'
12+
TYPE_MISMATCH = 'Variable Type Mismatch'
13+
MISSING_VARIABLE = 'Missing Variable'
14+
ERROR = 'Error'
1215
end
1316
end

lib/devcycle-ruby-server-sdk/models/variable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def self.openapi_types
7878
:'type' => :'String',
7979
:'value' => :'Object',
8080
:'defaultValue' => :'Object',
81-
:'isDefaulted' => :'Boolean'
81+
:'isDefaulted' => :'Boolean',
82+
:'eval' => :'Object'
8283
}
8384
end
8485

spec/api/devcycle_api_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
require 'spec_helper'
1414
require 'json'
15+
require 'webmock'
16+
17+
include WebMock::API
1518

1619
# Unit tests for DevCycle::Client
1720
# Automatically generated by openapi-generator (https://openapi-generator.tech)
@@ -68,6 +71,8 @@
6871
it 'should work' do
6972
result = @api_instance.variable(@user, "ruby-example-tests-default", false)
7073
expect(result.isDefaulted).to eq true
74+
expect(result.eval[:reason]).to eq "DEFAULT"
75+
expect(result.eval[:details]).to eq "Missing Variable"
7176

7277
result = @api_instance.variable_value(@user, "ruby-example-tests-default", true)
7378
expect(result).to eq true
@@ -88,6 +93,11 @@
8893

8994
result = @api_instance.variable_value(@user, "test", true)
9095
expect(result).to eq true
96+
97+
result = @api_instance.variable(@user, "test", "false")
98+
expect(result.isDefaulted).to eq true
99+
expect(result.eval[:reason]).to eq "DEFAULT"
100+
expect(result.eval[:details]).to eq "Variable Type Mismatch"
91101
end
92102
end
93103

@@ -104,4 +114,37 @@
104114
end
105115
end
106116

117+
describe 'get_variable_by_key test' do
118+
before do
119+
WebMock.disable_net_connect!(allow_localhost: true)
120+
end
121+
122+
after do
123+
WebMock.allow_net_connect!
124+
end
125+
126+
it 'should work with mocked response' do
127+
stub_request(:post, "https://bucketing-api.devcycle.com/v1/variables/mocked_variable").
128+
to_return(status: 200, body: "{\"isDefaulted\": false, \"value\": true, \"eval\": {\"reason\": \"SPLIT\", \"details\": \"Random Distribution | All Users\", \"target_id\": \"621642332ea68943c8833c4d\"}}", headers: {})
129+
130+
result = @api_instance.variable(@user, "mocked_variable", false)
131+
expect(result.isDefaulted).to eq false
132+
expect(result.value).to eq true
133+
# Use Hash syntax since eval is deserialized as a Hash
134+
expect(result.eval[:reason]).to eq "SPLIT"
135+
expect(result.eval[:details]).to eq "Random Distribution | All Users"
136+
expect(result.eval[:target_id]).to eq "621642332ea68943c8833c4d"
137+
end
138+
139+
it 'should return error details' do
140+
stub_request(:post, "https://bucketing-api.devcycle.com/v1/variables/test").
141+
to_return(status: 500, body: "{\"isDefaulted\": true, \"value\": false, \"eval\": {\"reason\": \"DEFAULT\", \"details\": \"Error\"}}", headers: {})
142+
143+
result = @api_instance.variable(@user, "test", false)
144+
expect(result.isDefaulted).to eq true
145+
expect(result.eval[:reason]).to eq "DEFAULT"
146+
expect(result.eval[:details]).to eq "Error"
147+
end
148+
end
149+
107150
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
# load the gem
1414
require 'devcycle-ruby-server-sdk'
15+
require 'webmock/rspec'
16+
17+
# Configure WebMock to allow real HTTP requests by default, but enable it for specific tests
18+
WebMock.allow_net_connect!
1519

1620
# The following was generated by the `rspec --init` command. Conventionally, all
1721
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.

0 commit comments

Comments
 (0)