featbit-openfeature-provider 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 +7 -0
- data/CHANGELOG.md +6 -0
- data/LICENSE +201 -0
- data/README.md +178 -0
- data/lib/featbit/openfeature/context_converter.rb +39 -0
- data/lib/featbit/openfeature/details_converter.rb +49 -0
- data/lib/featbit/openfeature/provider.rb +83 -0
- data/lib/featbit/openfeature/version.rb +7 -0
- data/lib/featbit/openfeature.rb +8 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a7fea16e3a1ffe039ee30b859b045a6c975777bd68bf77b253f0cff4899dbfe1
|
|
4
|
+
data.tar.gz: 92707172ac8ec186287d9e8cf9409aec243815477cbdaa475e25b31996d522b6
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ae0fc4db8250f5ce24c6d403a0181e7de3581ba0074ca811f209cdea60430f6bf1b08f283d8bce11239d0ba1b8e4ec8ba9ac881ccfb5ac4f4bc8fc4d409dbdcc
|
|
7
|
+
data.tar.gz: 6b52a31ae789bcd085e4b783c0a4b376196c8f209dc07c6bd26602480fd92b1e40d502edc3cf62a406d08db79be86c0f37d85665a290af6762b640a82d274da8
|
data/CHANGELOG.md
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# FeatBit OpenFeature Provider for Ruby
|
|
2
|
+
|
|
3
|
+
Use [FeatBit](https://featbit.co) feature flags through the
|
|
4
|
+
[OpenFeature Ruby SDK](https://openfeature.dev/docs/reference/sdks/server/ruby/).
|
|
5
|
+
Evaluation and analytics are handled by the [FeatBit Ruby Server SDK](https://github.com/featbit/featbit-ruby-server-sdk).
|
|
6
|
+
|
|
7
|
+
## Requirements and installation
|
|
8
|
+
|
|
9
|
+
Ruby 3.4 or newer. This provider uses `openfeature-sdk ~> 0.6.5` and
|
|
10
|
+
`featbit-server-sdk ~> 0.1.0`.
|
|
11
|
+
|
|
12
|
+
Add the provider to your Gemfile:
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
gem "featbit-openfeature-provider", "~> 0.1.0"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Run `bundle install`. For local development, use `path:` pointing at this checkout.
|
|
19
|
+
|
|
20
|
+
## Quick start
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
require "featbit/openfeature"
|
|
24
|
+
|
|
25
|
+
provider = FeatBit::OpenFeature::Provider.new(
|
|
26
|
+
FeatBit::Options.new(
|
|
27
|
+
env_secret: ENV.fetch("FEATBIT_ENV_SECRET"),
|
|
28
|
+
streaming_url: ENV.fetch("FEATBIT_STREAMING_URL", "wss://app-eval.featbit.co"),
|
|
29
|
+
event_url: ENV.fetch("FEATBIT_EVENT_URL", "https://app-eval.featbit.co"),
|
|
30
|
+
start_wait: 5
|
|
31
|
+
)
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
# Raises OpenFeature::SDK::ProviderInitializationError if FeatBit is not ready.
|
|
35
|
+
OpenFeature::SDK.configure { |config| config.set_provider_and_wait(provider) }
|
|
36
|
+
client = OpenFeature::SDK.build_client
|
|
37
|
+
context = OpenFeature::SDK::EvaluationContext.new(
|
|
38
|
+
targeting_key: "user-123", name: "Alice", country: "FR", plan: "pro"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
enabled = client.fetch_boolean_value(
|
|
42
|
+
flag_key: "new-checkout", default_value: false, evaluation_context: context
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
details = client.fetch_boolean_details(
|
|
46
|
+
flag_key: "new-checkout", default_value: false, evaluation_context: context
|
|
47
|
+
)
|
|
48
|
+
puts "value=#{enabled} variant=#{details.variant} reason=#{details.reason} error=#{details.error_code}"
|
|
49
|
+
|
|
50
|
+
# Call during graceful application shutdown to close connections and flush events.
|
|
51
|
+
OpenFeature::SDK.shutdown
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Create one provider per application process and reuse the OpenFeature client.
|
|
55
|
+
For prefork servers, create the provider in each worker after fork.
|
|
56
|
+
The FeatBit client starts during provider construction and waits up to `start_wait`.
|
|
57
|
+
OpenFeature calls `init` to check readiness; `config.set_provider(provider)` offers
|
|
58
|
+
nonblocking OpenFeature registration if you do not need to wait for registration.
|
|
59
|
+
|
|
60
|
+
An existing client can be supplied with
|
|
61
|
+
`FeatBit::OpenFeature::Provider.new(client: featbit_client)`. The provider takes
|
|
62
|
+
ownership: replacement or OpenFeature shutdown closes that client. Do not share
|
|
63
|
+
one FeatBit client between independently managed providers. A closed provider
|
|
64
|
+
cannot restart; construct a new instance.
|
|
65
|
+
|
|
66
|
+
## Evaluation context
|
|
67
|
+
|
|
68
|
+
| OpenFeature field | FeatBit user field |
|
|
69
|
+
| --- | --- |
|
|
70
|
+
| `targeting_key` | Required non-empty string user key |
|
|
71
|
+
| `name` | Optional string user name |
|
|
72
|
+
| Other fields | Custom attributes, with their values preserved |
|
|
73
|
+
|
|
74
|
+
Missing or empty targeting keys return `TARGETING_KEY_MISSING`. Non-string keys,
|
|
75
|
+
non-string names, and unsupported context objects return `INVALID_CONTEXT`.
|
|
76
|
+
The provider does not invent anonymous identities or use `key` as a fallback.
|
|
77
|
+
Built-in FeatBit user properties retain the SDK's precedence over custom attributes.
|
|
78
|
+
Contexts are not modified. Custom attribute matching and analytics serialization
|
|
79
|
+
follow the FeatBit SDK's behavior; use scalar attributes for targeting rules.
|
|
80
|
+
|
|
81
|
+
OpenFeature merges API, client, transaction, and invocation contexts before calling
|
|
82
|
+
the provider. A targeting key may therefore be set at any of those scopes.
|
|
83
|
+
|
|
84
|
+
## Values and resolution details
|
|
85
|
+
|
|
86
|
+
The provider supports `fetch_boolean_value`, `fetch_string_value`,
|
|
87
|
+
`fetch_number_value`, `fetch_integer_value`, `fetch_float_value`, and
|
|
88
|
+
`fetch_object_value`, plus OpenFeature's corresponding `fetch_*_details` methods.
|
|
89
|
+
Objects may be hashes or arrays. Integer evaluation truncates numeric values toward
|
|
90
|
+
zero; float evaluation converts numeric values to Float. Incompatible types return
|
|
91
|
+
the caller's default with `TYPE_MISMATCH`.
|
|
92
|
+
|
|
93
|
+
Successful details include the FeatBit variation ID as `variant`, plus
|
|
94
|
+
`featbit.reason`, `featbit.flag_name` (when available), and `featbit.in_experiment`
|
|
95
|
+
in flag metadata.
|
|
96
|
+
|
|
97
|
+
| FeatBit reason/error | OpenFeature reason/error |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| Flag off | `DISABLED` |
|
|
100
|
+
| User target or rule match | `TARGETING_MATCH` |
|
|
101
|
+
| Fallthrough | `DEFAULT` |
|
|
102
|
+
| Client not ready | `ERROR` / `PROVIDER_NOT_READY` |
|
|
103
|
+
| Flag not found | `ERROR` / `FLAG_NOT_FOUND` |
|
|
104
|
+
| Wrong type | `ERROR` / `TYPE_MISMATCH` |
|
|
105
|
+
| User not specified | `ERROR` / `TARGETING_KEY_MISSING` |
|
|
106
|
+
| Other evaluation errors | `ERROR` / `GENERAL` |
|
|
107
|
+
|
|
108
|
+
Errors return the caller's default without a variant. FeatBit does not expose a
|
|
109
|
+
separate parse error category, so malformed flag evaluation errors map to `GENERAL`.
|
|
110
|
+
Evaluation analytics are produced by FeatBit's `variation_detail`; a successful
|
|
111
|
+
FeatBit evaluation can record an event even if the provider subsequently rejects
|
|
112
|
+
its type.
|
|
113
|
+
|
|
114
|
+
## Events and tracking
|
|
115
|
+
|
|
116
|
+
The provider forwards FeatBit readiness, interrupted synchronization, and flag
|
|
117
|
+
changes as `PROVIDER_READY`, `PROVIDER_STALE`, and
|
|
118
|
+
`PROVIDER_CONFIGURATION_CHANGED` (with `flags_changed`). Failed or closed clients
|
|
119
|
+
emit `PROVIDER_ERROR`. Cached flags remain available during synchronization
|
|
120
|
+
interruptions. Shutdown removes the provider's listeners.
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
client.add_handler(OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED) do |event|
|
|
124
|
+
puts event[:flags_changed].inspect
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
client.track(
|
|
128
|
+
"purchase",
|
|
129
|
+
evaluation_context: context,
|
|
130
|
+
tracking_event_details: OpenFeature::SDK::TrackingEventDetails.new(value: 19.95)
|
|
131
|
+
)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Tracking requires a valid targeting key. Omitted metric values default to `1.0`.
|
|
135
|
+
Extra tracking fields are ignored because the FeatBit tracking API accepts only a
|
|
136
|
+
user, event name, and numeric value. Invalid tracking contexts produce no event.
|
|
137
|
+
|
|
138
|
+
## Offline use
|
|
139
|
+
|
|
140
|
+
```ruby
|
|
141
|
+
require "json"
|
|
142
|
+
|
|
143
|
+
provider = FeatBit::OpenFeature::Provider.new(
|
|
144
|
+
FeatBit::Options.new(offline: true, bootstrap: JSON.parse(File.read("bootstrap.json")))
|
|
145
|
+
)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Supply a full FeatBit data-sync payload containing feature flags and segments.
|
|
149
|
+
Offline mode disables networking and analytics. Without initialized bootstrap
|
|
150
|
+
data the provider reports `PROVIDER_NOT_READY`.
|
|
151
|
+
|
|
152
|
+
## Console example
|
|
153
|
+
|
|
154
|
+
The console example evaluates a boolean flag against a FeatBit environment:
|
|
155
|
+
|
|
156
|
+
```sh
|
|
157
|
+
FEATBIT_ENV_SECRET="your-environment-secret" bundle exec ruby examples/console.rb
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Enter a boolean flag key at the prompt to evaluate it. The prompt repeats so
|
|
161
|
+
multiple flags can be evaluated in one session. Enter `exit`, `quit`, or `q`
|
|
162
|
+
to stop the application.
|
|
163
|
+
|
|
164
|
+
Set `FEATBIT_TARGETING_KEY` to evaluate for a different user. The streaming and
|
|
165
|
+
event URLs default to FeatBit Cloud and can be overridden with
|
|
166
|
+
`FEATBIT_STREAMING_URL` and `FEATBIT_EVENT_URL`.
|
|
167
|
+
|
|
168
|
+
## Development
|
|
169
|
+
|
|
170
|
+
```sh
|
|
171
|
+
bundle install
|
|
172
|
+
bundle exec rake
|
|
173
|
+
gem build featbit-openfeature-provider.gemspec
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Tests run against the released SDK gems with real offline FeatBit evaluation,
|
|
177
|
+
including OpenFeature context merging, defaults, details, events, and tracking.
|
|
178
|
+
No FeatBit service or environment secret is required.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FeatBit
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class ContextConverter
|
|
6
|
+
class InvalidContext < StandardError
|
|
7
|
+
attr_reader :error_code
|
|
8
|
+
|
|
9
|
+
def initialize(error_code, message)
|
|
10
|
+
@error_code = error_code
|
|
11
|
+
super(message)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.convert(context)
|
|
16
|
+
codes = ::OpenFeature::SDK::Provider::ErrorCode
|
|
17
|
+
unless context.nil? || context.is_a?(::OpenFeature::SDK::EvaluationContext)
|
|
18
|
+
raise InvalidContext.new(codes::INVALID_CONTEXT, "Expected an OpenFeature evaluation context")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
key = context&.targeting_key
|
|
22
|
+
if key.nil? || key == ""
|
|
23
|
+
raise InvalidContext.new(codes::TARGETING_KEY_MISSING, "A non-empty targeting_key is required")
|
|
24
|
+
end
|
|
25
|
+
unless key.is_a?(String)
|
|
26
|
+
raise InvalidContext.new(codes::INVALID_CONTEXT, "targeting_key must be a string")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
name = context.field("name")
|
|
30
|
+
unless name.nil? || name.is_a?(String)
|
|
31
|
+
raise InvalidContext.new(codes::INVALID_CONTEXT, "name must be a string")
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
custom = context.fields.reject { |field, _| %w[targeting_key name].include?(field) }
|
|
35
|
+
FeatBit::User.new(key, name: name, custom: custom)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FeatBit
|
|
4
|
+
module OpenFeature
|
|
5
|
+
class DetailsConverter
|
|
6
|
+
API = ::OpenFeature::SDK::Provider
|
|
7
|
+
ERROR_CODES = {
|
|
8
|
+
client_not_ready: API::ErrorCode::PROVIDER_NOT_READY,
|
|
9
|
+
flag_not_found: API::ErrorCode::FLAG_NOT_FOUND,
|
|
10
|
+
user_not_specified: API::ErrorCode::TARGETING_KEY_MISSING,
|
|
11
|
+
wrong_type: API::ErrorCode::TYPE_MISMATCH
|
|
12
|
+
}.freeze
|
|
13
|
+
REASONS = {
|
|
14
|
+
"flag off" => API::Reason::DISABLED,
|
|
15
|
+
"target match" => API::Reason::TARGETING_MATCH,
|
|
16
|
+
"rule match" => API::Reason::TARGETING_MATCH,
|
|
17
|
+
"fall through all rules" => API::Reason::DEFAULT
|
|
18
|
+
}.freeze
|
|
19
|
+
TYPES = {
|
|
20
|
+
boolean: [TrueClass, FalseClass], string: [String], number: [Numeric],
|
|
21
|
+
integer: [Numeric], float: [Numeric], object: [Hash, Array]
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
def self.error(default_value, code, message)
|
|
25
|
+
API::ResolutionDetails.new(value: default_value, reason: API::Reason::ERROR,
|
|
26
|
+
error_code: code, error_message: message)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.convert(detail, type, default_value)
|
|
30
|
+
unless detail.success?
|
|
31
|
+
return error(default_value, ERROR_CODES.fetch(detail.error_kind, API::ErrorCode::GENERAL),
|
|
32
|
+
detail.error_message || detail.reason)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
value = detail.value
|
|
36
|
+
unless TYPES.fetch(type).any? { |klass| value.is_a?(klass) }
|
|
37
|
+
return error(default_value, API::ErrorCode::TYPE_MISMATCH, "Flag value is not of type #{type}")
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
value = value.to_i if type == :integer
|
|
41
|
+
value = value.to_f if type == :float
|
|
42
|
+
metadata = { "featbit.reason" => detail.reason, "featbit.in_experiment" => detail.send_to_experiment == true }
|
|
43
|
+
metadata["featbit.flag_name"] = detail.flag_name unless detail.flag_name.nil?
|
|
44
|
+
API::ResolutionDetails.new(value: value, reason: REASONS.fetch(detail.reason, API::Reason::UNKNOWN),
|
|
45
|
+
variant: detail.variation_id&.to_s, flag_metadata: metadata)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FeatBit
|
|
4
|
+
module OpenFeature
|
|
5
|
+
# Owns the supplied or newly created client and closes it on shutdown.
|
|
6
|
+
class Provider
|
|
7
|
+
include ::OpenFeature::SDK::Provider::EventEmitter
|
|
8
|
+
|
|
9
|
+
attr_reader :client, :metadata
|
|
10
|
+
|
|
11
|
+
def initialize(options = nil, client: nil)
|
|
12
|
+
raise ArgumentError, "Provide options or client, not both" if options && client
|
|
13
|
+
unless client || options.is_a?(FeatBit::Options)
|
|
14
|
+
raise ArgumentError, "FeatBit::Options or a FeatBit client is required"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
@client = client || FeatBit::Client.new(options)
|
|
18
|
+
@metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: "FeatBit").freeze
|
|
19
|
+
@status_listener = @client.status_provider.add_listener { |status, message| status_changed(status, message) }
|
|
20
|
+
@flag_listener = @client.add_flag_change_listener do |key|
|
|
21
|
+
emit_event(::OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, flags_changed: [key])
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def init(_evaluation_context = nil)
|
|
26
|
+
return if client.initialized? && [FeatBit::Status::READY, FeatBit::Status::INTERRUPTED,
|
|
27
|
+
FeatBit::Status::OFFLINE].include?(client.status_provider.status)
|
|
28
|
+
|
|
29
|
+
raise ContextConverter::InvalidContext.new(
|
|
30
|
+
::OpenFeature::SDK::Provider::ErrorCode::PROVIDER_NOT_READY, "FeatBit client is not ready"
|
|
31
|
+
)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def shutdown
|
|
35
|
+
client.status_provider.remove_listener(@status_listener)
|
|
36
|
+
client.remove_flag_change_listener(@flag_listener)
|
|
37
|
+
detach
|
|
38
|
+
client.close
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
%i[boolean string number integer float object].each do |type|
|
|
42
|
+
define_method("fetch_#{type}_value") do |flag_key:, default_value:, evaluation_context: nil|
|
|
43
|
+
resolve(type, flag_key, default_value, evaluation_context)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def track(tracking_event_name, evaluation_context: nil, tracking_event_details: nil)
|
|
48
|
+
user = ContextConverter.convert(evaluation_context)
|
|
49
|
+
client.track(user, tracking_event_name, tracking_event_details&.value || 1.0)
|
|
50
|
+
rescue ContextConverter::InvalidContext
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def resolve(type, key, default_value, context)
|
|
57
|
+
user = ContextConverter.convert(context)
|
|
58
|
+
DetailsConverter.convert(client.variation_detail(key, user, default_value), type, default_value)
|
|
59
|
+
rescue ContextConverter::InvalidContext => e
|
|
60
|
+
DetailsConverter.error(default_value, e.error_code, e.message)
|
|
61
|
+
rescue StandardError
|
|
62
|
+
DetailsConverter.error(default_value, ::OpenFeature::SDK::Provider::ErrorCode::GENERAL,
|
|
63
|
+
"FeatBit evaluation failed")
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def status_changed(status, message)
|
|
67
|
+
events = ::OpenFeature::SDK::ProviderEvent
|
|
68
|
+
if status == FeatBit::Status::OFFLINE && client.initialized?
|
|
69
|
+
status = FeatBit::Status::READY
|
|
70
|
+
end
|
|
71
|
+
case status
|
|
72
|
+
when FeatBit::Status::READY
|
|
73
|
+
emit_event(events::PROVIDER_READY)
|
|
74
|
+
when FeatBit::Status::INTERRUPTED
|
|
75
|
+
emit_event(events::PROVIDER_STALE, message: message)
|
|
76
|
+
when FeatBit::Status::STARTING, FeatBit::Status::OFFLINE, FeatBit::Status::FAILED, FeatBit::Status::CLOSED
|
|
77
|
+
emit_event(events::PROVIDER_ERROR, error_code: ::OpenFeature::SDK::Provider::ErrorCode::PROVIDER_NOT_READY,
|
|
78
|
+
message: message || "FeatBit client is not ready")
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: featbit-openfeature-provider
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- FeatBit
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: featbit-server-sdk
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.1.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.1.0
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: openfeature-sdk
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: 0.6.5
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: 0.6.5
|
|
40
|
+
description: Evaluate FeatBit feature flags through the OpenFeature Ruby SDK.
|
|
41
|
+
email:
|
|
42
|
+
- contact@featbit.co
|
|
43
|
+
executables: []
|
|
44
|
+
extensions: []
|
|
45
|
+
extra_rdoc_files: []
|
|
46
|
+
files:
|
|
47
|
+
- CHANGELOG.md
|
|
48
|
+
- LICENSE
|
|
49
|
+
- README.md
|
|
50
|
+
- lib/featbit/openfeature.rb
|
|
51
|
+
- lib/featbit/openfeature/context_converter.rb
|
|
52
|
+
- lib/featbit/openfeature/details_converter.rb
|
|
53
|
+
- lib/featbit/openfeature/provider.rb
|
|
54
|
+
- lib/featbit/openfeature/version.rb
|
|
55
|
+
homepage: https://www.featbit.co/
|
|
56
|
+
licenses:
|
|
57
|
+
- Apache-2.0
|
|
58
|
+
metadata:
|
|
59
|
+
source_code_uri: https://github.com/featbit/openfeature-provider-ruby-server
|
|
60
|
+
bug_tracker_uri: https://github.com/featbit/openfeature-provider-ruby-server/issues
|
|
61
|
+
rubygems_mfa_required: 'true'
|
|
62
|
+
rdoc_options: []
|
|
63
|
+
require_paths:
|
|
64
|
+
- lib
|
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
66
|
+
requirements:
|
|
67
|
+
- - ">="
|
|
68
|
+
- !ruby/object:Gem::Version
|
|
69
|
+
version: '3.4'
|
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
|
+
requirements:
|
|
72
|
+
- - ">="
|
|
73
|
+
- !ruby/object:Gem::Version
|
|
74
|
+
version: '0'
|
|
75
|
+
requirements: []
|
|
76
|
+
rubygems_version: 3.6.9
|
|
77
|
+
specification_version: 4
|
|
78
|
+
summary: FeatBit OpenFeature provider for Ruby server applications
|
|
79
|
+
test_files: []
|