featureflip-openfeature 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f6fa5f7d072f5f820620ac950eda71f01af344681916c8fd152229a881a0f142
4
+ data.tar.gz: bc9efccb44950cc991d5aabf9594f245075fb205ecc3a0c48f8ba34de2198f87
5
+ SHA512:
6
+ metadata.gz: 7dda6c5cf1cb568b1632b8d8d4fe4af625ccf917236cb0f3ce4cdd4040c01f6ea5ebf07d0670d6ce8170484429eeb3984176cfdab6d701f66f6c9e5a97ae9e83
7
+ data.tar.gz: 754d476ea0e98b5421f21b4e786ce10efaf2acf8664dfce812fdb849e8811e6e593f04d198ebe6758a19ae2ff688b3bc140fe01316988b5ffd0bf66a18576532
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 — 2026-10-10
4
+
5
+ ### Added
6
+
7
+ - First release of the OpenFeature provider for Ruby, backed by the `featureflip` gem. Resolves boolean, string, number, integer, float and object flags through the standard `openfeature-sdk` client, maps Featureflip reasons onto OpenFeature's (an unmet prerequisite surfaces as `PREREQUISITE_FAILED`), exposes `ruleId` and `prerequisiteKey` as flag metadata, writes the targeting key under both `user_id` and `userId`, and emits `PROVIDER_CONFIGURATION_CHANGED` with the changed flag keys. Needs `featureflip` 2.10 or newer. (#3643)
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 Canopy Labs LLC
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Featureflip OpenFeature provider for Ruby
2
+
3
+ Use [Featureflip](https://featureflip.io) feature flags through the
4
+ [OpenFeature](https://openfeature.dev) Ruby SDK.
5
+
6
+ ## Install
7
+
8
+ ```ruby
9
+ # Gemfile
10
+ gem "featureflip-openfeature"
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ruby
16
+ require "featureflip-openfeature"
17
+
18
+ OpenFeature::SDK.set_provider_and_wait(
19
+ Featureflip::OpenFeature::Provider.new(sdk_key: ENV.fetch("FEATUREFLIP_SDK_KEY"))
20
+ )
21
+
22
+ client = OpenFeature::SDK.build_client
23
+ enabled = client.fetch_boolean_value(
24
+ flag_key: "new-checkout",
25
+ default_value: false,
26
+ evaluation_context: OpenFeature::SDK::EvaluationContext.new(targeting_key: "user-42", plan: "pro")
27
+ )
28
+ ```
29
+
30
+ Use `set_provider_and_wait`. Plain `set_provider` initializes in the background
31
+ and returns before flags load, so the next read gets your default.
32
+
33
+ Already using the Featureflip client directly? Pass it in and the provider will
34
+ share it, leaving ownership with you:
35
+
36
+ ```ruby
37
+ client = Featureflip::Client.get("sdk-...")
38
+ OpenFeature::SDK.set_provider_and_wait(Featureflip::OpenFeature::Provider.new(client: client))
39
+ ```
40
+
41
+ ## Behavior
42
+
43
+ - `targeting_key` is written under both `user_id` and `userId`, so a targeting
44
+ rule on either spelling matches. An explicit attribute of either spelling wins.
45
+ - `Time`, `Date` and `DateTime` attributes become ISO-8601 strings.
46
+ - `fetch_integer_value` accepts whole-number floats (`3.0` returns `3`).
47
+ `fetch_float_value` accepts integers (`5` returns `5.0`).
48
+ `fetch_object_value` accepts hashes and arrays only.
49
+ - An unmet prerequisite resolves with reason `PREREQUISITE_FAILED`. `ruleId` and
50
+ `prerequisiteKey` are in `flag_metadata`.
51
+ - `PROVIDER_CONFIGURATION_CHANGED` fires with `flags_changed` whenever flag
52
+ configuration changes after startup. The initial load does not fire it.
53
+
54
+ ## Forking servers
55
+
56
+ On Puma with `preload_app!`, Unicorn or Passenger, register the provider in the
57
+ post-fork hook (`on_worker_boot` or `after_fork`). A provider built before the
58
+ fork leaves each worker with dead polling and streaming threads, so it serves
59
+ the boot snapshot and never emits `PROVIDER_CONFIGURATION_CHANGED`. If you pass
60
+ your own `client:`, call `client.restart` in that hook instead.
61
+
62
+ Full documentation: https://featureflip.io/docs/integrations/openfeature/#ruby
63
+
64
+ ## License
65
+
66
+ Apache-2.0
@@ -0,0 +1,47 @@
1
+ require "date"
2
+ require "time"
3
+
4
+ module Featureflip
5
+ module OpenFeature
6
+ # OpenFeature evaluation context -> the Featureflip SDK's context hash.
7
+ module ContextMapping
8
+ TARGETING_KEY = "targeting_key"
9
+ IDENTITY_KEYS = %w[user_id userId].freeze
10
+
11
+ module_function
12
+
13
+ def to_featureflip(evaluation_context)
14
+ return {} if evaluation_context.nil?
15
+
16
+ fields = evaluation_context.fields
17
+ # The reserved key is OpenFeature's, not an attribute: leaking it into
18
+ # the bag is a recorded .NET-provider bug.
19
+ context = fields.each_with_object({}) do |(key, value), out|
20
+ out[key] = render(value) unless key == TARGETING_KEY
21
+ end
22
+
23
+ targeting_key = fields[TARGETING_KEY]
24
+ blank = targeting_key.nil? || (targeting_key.respond_to?(:empty?) && targeting_key.empty?)
25
+ # BOTH spellings, because the Ruby SDK's condition evaluator reads
26
+ # context[attribute] exactly; the userId/user_id alias exists only on
27
+ # the bucketing path. A rule on either spelling must match. An explicit
28
+ # attribute of either spelling wins and nothing is overwritten.
29
+ if !blank && IDENTITY_KEYS.none? { |k| context.key?(k) }
30
+ IDENTITY_KEYS.each { |k| context[k] = targeting_key }
31
+ end
32
+ context
33
+ end
34
+
35
+ # Every SDK gates its date operators on the ISO grammar, so a Time left as
36
+ # an object would match no date rule at all.
37
+ def render(value)
38
+ case value
39
+ when Time, Date then value.iso8601 # DateTime < Date, so it lands here too
40
+ when Hash then value.transform_values { |v| render(v) }
41
+ when Array then value.map { |v| render(v) }
42
+ else value
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,199 @@
1
+ module Featureflip
2
+ module OpenFeature
3
+ # OpenFeature server provider backed by the Featureflip Ruby SDK.
4
+ #
5
+ # ::OpenFeature::SDK.set_provider_and_wait(
6
+ # Featureflip::OpenFeature::Provider.new(sdk_key: "sdk-...")
7
+ # )
8
+ #
9
+ # Prefer set_provider_and_wait: plain set_provider initializes on a
10
+ # background thread and returns before flags load.
11
+ class Provider
12
+ include ::OpenFeature::SDK::Provider::EventEmitter
13
+
14
+ NAME = "featureflip-ruby"
15
+
16
+ attr_reader :metadata
17
+
18
+ # Pass exactly one of:
19
+ # - sdk_key: (with an optional Featureflip::Config). The provider builds the
20
+ # client in #init and closes it in #shutdown.
21
+ # - client: an existing Featureflip::Client. The caller keeps ownership;
22
+ # #shutdown never closes it.
23
+ def initialize(sdk_key: nil, config: nil, client: nil)
24
+ raise ArgumentError, "pass exactly one of sdk_key: or client:" if sdk_key.nil? == client.nil?
25
+ raise ArgumentError, "config: applies only with sdk_key:" if client && config
26
+
27
+ @metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: NAME).freeze
28
+ @sdk_key = sdk_key
29
+ @config = config
30
+ @client = client
31
+ @owns_client = client.nil?
32
+ # Held across #init's blocking Client.get, so nothing on the SDK's update
33
+ # thread may ever wait on it.
34
+ @lifecycle_mutex = Mutex.new
35
+ @unsubscribe = nil
36
+ # Guards the fields below. Held only for a few assignments and never while
37
+ # an app handler runs, because emit_event calls handlers inline.
38
+ @state_mutex = Mutex.new
39
+ @events_drained = ConditionVariable.new
40
+ @shut_down = false
41
+ # Bumped by #shutdown. A listener only emits for the generation it was
42
+ # subscribed under, so a dispatch the SDK snapshotted before an
43
+ # unsubscribe can never surface on a later subscription.
44
+ @generation = 0
45
+ @emitting = Hash.new(0) # thread => events it is delivering right now
46
+ end
47
+
48
+ # Builds the owned client here rather than in #new because Client.get blocks
49
+ # on the first flag fetch, and that wait belongs inside
50
+ # set_provider_and_wait. It is bounded by config.init_timeout and raises
51
+ # Featureflip::InitializationError, which OpenFeature reports as
52
+ # PROVIDER_ERROR. The evaluation context is unused: this is a
53
+ # dynamic-context (server) provider.
54
+ def init(_evaluation_context = nil)
55
+ @lifecycle_mutex.synchronize do
56
+ # Plain set_provider runs #init on a background thread, so replacing the
57
+ # provider at once can run #shutdown first. Building a client now would
58
+ # leave one nothing ever closes. Setting the provider again re-arms it
59
+ # (see #attach).
60
+ generation = @state_mutex.synchronize { @shut_down ? nil : @generation }
61
+ return nil if generation.nil?
62
+
63
+ @client ||= Featureflip::Client.get(@sdk_key, config: @config)
64
+ # Subscribed after the client is ready, so the initial load is never a
65
+ # "change" (OpenFeature signals it with PROVIDER_READY). Guarded because
66
+ # OpenFeature may initialize a provider more than once. The listener can
67
+ # fire before on_update returns; it checks the generation, not
68
+ # @unsubscribe, so that change is still delivered.
69
+ @unsubscribe ||= @client.on_update(->(keys) { on_flags_changed(keys, generation) })
70
+ end
71
+ nil
72
+ end
73
+
74
+ def shutdown
75
+ @lifecycle_mutex.synchronize do
76
+ @state_mutex.synchronize do
77
+ @shut_down = true
78
+ @generation += 1
79
+ end
80
+ # Always unsubscribe: the listener publishes on this provider's event
81
+ # channel and must not outlive it, even on a borrowed client.
82
+ @unsubscribe&.call
83
+ @unsubscribe = nil
84
+ # A borrowed client is a refcounted handle the caller still holds;
85
+ # closing it would make THEIR handle start serving defaults.
86
+ if @owns_client && @client
87
+ @client.close
88
+ @client = nil
89
+ end
90
+ end
91
+ # Outside the lifecycle mutex: a handler still running may itself call
92
+ # into this provider.
93
+ wait_for_events_in_flight
94
+ nil
95
+ end
96
+
97
+ def fetch_boolean_value(flag_key:, default_value:, evaluation_context: nil)
98
+ resolve(:boolean, flag_key, default_value, evaluation_context)
99
+ end
100
+
101
+ def fetch_string_value(flag_key:, default_value:, evaluation_context: nil)
102
+ resolve(:string, flag_key, default_value, evaluation_context)
103
+ end
104
+
105
+ def fetch_number_value(flag_key:, default_value:, evaluation_context: nil)
106
+ resolve(:number, flag_key, default_value, evaluation_context)
107
+ end
108
+
109
+ def fetch_integer_value(flag_key:, default_value:, evaluation_context: nil)
110
+ resolve(:integer, flag_key, default_value, evaluation_context)
111
+ end
112
+
113
+ def fetch_float_value(flag_key:, default_value:, evaluation_context: nil)
114
+ resolve(:float, flag_key, default_value, evaluation_context)
115
+ end
116
+
117
+ def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
118
+ resolve(:object, flag_key, default_value, evaluation_context)
119
+ end
120
+
121
+ # Called by openfeature-sdk 0.6.1+; 0.5 has no tracking API and never calls it.
122
+ def track(tracking_event_name, evaluation_context: nil, tracking_event_details: nil)
123
+ client = @client
124
+ return if client.nil?
125
+
126
+ metadata = nil
127
+ if tracking_event_details
128
+ metadata = tracking_event_details.fields.dup
129
+ metadata["value"] = tracking_event_details.value unless tracking_event_details.value.nil?
130
+ end
131
+ client.track(tracking_event_name, ContextMapping.to_featureflip(evaluation_context), metadata)
132
+ end
133
+
134
+ private
135
+
136
+ # Reads UNTYPED via variation_detail and applies the type guard here. The
137
+ # SDK's typed accessors report a wrong-typed value as reason "Error", the
138
+ # same reason a real failure carries, so TYPE_MISMATCH could not be told
139
+ # apart from GENERAL through them.
140
+ def resolve(type, flag_key, default_value, evaluation_context)
141
+ client = @client
142
+ if client.nil?
143
+ return ResolutionMapping.error(default_value, ::OpenFeature::SDK::Provider::ErrorCode::PROVIDER_NOT_READY,
144
+ "Featureflip provider has not been initialized")
145
+ end
146
+
147
+ detail = client.variation_detail(flag_key, ContextMapping.to_featureflip(evaluation_context), default_value)
148
+ ResolutionMapping.to_resolution(flag_key, detail, default_value, type)
149
+ rescue StandardError => e
150
+ # The SDK rescues its own evaluation errors; this only catches a mapping
151
+ # defect, which must still serve the caller's default.
152
+ ResolutionMapping.error(default_value, ::OpenFeature::SDK::Provider::ErrorCode::GENERAL, e.message)
153
+ end
154
+
155
+ # OpenFeature attaches a provider every time it is set, always before the
156
+ # #init that follows, so this is the one signal that a later #init is
157
+ # legitimate rather than a stale one that lost the race to #shutdown.
158
+ # Overrides the private EventEmitter#attach, which 0.5 and 0.6 both call.
159
+ def attach(configuration)
160
+ @state_mutex.synchronize { @shut_down = false }
161
+ super
162
+ end
163
+
164
+ # Runs on the SDK's streaming/polling thread. Needs no rescue: Client#on_update
165
+ # isolates and logs a raising listener.
166
+ #
167
+ # openfeature-sdk (0.5 and 0.6) never detaches a replaced provider, so an
168
+ # event emitted after #shutdown would still reach every API-level handler.
169
+ # The generation check and the in-flight count are taken together under
170
+ # @state_mutex, and #shutdown bumps the generation under the same lock and
171
+ # then waits for the count to drain: once #shutdown returns, no event from
172
+ # this provider is being delivered and none can start.
173
+ def on_flags_changed(keys, generation)
174
+ @state_mutex.synchronize do
175
+ return unless generation == @generation
176
+
177
+ @emitting[Thread.current] += 1
178
+ end
179
+ begin
180
+ emit_event(::OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, flags_changed: keys)
181
+ ensure
182
+ @state_mutex.synchronize do
183
+ @emitting.delete(Thread.current) if (@emitting[Thread.current] -= 1).zero?
184
+ @events_drained.broadcast
185
+ end
186
+ end
187
+ end
188
+
189
+ def wait_for_events_in_flight
190
+ @state_mutex.synchronize do
191
+ # A handler that shuts this provider down runs on the delivering thread.
192
+ # Waiting for its own event would never return, so only other threads'
193
+ # deliveries are waited out.
194
+ @events_drained.wait(@state_mutex) while @emitting.any? { |thread, _| !thread.equal?(Thread.current) }
195
+ end
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,103 @@
1
+ module Featureflip
2
+ module OpenFeature
3
+ # Featureflip EvaluationDetail -> OpenFeature ResolutionDetails.
4
+ module ResolutionMapping
5
+ ResolutionDetails = ::OpenFeature::SDK::Provider::ResolutionDetails
6
+ Reason = ::OpenFeature::SDK::Provider::Reason
7
+ ErrorCode = ::OpenFeature::SDK::Provider::ErrorCode
8
+
9
+ # No standard OpenFeature reason models an unmet prerequisite, and reasons
10
+ # are open strings, so it is passed through rather than mislabeled DEFAULT
11
+ # or DISABLED. Same choice as every other Featureflip provider.
12
+ PREREQUISITE_FAILED = "PREREQUISITE_FAILED"
13
+
14
+ REASONS = {
15
+ "RuleMatch" => Reason::TARGETING_MATCH,
16
+ "Fallthrough" => Reason::DEFAULT,
17
+ "FlagDisabled" => Reason::DISABLED,
18
+ "PrerequisiteFailed" => PREREQUISITE_FAILED
19
+ }.freeze
20
+
21
+ ERROR_CODES = {
22
+ "FlagNotFound" => ErrorCode::FLAG_NOT_FOUND,
23
+ "Error" => ErrorCode::GENERAL
24
+ }.freeze
25
+
26
+ ERROR_MESSAGES = {
27
+ "FlagNotFound" => "Flag '%s' was not found",
28
+ "Error" => "Flag '%s' could not be evaluated"
29
+ }.freeze
30
+
31
+ # A double represents every integer exactly only up to 2**53. Beyond it a
32
+ # whole-looking float may be a rounded neighbor of the number that was
33
+ # meant, so it is refused rather than handed back silently rounded.
34
+ MAX_EXACT_INTEGER = 2**53
35
+
36
+ MISMATCH = Object.new.freeze
37
+ private_constant :MISMATCH
38
+
39
+ module_function
40
+
41
+ def to_resolution(flag_key, detail, default_value, type)
42
+ error_code = ERROR_CODES[detail.reason]
43
+ if error_code
44
+ # The caller's default, never detail.value: an error detail can carry a
45
+ # wrong-typed off variation (a failed prerequisite chain), which is not
46
+ # what the OpenFeature error contract prescribes. The type guard is
47
+ # skipped for the same reason; it would mislabel the error.
48
+ return error(default_value, error_code, format(ERROR_MESSAGES.fetch(detail.reason), flag_key))
49
+ end
50
+
51
+ value = coerce(detail.value, type)
52
+ if value.equal?(MISMATCH)
53
+ return error(default_value, ErrorCode::TYPE_MISMATCH,
54
+ "Flag '#{flag_key}' did not resolve to #{article(type)} #{type} value")
55
+ end
56
+
57
+ ResolutionDetails.new(
58
+ value: value,
59
+ reason: REASONS.fetch(detail.reason, Reason::UNKNOWN),
60
+ variant: detail.variation_key,
61
+ flag_metadata: metadata(detail)
62
+ )
63
+ end
64
+
65
+ def error(default_value, error_code, message)
66
+ ResolutionDetails.new(value: default_value, reason: Reason::ERROR,
67
+ error_code: error_code, error_message: message)
68
+ end
69
+
70
+ def article(type)
71
+ type.to_s.match?(/\A[aeiou]/i) ? "an" : "a"
72
+ end
73
+
74
+ def metadata(detail)
75
+ { "ruleId" => detail.rule_id, "prerequisiteKey" => detail.prerequisite_key }.compact
76
+ end
77
+
78
+ def coerce(value, type)
79
+ case type
80
+ when :boolean then value == true || value == false ? value : MISMATCH
81
+ when :string then value.is_a?(String) ? value : MISMATCH
82
+ when :number then value.is_a?(Numeric) ? value : MISMATCH
83
+ when :integer then to_integer(value)
84
+ # The OpenFeature client checks is_a?(Float) strictly, so an integral
85
+ # flag value must be widened here or the client reports a mismatch.
86
+ when :float then value.is_a?(Integer) ? value.to_f : (value.is_a?(Float) ? value : MISMATCH)
87
+ when :object then value.is_a?(Hash) || value.is_a?(Array) ? value : MISMATCH
88
+ else raise ArgumentError, "unknown OpenFeature type #{type.inspect}"
89
+ end
90
+ end
91
+
92
+ # JSON does not distinguish 1 from 1.0, and the engine renders whole floats
93
+ # with a decimal part, so a whole Float is an integer here and is narrowed.
94
+ def to_integer(value)
95
+ return value if value.is_a?(Integer)
96
+ return MISMATCH unless value.is_a?(Float) && value.finite? && value == value.truncate
97
+ return MISMATCH if value.abs > MAX_EXACT_INTEGER
98
+
99
+ value.to_i
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,7 @@
1
+ module Featureflip
2
+ module OpenFeature
3
+ # Rewritten from the release tag at publish time; the committed value is
4
+ # convention, the tag is what ships.
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require "featureflip"
2
+ require "open_feature/sdk"
3
+
4
+ require_relative "featureflip/open_feature/version"
5
+ require_relative "featureflip/open_feature/context_mapping"
6
+ require_relative "featureflip/open_feature/resolution_mapping"
7
+ require_relative "featureflip/open_feature/provider"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: featureflip-openfeature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Featureflip
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: featureflip
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: openfeature-sdk
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '0.7'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0.5'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.13'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.13'
61
+ description: Use Featureflip feature flags through the OpenFeature Ruby SDK.
62
+ email:
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - CHANGELOG.md
68
+ - LICENSE
69
+ - README.md
70
+ - lib/featureflip-openfeature.rb
71
+ - lib/featureflip/open_feature/context_mapping.rb
72
+ - lib/featureflip/open_feature/provider.rb
73
+ - lib/featureflip/open_feature/resolution_mapping.rb
74
+ - lib/featureflip/open_feature/version.rb
75
+ homepage: https://featureflip.io/docs/integrations/openfeature/
76
+ licenses:
77
+ - Apache-2.0
78
+ metadata:
79
+ homepage_uri: https://featureflip.io/docs/integrations/openfeature/
80
+ documentation_uri: https://featureflip.io/docs/integrations/openfeature/
81
+ source_code_uri: https://github.com/canopy-labs/featureflip-ruby-openfeature
82
+ changelog_uri: https://github.com/canopy-labs/featureflip-ruby-openfeature/blob/master/CHANGELOG.md
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 3.2.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.5.22
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: OpenFeature provider for Featureflip
102
+ test_files: []