zenmanage 1.0.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 +27 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +22 -0
- data/LICENSE +21 -0
- data/README.md +382 -0
- data/SECURITY.md +9 -0
- data/examples/caching.rb +58 -0
- data/examples/context_based_flags.rb +45 -0
- data/examples/defaults.rb +62 -0
- data/examples/percentage_rollouts.rb +37 -0
- data/examples/rails_integration.rb +74 -0
- data/examples/simple_flags.rb +41 -0
- data/lib/generators/zenmanage/install/install_generator.rb +18 -0
- data/lib/generators/zenmanage/install/templates/initializer.rb +23 -0
- data/lib/zenmanage/api_client.rb +264 -0
- data/lib/zenmanage/attribute.rb +55 -0
- data/lib/zenmanage/cache/base.rb +46 -0
- data/lib/zenmanage/cache/filesystem_cache.rb +88 -0
- data/lib/zenmanage/cache/memory_cache.rb +63 -0
- data/lib/zenmanage/cache/null_cache.rb +22 -0
- data/lib/zenmanage/config_builder.rb +215 -0
- data/lib/zenmanage/configuration.rb +118 -0
- data/lib/zenmanage/context.rb +125 -0
- data/lib/zenmanage/defaults_collection.rb +68 -0
- data/lib/zenmanage/errors.rb +21 -0
- data/lib/zenmanage/flag.rb +200 -0
- data/lib/zenmanage/flag_manager.rb +197 -0
- data/lib/zenmanage/hash_access.rb +72 -0
- data/lib/zenmanage/rails/controller_helpers.rb +85 -0
- data/lib/zenmanage/rails/middleware.rb +34 -0
- data/lib/zenmanage/rails/railtie.rb +60 -0
- data/lib/zenmanage/rollout.rb +45 -0
- data/lib/zenmanage/rollout_bucket.rb +40 -0
- data/lib/zenmanage/rule.rb +142 -0
- data/lib/zenmanage/rule_engine.rb +156 -0
- data/lib/zenmanage/rule_store.rb +164 -0
- data/lib/zenmanage/target.rb +37 -0
- data/lib/zenmanage/version.rb +6 -0
- data/lib/zenmanage/webhook.rb +89 -0
- data/lib/zenmanage.rb +183 -0
- metadata +216 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c776ddeed5030b2d002c9bde97c766c3cfad5b6e3fb5075543e70b20f4938c0a
|
|
4
|
+
data.tar.gz: 79648f69ac2c1124909648c4c81da2c80242d82baf3e0ebcd58dd20e88b43516
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8556ac4a171ee18d27a8573075d6931634167ce137863f122279ccf7bd4c0d8348576619cad43a5508cdc27bdfcac2c36191fdc83e96557df74e104bd4538db4
|
|
7
|
+
data.tar.gz: 247162172f75ed559bbf0e7310dc38b73a959aebb09ce07a4a2ff8cb245547424b80fdbc9cdc3f7263fff965587b0f7f4a27fc4e6d44405cc6e59d8c6d45eed1
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to the `zenmanage` Ruby gem are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format follows [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [1.0.0] - 2026-11-29
|
|
11
|
+
|
|
12
|
+
First release of the official Zenmanage SDK for Ruby.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- Local evaluation of boolean, string, number and JSON flags, with typed accessors that never raise on the wrong type.
|
|
17
|
+
- Targeting by context, segment and attribute, matching the Zenmanage API's rule evaluation.
|
|
18
|
+
- Percentage rollouts, bucketed the same way as the API and the other SDKs.
|
|
19
|
+
- Inline defaults and `DefaultsCollection`, used when a flag is missing or the API can't be reached.
|
|
20
|
+
- Usage reporting in the background, with the context and default value.
|
|
21
|
+
- Memory, filesystem, null and custom cache backends. Rules reload after `cache_ttl`, and a failed reload keeps the
|
|
22
|
+
rules already loaded.
|
|
23
|
+
- Rails integration: auto-configuration, `config.zenmanage` options, `feature_enabled?` and `zen_flags` in controllers
|
|
24
|
+
and views, Rack middleware and the `zenmanage:install` generator.
|
|
25
|
+
- `Zenmanage::Webhook::Endpoint`, which refreshes the rules when it receives a signed Zenmanage webhook.
|
|
26
|
+
- Requests identify themselves as `zenmanage-ruby`, or `zenmanage-rails` under Rails.
|
|
27
|
+
- Supports Ruby 3.2 and later. No runtime gem dependencies.
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, caste, color, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behavior include:
|
|
20
|
+
|
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or advances of any kind
|
|
22
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
23
|
+
* Public or private harassment
|
|
24
|
+
* Publishing others' private information, such as a physical or email address, without their explicit permission
|
|
25
|
+
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
26
|
+
|
|
27
|
+
## Enforcement Responsibilities
|
|
28
|
+
|
|
29
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
|
30
|
+
|
|
31
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
32
|
+
|
|
33
|
+
## Scope
|
|
34
|
+
|
|
35
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
36
|
+
|
|
37
|
+
## Enforcement
|
|
38
|
+
|
|
39
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at hello@zenmanage.com. All complaints will be reviewed and investigated promptly and fairly.
|
|
40
|
+
|
|
41
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
|
42
|
+
|
|
43
|
+
## Enforcement Guidelines
|
|
44
|
+
|
|
45
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
|
46
|
+
|
|
47
|
+
### 1. Correction
|
|
48
|
+
|
|
49
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
|
50
|
+
|
|
51
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
|
52
|
+
|
|
53
|
+
### 2. Warning
|
|
54
|
+
|
|
55
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
|
56
|
+
|
|
57
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
58
|
+
|
|
59
|
+
### 3. Temporary Ban
|
|
60
|
+
|
|
61
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
|
62
|
+
|
|
63
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
64
|
+
|
|
65
|
+
### 4. Permanent Ban
|
|
66
|
+
|
|
67
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
68
|
+
|
|
69
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
|
70
|
+
|
|
71
|
+
## Attribution
|
|
72
|
+
|
|
73
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
74
|
+
|
|
75
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
76
|
+
|
|
77
|
+
For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations].
|
|
78
|
+
|
|
79
|
+
[homepage]: https://www.contributor-covenant.org
|
|
80
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
81
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
82
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
83
|
+
[translations]: https://www.contributor-covenant.org/translations
|
|
84
|
+
|
data/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Contributing to Zenmanage SDKs
|
|
2
|
+
|
|
3
|
+
Thanks for your interest in contributing to a Zenmanage SDK. Contributions are welcome from the community.
|
|
4
|
+
|
|
5
|
+
## Reporting bugs and requesting features
|
|
6
|
+
|
|
7
|
+
Open an issue in the repository for the SDK you're using. Include the SDK version, your runtime version, a minimal reproduction, and what you expected to happen.
|
|
8
|
+
|
|
9
|
+
## Submitting changes
|
|
10
|
+
|
|
11
|
+
1. Fork the repository and create a branch from `main`.
|
|
12
|
+
2. Make your change, and add or update tests where the repository has them.
|
|
13
|
+
3. Run the build and test commands documented in the repository's README or CI configuration.
|
|
14
|
+
4. Open a pull request against `main` that describes the change and why it's needed.
|
|
15
|
+
|
|
16
|
+
## Code of conduct
|
|
17
|
+
|
|
18
|
+
Participation in Zenmanage projects is governed by the [Code of Conduct](CODE_OF_CONDUCT.md).
|
|
19
|
+
|
|
20
|
+
## License
|
|
21
|
+
|
|
22
|
+
By contributing, you agree that your contributions are licensed under the same license as the repository you contribute to (MIT for the Zenmanage SDKs).
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zenmanage
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
# Zenmanage Ruby SDK
|
|
2
|
+
|
|
3
|
+
[](https://github.com/zenmanage/zenmanage-ruby/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/zenmanage)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://app.codacy.com/gh/zenmanage/zenmanage-ruby/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
|
|
7
|
+
[](https://github.com/zenmanage/zenmanage-ruby/actions/workflows/ci.yml)
|
|
8
|
+
|
|
9
|
+
Feature flags for Ruby and Rails with [Zenmanage](https://zenmanage.com). Your rules are fetched once, cached and
|
|
10
|
+
evaluated inside your process, so checking a flag doesn't cost a network round trip.
|
|
11
|
+
|
|
12
|
+
- Boolean, string, number and JSON flags
|
|
13
|
+
- Targeting by context, segment and attribute
|
|
14
|
+
- Percentage rollouts
|
|
15
|
+
- Defaults for when a flag, or the API, isn't there
|
|
16
|
+
- Memory, filesystem or your own cache
|
|
17
|
+
- Rails integration: controller and view helpers, an install generator and a webhook endpoint
|
|
18
|
+
- No runtime dependencies
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
Add to your `Gemfile`:
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
gem "zenmanage"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Then run `bundle install`, or install it directly with `gem install zenmanage`.
|
|
29
|
+
|
|
30
|
+
Requirements: Ruby 3.2 or later.
|
|
31
|
+
|
|
32
|
+
## Key compatibility
|
|
33
|
+
|
|
34
|
+
This is a server-side SDK. It takes your environment's **server key** (it starts with `srv_`). Client keys (`cli_`) and
|
|
35
|
+
mobile keys (`mob_`) are rejected when the SDK is configured.
|
|
36
|
+
|
|
37
|
+
## Quick start
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require "zenmanage"
|
|
41
|
+
|
|
42
|
+
config = Zenmanage::ConfigBuilder.create
|
|
43
|
+
.with_environment_token(ENV["ZENMANAGE_ENVIRONMENT_TOKEN"])
|
|
44
|
+
.build
|
|
45
|
+
|
|
46
|
+
client = Zenmanage::Client.new(config)
|
|
47
|
+
|
|
48
|
+
if client.flags.single("new-dashboard", false).enabled?
|
|
49
|
+
puts "Show the new dashboard"
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Create one client and reuse it. It holds the loaded rules.
|
|
54
|
+
|
|
55
|
+
## Rails
|
|
56
|
+
|
|
57
|
+
### Setup
|
|
58
|
+
|
|
59
|
+
With `ZENMANAGE_ENVIRONMENT_TOKEN` set, there's nothing else to do: the gem configures itself when your app boots.
|
|
60
|
+
|
|
61
|
+
To keep the settings in your app, generate an initializer:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
bin/rails generate zenmanage:install
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
# config/initializers/zenmanage.rb
|
|
69
|
+
Zenmanage.configure do |config|
|
|
70
|
+
config.environment_token = Rails.application.credentials.zenmanage_environment_token
|
|
71
|
+
config.cache_ttl = 300
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
You can also set single options in `config/application.rb` or an environment file, which applies when you don't call
|
|
76
|
+
`Zenmanage.configure`:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
config.zenmanage.cache_ttl = 300
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Auto-configuration happens after your initializers have run. To read flags while the app is still booting, call
|
|
83
|
+
`Zenmanage.configure` first.
|
|
84
|
+
|
|
85
|
+
Settings you leave out come from the `ZENMANAGE_*` environment variables. The SDK logs to `Rails.logger`. Without a
|
|
86
|
+
token, your app still boots: it logs a warning and `feature_enabled?` returns the default.
|
|
87
|
+
|
|
88
|
+
### Controllers and views
|
|
89
|
+
|
|
90
|
+
`feature_enabled?` and `zen_flags` are available in `ActionController::Base` and `ActionController::API` controllers,
|
|
91
|
+
and in views:
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
class ProductsController < ApplicationController
|
|
95
|
+
def show
|
|
96
|
+
@new_layout = feature_enabled?("new-layout")
|
|
97
|
+
@checkout = zen_flags.single("checkout-flow", "control").as_string
|
|
98
|
+
@page_size = zen_flags.single("page-size", 20).as_number
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```erb
|
|
104
|
+
<% if feature_enabled?("promo-banner") %>
|
|
105
|
+
<%= render "shared/promo_banner" %>
|
|
106
|
+
<% end %>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Outside a controller (jobs, mailers, models), use `Zenmanage.flags`:
|
|
110
|
+
|
|
111
|
+
```ruby
|
|
112
|
+
Zenmanage.flags
|
|
113
|
+
.with_context(Zenmanage::Context.single("user", user.id.to_s))
|
|
114
|
+
.single("new-export", false)
|
|
115
|
+
.enabled?
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Who flags are evaluated for
|
|
119
|
+
|
|
120
|
+
If a controller has a `current_user`, flags are evaluated for a `user` context with its `id` and, if it has one, its
|
|
121
|
+
`name`. Both are sent to Zenmanage with usage reports.
|
|
122
|
+
|
|
123
|
+
To send something else, or nothing, override the private `zenmanage_context` method:
|
|
124
|
+
|
|
125
|
+
```ruby
|
|
126
|
+
class ApplicationController < ActionController::Base
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
def zenmanage_context
|
|
130
|
+
return unless current_user
|
|
131
|
+
|
|
132
|
+
Zenmanage::Context.new(
|
|
133
|
+
"user",
|
|
134
|
+
identifier: current_user.id.to_s, # no name sent
|
|
135
|
+
attributes: [Zenmanage::Attribute.new("plan", [current_user.plan])],
|
|
136
|
+
)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Return `nil` to evaluate without a context.
|
|
142
|
+
|
|
143
|
+
### Sinatra and plain Rack
|
|
144
|
+
|
|
145
|
+
Without Rails, add the middleware yourself and read the flags from the Rack environment:
|
|
146
|
+
|
|
147
|
+
```ruby
|
|
148
|
+
use Zenmanage::Rails::Middleware, client
|
|
149
|
+
|
|
150
|
+
get "/" do
|
|
151
|
+
env["zenmanage.flags"].single("new-home", false).enabled? ? "new" : "old"
|
|
152
|
+
end
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Flags and values
|
|
156
|
+
|
|
157
|
+
```ruby
|
|
158
|
+
flags = client.flags
|
|
159
|
+
|
|
160
|
+
flags.single("new-feature").enabled? # boolean flags: true / false
|
|
161
|
+
flags.single("checkout-layout").as_string # string flags
|
|
162
|
+
flags.single("max-results").as_number # number flags
|
|
163
|
+
flags.single("theme-config").as_json # JSON flags: a Hash or an Array
|
|
164
|
+
|
|
165
|
+
flags.all.each { |flag| puts "#{flag.key}: #{flag.value}" }
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
`single` reports the flag's usage; `all` doesn't.
|
|
169
|
+
|
|
170
|
+
### Value types and coercion
|
|
171
|
+
|
|
172
|
+
Calling the "wrong" accessor never raises. Each one reads its own type and returns an empty value for the others. This
|
|
173
|
+
is the same in every Zenmanage SDK.
|
|
174
|
+
|
|
175
|
+
| Flag type | `as_bool` | `as_string` | `as_number` | `as_json` |
|
|
176
|
+
|-----------|-----------|-------------|-------------|-------------------|
|
|
177
|
+
| `boolean` | the value | `""` | `0` | `{}` |
|
|
178
|
+
| `string` | `true` | the value | `0` | `{}` |
|
|
179
|
+
| `number` | `true` | `""` | the value | `{}` |
|
|
180
|
+
| `json` | `true` | `""` | `0` | the decoded value |
|
|
181
|
+
|
|
182
|
+
`as_bool` is `true` for every non-boolean flag, whatever its value. To ask whether a boolean flag is on, use
|
|
183
|
+
`enabled?`, which is `false` for any other type.
|
|
184
|
+
|
|
185
|
+
A JSON flag whose value is a bare scalar (`5`, `"x"`) makes `as_json` return `{}`.
|
|
186
|
+
|
|
187
|
+
In Rails, `as_json` is also what the JSON encoder calls. To send a flag in a response, render its value or
|
|
188
|
+
`flag.to_h`, for example `render json: zen_flags.all.map(&:to_h)`.
|
|
189
|
+
|
|
190
|
+
## Contexts and targeting
|
|
191
|
+
|
|
192
|
+
A context says who you're evaluating for. Targeting rules match its type, identifier and attributes.
|
|
193
|
+
|
|
194
|
+
```ruby
|
|
195
|
+
context = Zenmanage::Context.new("user", identifier: "user-123", name: "Jane Doe")
|
|
196
|
+
context.add_attribute(Zenmanage::Attribute.new("plan", ["pro"]))
|
|
197
|
+
context.add_attribute(Zenmanage::Attribute.new("teams", ["billing", "platform"]))
|
|
198
|
+
|
|
199
|
+
flags = client.flags.with_context(context)
|
|
200
|
+
flags.single("beta-feature", false).enabled?
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
`Zenmanage::Context.single("user", "user-123")` is the short form, and `Zenmanage::Context.from_hash` builds one from a
|
|
204
|
+
Hash:
|
|
205
|
+
|
|
206
|
+
```ruby
|
|
207
|
+
Zenmanage::Context.from_hash(
|
|
208
|
+
"type" => "organization",
|
|
209
|
+
"identifier" => "org-9",
|
|
210
|
+
"attributes" => [{ "key" => "tier", "values" => ["enterprise"] }],
|
|
211
|
+
)
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`with_context` returns a new manager. The one you called it on doesn't change.
|
|
215
|
+
|
|
216
|
+
## Percentage rollouts
|
|
217
|
+
|
|
218
|
+
Start a rollout in Zenmanage. The SDK puts each context in or out of it by its identifier, so the same user gets the
|
|
219
|
+
same answer every time, and raising the percentage only adds users. The bucketing matches the Zenmanage API and the
|
|
220
|
+
other SDKs.
|
|
221
|
+
|
|
222
|
+
```ruby
|
|
223
|
+
flags = client.flags.with_context(Zenmanage::Context.single("user", "user-42"))
|
|
224
|
+
flags.single("new-checkout", false).enabled?
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
A context without an identifier isn't in any rollout.
|
|
228
|
+
|
|
229
|
+
## Defaults
|
|
230
|
+
|
|
231
|
+
A default is used when a flag isn't in your rules, including when the rules couldn't be loaded.
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
flags.single("missing-flag", false) # boolean
|
|
235
|
+
flags.single("missing-flag", "control") # string
|
|
236
|
+
flags.single("missing-flag", 10) # number
|
|
237
|
+
flags.single("missing-flag", { "a" => 1 }) # JSON (an Array works too)
|
|
238
|
+
|
|
239
|
+
defaults = Zenmanage::DefaultsCollection.from_hash("new-search" => false, "page-size" => 20)
|
|
240
|
+
flags = client.flags.with_defaults(defaults)
|
|
241
|
+
flags.single("page-size").as_number
|
|
242
|
+
flags.all # includes defaults for keys your rules don't have
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
An inline default wins over the collection. With neither, `single` raises `Zenmanage::EvaluationError`.
|
|
246
|
+
|
|
247
|
+
## Keeping rules fresh
|
|
248
|
+
|
|
249
|
+
Rules are used for `cache_ttl` seconds (3600 by default), then reloaded by the next flag check. While one thread
|
|
250
|
+
reloads, the others keep using the rules they have. If a reload fails, the current rules stay in use and the SDK tries
|
|
251
|
+
again 30 seconds later (sooner if your TTL is shorter).
|
|
252
|
+
|
|
253
|
+
Reload now:
|
|
254
|
+
|
|
255
|
+
```ruby
|
|
256
|
+
client.flags.refresh_rules
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
`refresh_rules` raises `Zenmanage::FetchRulesError` if the rules can't be fetched, and keeps the ones it had.
|
|
260
|
+
|
|
261
|
+
### Webhooks
|
|
262
|
+
|
|
263
|
+
To apply changes as soon as they're published, add a webhook for your environment in Zenmanage and mount the endpoint:
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
# config/routes.rb
|
|
267
|
+
mount Zenmanage::Webhook::Endpoint.new => "/zenmanage/webhook"
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Set `ZENMANAGE_WEBHOOK_SECRET` to the webhook's signing secret (it starts with `whsec_`). The endpoint checks the
|
|
271
|
+
`X-Zenmanage-Signature` header and refreshes the rules. In Sinatra or Rack:
|
|
272
|
+
|
|
273
|
+
```ruby
|
|
274
|
+
map "/zenmanage/webhook" do
|
|
275
|
+
run Zenmanage::Webhook::Endpoint.new(client: client)
|
|
276
|
+
end
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
A webhook reaches one process. With several (Puma workers, several hosts), the others pick the change up when their
|
|
280
|
+
`cache_ttl` passes.
|
|
281
|
+
|
|
282
|
+
To handle the request yourself, `Zenmanage::Webhook.verify_signature(body, signature_header, secret)` does the check.
|
|
283
|
+
|
|
284
|
+
## Usage reporting
|
|
285
|
+
|
|
286
|
+
`single` tells Zenmanage a flag was used, with the context and the default you passed, so you can see which flags are
|
|
287
|
+
live and which are dead code. Reports are sent in the background and never raise.
|
|
288
|
+
|
|
289
|
+
Turn it off with `with_usage_reporting(false)` (or `config.enable_usage_reporting = false`). To report usage yourself,
|
|
290
|
+
for example after reading a flag from `all`:
|
|
291
|
+
|
|
292
|
+
```ruby
|
|
293
|
+
client.flags.report_usage("new-checkout", context)
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## Configuration
|
|
297
|
+
|
|
298
|
+
```ruby
|
|
299
|
+
config = Zenmanage::ConfigBuilder.create
|
|
300
|
+
.with_environment_token("srv_your_server_key") # required
|
|
301
|
+
.with_cache_ttl(3600) # seconds
|
|
302
|
+
.with_cache_backend("memory") # "memory", "filesystem" or "null"
|
|
303
|
+
.with_cache_directory("/tmp/zenmanage") # required for "filesystem"
|
|
304
|
+
.with_usage_reporting(true)
|
|
305
|
+
.with_api_endpoint("https://api.zenmanage.com")
|
|
306
|
+
.with_logger(Logger.new($stdout))
|
|
307
|
+
.build
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
Or from environment variables, with `Zenmanage::ConfigBuilder.from_environment.build`:
|
|
311
|
+
|
|
312
|
+
| Variable | Setting |
|
|
313
|
+
|------------------------------------|------------------------|
|
|
314
|
+
| `ZENMANAGE_ENVIRONMENT_TOKEN` | server key |
|
|
315
|
+
| `ZENMANAGE_CACHE_TTL` | seconds |
|
|
316
|
+
| `ZENMANAGE_CACHE_BACKEND` | `memory`, `filesystem`, `null` |
|
|
317
|
+
| `ZENMANAGE_CACHE_DIR` | filesystem cache path |
|
|
318
|
+
| `ZENMANAGE_ENABLE_USAGE_REPORTING` | `true` / `false` |
|
|
319
|
+
| `ZENMANAGE_API_ENDPOINT` | API URL |
|
|
320
|
+
| `ZENMANAGE_WEBHOOK_SECRET` | webhook signing secret |
|
|
321
|
+
|
|
322
|
+
### Caching
|
|
323
|
+
|
|
324
|
+
| Backend | What it does |
|
|
325
|
+
|--------------|------------------------------------------------------------------------------------------------|
|
|
326
|
+
| `memory` | Default. Per process, gone on restart. |
|
|
327
|
+
| `filesystem` | Survives restarts and is shared by processes on one machine, such as Puma workers. |
|
|
328
|
+
| `null` | Nothing is cached outside the client. Set `cache_ttl` to `0` to fetch the rules on every check. |
|
|
329
|
+
|
|
330
|
+
To use your own store, pass anything with `get(key)` and `set(key, value, ttl)`:
|
|
331
|
+
|
|
332
|
+
```ruby
|
|
333
|
+
class RailsCache < Zenmanage::Cache::Base
|
|
334
|
+
def get(key) = Rails.cache.read(key)
|
|
335
|
+
def set(key, value, ttl = nil) = Rails.cache.write(key, value, expires_in: ttl)
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
Zenmanage.configure { |config| config.cache = RailsCache.new }
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
## Errors
|
|
342
|
+
|
|
343
|
+
Flag checks don't fail because the API is unreachable: you get your default. These are the errors you can see:
|
|
344
|
+
|
|
345
|
+
| Error | When |
|
|
346
|
+
|------------------------------------|-----------------------------------------------------------------|
|
|
347
|
+
| `Zenmanage::ConfigurationError` | A setting is missing or invalid, or `Zenmanage.flags` is called before configuring |
|
|
348
|
+
| `Zenmanage::EvaluationError` | A flag can't be found and has no default |
|
|
349
|
+
| `Zenmanage::FetchRulesError` | `refresh_rules` couldn't fetch the rules |
|
|
350
|
+
| `Zenmanage::InvalidRulesError` | `refresh_rules` got a response it can't read |
|
|
351
|
+
|
|
352
|
+
All of them subclass `Zenmanage::Error`.
|
|
353
|
+
|
|
354
|
+
## How requests are identified
|
|
355
|
+
|
|
356
|
+
The SDK sends `X-ZEN-CLIENT-AGENT: zenmanage-ruby/<version>`, or `zenmanage-rails/<version>` when Rails is loaded.
|
|
357
|
+
|
|
358
|
+
## Examples
|
|
359
|
+
|
|
360
|
+
The [`examples`](examples) directory has runnable scripts for flags, contexts, defaults, rollouts, caching and Rails.
|
|
361
|
+
|
|
362
|
+
## Development
|
|
363
|
+
|
|
364
|
+
```bash
|
|
365
|
+
git clone https://github.com/zenmanage/zenmanage-ruby
|
|
366
|
+
cd zenmanage-ruby
|
|
367
|
+
bundle install
|
|
368
|
+
|
|
369
|
+
bundle exec rake # tests and lint
|
|
370
|
+
bundle exec rspec # tests only, with a coverage report
|
|
371
|
+
ruby script/release_check.rb # build and check the gem without publishing
|
|
372
|
+
bundle exec yard doc # API docs
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## Contributing
|
|
376
|
+
|
|
377
|
+
Bug reports and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) and the
|
|
378
|
+
[Code of Conduct](CODE_OF_CONDUCT.md). To report a vulnerability, see [SECURITY.md](SECURITY.md).
|
|
379
|
+
|
|
380
|
+
## License
|
|
381
|
+
|
|
382
|
+
[MIT](LICENSE)
|
data/SECURITY.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Reporting a vulnerability
|
|
4
|
+
|
|
5
|
+
Please don't report security vulnerabilities in public issues or pull requests.
|
|
6
|
+
|
|
7
|
+
Report them privately: open the **Security** tab of this repository and choose **Report a vulnerability**.
|
|
8
|
+
|
|
9
|
+
We'll acknowledge reports as soon as we can and keep you updated while we investigate and fix the issue.
|
data/examples/caching.rb
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# examples/caching.rb
|
|
4
|
+
#
|
|
5
|
+
# Shows the three caching backends: memory, filesystem, and null.
|
|
6
|
+
#
|
|
7
|
+
# Usage:
|
|
8
|
+
# ZENMANAGE_ENVIRONMENT_TOKEN=srv_... ruby examples/caching.rb
|
|
9
|
+
|
|
10
|
+
require_relative "../lib/zenmanage"
|
|
11
|
+
require "tmpdir"
|
|
12
|
+
|
|
13
|
+
token = ENV.fetch("ZENMANAGE_ENVIRONMENT_TOKEN", "srv_demo_token")
|
|
14
|
+
|
|
15
|
+
# ── Memory cache (default) ────────────────────────────────────────────────────
|
|
16
|
+
puts "=== Memory Cache ==="
|
|
17
|
+
memory_config = Zenmanage::ConfigBuilder.create
|
|
18
|
+
.with_environment_token(token)
|
|
19
|
+
.with_cache_backend("memory")
|
|
20
|
+
.with_cache_ttl(3600)
|
|
21
|
+
.build
|
|
22
|
+
|
|
23
|
+
memory_client = Zenmanage::Client.new(memory_config)
|
|
24
|
+
puts "Flag: #{memory_client.flags.single("dark-mode", false).enabled?}"
|
|
25
|
+
puts "Second call uses cache (no extra HTTP requests)"
|
|
26
|
+
puts "Flag: #{memory_client.flags.single("dark-mode", false).enabled?}"
|
|
27
|
+
|
|
28
|
+
# ── Filesystem cache ─────────────────────────────────────────────────────────
|
|
29
|
+
puts "\n=== Filesystem Cache ==="
|
|
30
|
+
cache_dir = Dir.mktmpdir("zen_fs_cache_")
|
|
31
|
+
fs_config = Zenmanage::ConfigBuilder.create
|
|
32
|
+
.with_environment_token(token)
|
|
33
|
+
.with_cache_backend("filesystem")
|
|
34
|
+
.with_cache_directory(cache_dir)
|
|
35
|
+
.with_cache_ttl(3600)
|
|
36
|
+
.build
|
|
37
|
+
|
|
38
|
+
fs_client = Zenmanage::Client.new(fs_config)
|
|
39
|
+
puts "Flag (first call): #{fs_client.flags.single("dark-mode", false).enabled?}"
|
|
40
|
+
puts "Cache dir: #{Dir.glob(File.join(cache_dir, "*.zen_cache")).length} file(s)"
|
|
41
|
+
|
|
42
|
+
# Simulating a second process reading from the same cache:
|
|
43
|
+
fs_client2 = Zenmanage::Client.new(fs_config)
|
|
44
|
+
puts "Flag (second client from cache): #{fs_client2.flags.single("dark-mode", false).enabled?}"
|
|
45
|
+
|
|
46
|
+
FileUtils.rm_rf(cache_dir)
|
|
47
|
+
|
|
48
|
+
# ── Null cache ────────────────────────────────────────────────────────────────
|
|
49
|
+
puts "\n=== Null Cache ==="
|
|
50
|
+
null_config = Zenmanage::ConfigBuilder.create
|
|
51
|
+
.with_environment_token(token)
|
|
52
|
+
.with_cache_backend("null")
|
|
53
|
+
.with_cache_ttl(0) # fetch the rules on every check
|
|
54
|
+
.build
|
|
55
|
+
|
|
56
|
+
null_client = Zenmanage::Client.new(null_config)
|
|
57
|
+
puts "Flag: #{null_client.flags.single("dark-mode", false).enabled?}"
|
|
58
|
+
puts "With a null cache and a TTL of 0, every check fetches the rules"
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# examples/context_based_flags.rb
|
|
4
|
+
#
|
|
5
|
+
# Demonstrates how to pass user and organization context so that targeting
|
|
6
|
+
# rules in Zenmanage can evaluate against them.
|
|
7
|
+
#
|
|
8
|
+
# Usage:
|
|
9
|
+
# ZENMANAGE_ENVIRONMENT_TOKEN=srv_... ruby examples/context_based_flags.rb
|
|
10
|
+
|
|
11
|
+
require_relative "../lib/zenmanage"
|
|
12
|
+
|
|
13
|
+
config = Zenmanage::ConfigBuilder.create
|
|
14
|
+
.with_environment_token(ENV.fetch("ZENMANAGE_ENVIRONMENT_TOKEN", "srv_demo_token"))
|
|
15
|
+
.with_cache_backend("null")
|
|
16
|
+
.build
|
|
17
|
+
|
|
18
|
+
client = Zenmanage::Client.new(config)
|
|
19
|
+
|
|
20
|
+
# ── Simple user context ───────────────────────────────────────────────────────
|
|
21
|
+
user_ctx = Zenmanage::Context.single("user", "user-42", name: "Alice")
|
|
22
|
+
|
|
23
|
+
user_flags = client.flags.with_context(user_ctx)
|
|
24
|
+
puts "dark-mode for Alice → #{user_flags.single("dark-mode", false).enabled?}"
|
|
25
|
+
|
|
26
|
+
# ── Context with attributes ───────────────────────────────────────────────────
|
|
27
|
+
pro_user = Zenmanage::Context.new("user", identifier: "user-99", name: "Bob")
|
|
28
|
+
pro_user.add_attribute(Zenmanage::Attribute.new("plan", ["pro"]))
|
|
29
|
+
pro_user.add_attribute(Zenmanage::Attribute.new("locale", ["en-GB"]))
|
|
30
|
+
|
|
31
|
+
pro_flags = client.flags.with_context(pro_user)
|
|
32
|
+
puts "checkout-layout for pro Bob → #{pro_flags.single("checkout-layout", "control").as_string}"
|
|
33
|
+
|
|
34
|
+
# ── Organisation context ──────────────────────────────────────────────────────
|
|
35
|
+
org_ctx = Zenmanage::Context.from_hash(
|
|
36
|
+
"type" => "org",
|
|
37
|
+
"identifier" => "org-123",
|
|
38
|
+
"name" => "Acme Corp",
|
|
39
|
+
"attributes" => [
|
|
40
|
+
{ "key" => "tier", "values" => ["enterprise"] },
|
|
41
|
+
],
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
org_flags = client.flags.with_context(org_ctx)
|
|
45
|
+
puts "feature for Acme Corp → #{org_flags.single("enterprise-dashboard", false).enabled?}"
|