This is a client for talking to the Bandiera feature flagging service from a Ruby application.
This client is compatible with the v2 Bandiera API.
This client has been tested against the latest MRI and JRuby builds.
Add the following to your Gemfile
:
gem 'bandiera-client'
Configure the host and port of Bandiera server in your initializer file, and optionally a default_group name ("default" if not set) and logger (Logger if not set).
require 'bandiera_client'
Bandiera.configure do |config|
config.host = "172.17.0.1"
config.port = 5000
config.default_group = "All"
config.logger = Rails.logger
end
Then interact with a Bandiera server like so:
require 'bandiera_client'
if Bandiera.on?('show-new-search')
# show the new experimental search function
end
You can specify a group name when calling the server if your features are not in the default group
require 'bandiera_client'
if Bandiera.on?('show-new-search', 'my-group')
# show the new experimental search function
end
You can also specify parameters to use more advanced Bandiera features like user percentages and user groups:
require 'bandiera_client'
if Bandiera.on?('show-new-search', 'my-group', { user_id: '1234567'})
# show the new experimental search function
end
Add the following to your Gemfile
:
gem 'bandiera-client'
Then interact with a Bandiera server like so:
require 'bandiera/client'
client = Bandiera::Client.new('http://bandiera-demo.herokuapp.com')
params = {}
if client.enabled?('pubserv', 'show-new-search', params)
# show the new experimental search function
end
The client.enabled?
command takes two main arguments - the 'feature group', and the 'feature name'. This is because in Bandiera, features are organised into groups as it is intented as a service for multiple applications to use at the same time - this organisation allows separation of feature flags that are intended for different audiences.
client.enabled?
also takes an optional params
hash, this is for use with some of the more advanced features in Bandiera - user group and percentage based flags. It is in this params hash you pass in your user_group
and user_id
, i.e.:
client.enabled?('pubserv', 'show-new-search',
{ user_id: '1234567', user_group: 'Administrators' })
For more information on these advanced features, please see the Bandiera wiki:
https://github.com/springernature/bandiera/wiki/How-Feature-Flags-Work#feature-flags-in-bandiera
Using the client.enabled?
method all over your codebase isn't the most efficient way of working with Bandiera as every time you call enabled?
you will make a HTTP request to the Bandiera server.
One way of working more efficiently is using the Direct API Access methods, to fetch all the feature flags for a given group (or even all of the feature flags in the Bandiera server) in one request. You can then hold these as you please in your application and call on the values when needed.
Another approach is to use the Bandiera::Middleware class supplied in this gem. This can be used in conjunction with other middlewares for identifying your currently logged in user and assigning them a UUID to enable all of the most advanced features in Bandiera very simply.
See the blog post on cruft.io for more information on how Bandiera::Client is used at Nature.
If you'd prefer not to use the enabled?
method for featching feature flag values, the following methods are available...
Get features for all groups:
client.get_all(params)
# gives:
# {
# 'group1' => {
# 'feature1' => true,
# 'feature2' => false
# },
# 'group2' => {
# 'feature1' => false
# },
# }
Get features for a group:
client.get_features_for_group('pubserv', params)
# gives:
# {
# 'feature1' => true,
# 'feature2' => false
# }
Get an individual feature:
client.get_feature('pubserv', 'show-article-metrics', params)
# gives: true/false
As with the enabled?
method the params
hash is for passing in your user_group
and user_id
values if you are using some of the more advanced features in Bandiera.
- Fork this repo.
- Run
bundle install
© 2014 Springer Nature. Bandiera::Client (Ruby) is licensed under the MIT License.