|
| 1 | +--- |
| 2 | +title: Ruby OpenFeature Provider |
| 3 | +sidebar_label: OpenFeature |
| 4 | +sidebar_position: 4 |
| 5 | +description: How to implement the OpenFeature Provider |
| 6 | +sidebar_custom_props: { icon: material-symbols:toggle-off } |
| 7 | +--- |
| 8 | + |
| 9 | +# OpenFeature Provider |
| 10 | + |
| 11 | +OpenFeature is an open standard that provides a vendor-agnostic, community-driven API for feature flagging that works with DevCycle. |
| 12 | + |
| 13 | +DevCycle provides a Ruby implementation of the [OpenFeature](https://openfeature.dev/) Provider interface, if you prefer to use the OpenFeature API. |
| 14 | + |
| 15 | +[](https://github.com/DevCycleHQ/ruby-server-sdk) |
| 16 | + |
| 17 | +## Usage |
| 18 | + |
| 19 | +### Installation |
| 20 | +[//]: # (wizard-install-start) |
| 21 | + |
| 22 | +Install the OpenFeature Ruby SDK and DevCycle Provider: |
| 23 | + |
| 24 | +```shell |
| 25 | +gem install devcycle-ruby-server-sdk |
| 26 | +``` |
| 27 | + |
| 28 | +or |
| 29 | + |
| 30 | +```shell |
| 31 | +bundler add devcycle-ruby-server-sdk |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +[//]: # (wizard-install-end) |
| 36 | + |
| 37 | +### Getting Started |
| 38 | +[//]: # (wizard-initialize-start) |
| 39 | + |
| 40 | +Initialize the DevCycle SDK and set the DevCycleProvider as the provider for OpenFeature: |
| 41 | + |
| 42 | +```ruby |
| 43 | +require 'open_feature/sdk' |
| 44 | +require 'devcycle-ruby-server-sdk' |
| 45 | + |
| 46 | +dvc_client = DevCycle::Client.new(ENV['DEVCYCLE_SERVER_SDK_KEY'], DevCycle::Options.new) |
| 47 | +OpenFeature::SDK.configure do |config| |
| 48 | + config.set_provider(dvc_client.open_feature_provider) |
| 49 | +end |
| 50 | +@open_feature_client = OpenFeature::SDK.build_client |
| 51 | +``` |
| 52 | +[//]: # (wizard-initialize-end) |
| 53 | + |
| 54 | +### Evaluate a Variable |
| 55 | +Use a Variable value by setting the EvaluationContext, then passing the Variable key and default value to one of the OpenFeature flag evaluation methods. |
| 56 | + |
| 57 | +[//]: # (wizard-evaluate-start) |
| 58 | + |
| 59 | +```ruby |
| 60 | +context = OpenFeature::SDK::EvaluationContext.new(user_id:'user_id') |
| 61 | +flag_value = @open_feature_client.fetch_integer_value(flag_key: 'flag_key', default_value: 1, evaluation_context: context) |
| 62 | +``` |
| 63 | +[//]: # (wizard-evaluate-end) |
| 64 | + |
| 65 | +### Required Targeting Key |
| 66 | + |
| 67 | +For the DevCycle SDK to work we require either a `targeting_key` or `user_id` to be set on the OpenFeature context. |
| 68 | +This is used to identify the user as the `user_id` for a `DevCycleUser` in DevCycle. |
| 69 | + |
| 70 | +### Context properties to DevCycleUser |
| 71 | + |
| 72 | +The provider will automatically translate known `DevCycleUser` properties from the OpenFeature context to the `DevCycleUser` object. |
| 73 | +[DevCycleUser Ruby Interface](https://github.com/DevCycleHQ/ruby-server-sdk/blob/main/lib/devcycle-ruby-server-sdk/models/user.rb) |
| 74 | + |
| 75 | +For example all these properties will be set on the `DevCycleUser`: |
| 76 | + |
| 77 | +```ruby |
| 78 | +# Pass context when querying values from the OpenFeature client |
| 79 | +context = OpenFeature::SDK::EvaluationContext.new( |
| 80 | + user_id: 'user_id', |
| 81 | + email: 'email', |
| 82 | + name: 'name', |
| 83 | + appBuild: 1, |
| 84 | + appVersion: '1.0.0', |
| 85 | + randomField: 'value', |
| 86 | + privateCustomData: { 'secretkey' => 'secretvalue' }, |
| 87 | + customData: { 'key' => 'value' }) |
| 88 | +``` |
| 89 | + |
| 90 | +Context properties that are not known `DevCycleUser` properties will be automatically |
| 91 | +added to the `customData` property of the `DevCycleUser`. |
| 92 | + |
| 93 | +### Context Limitations |
| 94 | + |
| 95 | +DevCycle only supports flat JSON Object properties used in the Context. Non-flat properties will be ignored. |
| 96 | + |
| 97 | +For example `obj` will be ignored: |
| 98 | + |
| 99 | +```ruby |
| 100 | +context = OpenFeature::SDK::EvaluationContext.new( |
| 101 | + user_id: 'user_id', |
| 102 | + obj: { 'secretkey' => 'secretvalue' }) |
| 103 | +``` |
| 104 | + |
| 105 | +### JSON Flag Limitations |
| 106 | + |
| 107 | +The OpenFeature spec for JSON flags allows for any type of valid JSON value to be set as the flag value. |
| 108 | + |
| 109 | +For example the following are all valid default value types to use with OpenFeature: |
| 110 | + |
| 111 | +```ruby |
| 112 | +# Invalid JSON values for the DevCycle SDK, will return defaults |
| 113 | +open_feature_client.fetch_object_value(flag_key: 'json-variable', default_value: 1.0, evaluation_context: context) |
| 114 | +open_feature_client.fetch_object_value(flag_key: 'json-variable', default_value: false, evaluation_context: context) |
| 115 | +open_feature_client.fetch_object_value(flag_key: 'json-variable', default_value: 'string', evaluation_context: context) |
| 116 | +open_feature_client.fetch_object_value(flag_key: 'json-variable', default_value: nil, evaluation_context: context) |
| 117 | +``` |
| 118 | + |
| 119 | +However, these are not valid types for the DevCycle SDK, the DevCycle SDK only supports JSON Objects: |
| 120 | + |
| 121 | +```ruby |
| 122 | +# Valid JSON Object as the default value, will be evaluated by the DevCycle SDK |
| 123 | +@client.fetch_object_value(flag_key: 'json-variable', default_value: { 'key' => 'value' }, evaluation_context: context) |
| 124 | +``` |
0 commit comments