Skip to content

Commit f7a1dfd

Browse files
author
Tom
committed
Initial commit
0 parents  commit f7a1dfd

File tree

8 files changed

+275
-0
lines changed

8 files changed

+275
-0
lines changed

.vscode/launch.json

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Debug Local File",
9+
"type": "Ruby",
10+
"request": "launch",
11+
"cwd": "${workspaceRoot}",
12+
"program": "${workspaceRoot}/lib/bullet/train/ruby.rb"
13+
},
14+
{
15+
"name": "Listen for rdebug-ide",
16+
"type": "Ruby",
17+
"request": "attach",
18+
"cwd": "${workspaceRoot}",
19+
"remoteHost": "127.0.0.1",
20+
"remotePort": "1234",
21+
"remoteWorkspaceRoot": "${workspaceRoot}"
22+
},
23+
{
24+
"name": "Rails server",
25+
"type": "Ruby",
26+
"request": "launch",
27+
"cwd": "${workspaceRoot}",
28+
"program": "${workspaceRoot}/bin/rails",
29+
"args": [
30+
"server"
31+
]
32+
},
33+
{
34+
"name": "RSpec - all",
35+
"type": "Ruby",
36+
"request": "launch",
37+
"cwd": "${workspaceRoot}",
38+
"program": "${workspaceRoot}/bin/rspec",
39+
"args": [
40+
"-I",
41+
"${workspaceRoot}"
42+
]
43+
},
44+
{
45+
"name": "RSpec - active spec file only",
46+
"type": "Ruby",
47+
"request": "launch",
48+
"cwd": "${workspaceRoot}",
49+
"program": "${workspaceRoot}/bin/rspec",
50+
"args": [
51+
"-I",
52+
"${workspaceRoot}",
53+
"${file}"
54+
]
55+
},
56+
{
57+
"name": "Cucumber",
58+
"type": "Ruby",
59+
"request": "launch",
60+
"cwd": "${workspaceRoot}",
61+
"program": "${workspaceRoot}/bin/cucumber"
62+
}
63+
]
64+
}

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source "https://rubygems.org"
2+
3+
git_source(:github) {|repo_name| "https://github.com/SolidStateGroup/bullet-train-ruby-client" }
4+
5+
# Specify your gem's dependencies in bullet-train-ruby.gemspec
6+
gemspec

LICENCE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright (c) 2018 Solid State Technology Ltd (https://www.solidstategroup.com/) and individual contributors.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5+
6+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7+
8+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9+
10+
3. Neither the name of the Sentry nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11+
12+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<img width="100%" src="https://raw.githubusercontent.com/SolidStateGroup/bullet-train-frontend/master/hero.png"/>
2+
3+
# Bullet Train Client
4+
[![Gitter](https://img.shields.io/gitter/room/gitterHQ/gitter.svg)](https://gitter.im/SolidStateGroup/bullettrain?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5+
[![npm version](https://badge.fury.io/js/bullet-train-nodejs.svg)](https://badge.fury.io/js/bullet-train-nodejs)
6+
[![](https://data.jsdelivr.com/v1/package/npm/bullet-train-nodejs/badge)](https://www.jsdelivr.com/package/npm/bullet-train-nodejs)
7+
8+
The SDK clients for Ruby [https://bullet-train.io/](https://www.bullet-train.io/). Bullet Train allows you to manage feature flags and remote config across multiple projects, environments and organisations.
9+
10+
## Getting Started
11+
12+
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See running in production for notes on how to deploy the project on a live system.
13+
14+
## Installing
15+
16+
### VIA gem
17+
```gem install bullet-train```
18+
19+
## Usage
20+
**Retrieving feature flags for your project**
21+
22+
**For full documentation visit [https://docs.bullet-train.io](https://docs.bullet-train.io)**
23+
```javascript
24+
var bulletTrain = require("bullet-train-nodejs");
25+
26+
bulletTrain.init({
27+
environmentID:"<YOUR_ENVIRONMENT_KEY>"
28+
});
29+
30+
31+
bulletTrain.hasFeature("header", '<My User Id>')
32+
.then((featureEnabled) => {
33+
if (featureEnabled) {
34+
//Show my awesome cool new feature to this one user
35+
}
36+
});
37+
bulletTrain.hasFeature("header")
38+
.then((featureEnabled) => {
39+
if (featureEnabled) {
40+
//Show my awesome cool new feature to the world
41+
}
42+
});
43+
44+
bulletTrain.getValue("header", '<My User Id>')
45+
.then((value) => {
46+
//Show some unique value to this user
47+
});
48+
49+
bulletTrain.getValue("header")
50+
.then((value) => {
51+
//Show a value to the world
52+
});
53+
```
54+
**Available Options**
55+
56+
| Property | Description | Required | Default Value |
57+
| ------------- |:-------------:| -----:| -----:|
58+
| ```environmentID``` | Defines which project environment you wish to get flags for. *example ACME Project - Staging.* | **YES** | null
59+
| ```onError``` | Callback function on failure to retrieve flags. ``` (error)=>{...} ``` | **NO** | null
60+
| ```defaultFlags``` | Defines the default flags if there are any | **NO** | null
61+
| ```api``` | Use this property to define where you're getting feature flags from, e.g. if you're self hosting. | **NO** | https://bullet-train-api.dokku1.solidstategroup.com/api/v1/
62+
63+
**Available Functions**
64+
65+
| Property | Description |
66+
| ------------- |:-------------:|
67+
| ```init``` | Initialise the sdk against a particular environment
68+
| ```hasFeature(key)``` | Get the value of a particular feature e.g. ```bulletTrain.hasFeature("powerUserFeature") // true```
69+
| ```hasFeature(key, userId)``` | Get the value of a particular feature for a user e.g. ```bulletTrain.hasFeature("powerUserFeature", 1234) // true```
70+
| ```getValue(key)``` | Get the value of a particular feature e.g. ```bulletTrain.getValue("font_size") // 10```
71+
| ```getValue(keym userId)``` | Get the value of a particular feature for a specificed user e.g. ```bulletTrain.getValue("font_size", 1234) // 15```
72+
| ```getFlags()``` | Trigger a manual fetch of the environment features, if a user is identified it will fetch their features
73+
| ```getFlagsForUser(1234)``` | Trigger a manual fetch of the environment features with a given user id
74+
75+
76+
**Identifying users**
77+
78+
Identifying users allows you to target specific users from the [Bullet Train dashboard](https://www.bullet-train.io/).
79+
You can include an optional user identifier as part of the `hasFeature` and `getValue` methods to retrieve unique user flags and variables.
80+
81+
82+
## Contributing
83+
84+
Please read [CONTRIBUTING.md](https://gist.github.com/kyle-ssg/c36a03aebe492e45cbd3eefb21cb0486) for details on our code of conduct, and the process for submitting pull requests to us.
85+
86+
## Getting Help
87+
88+
If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates.
89+
90+
## Get in touch
91+
92+
If you have any questions about our projects you can email <a href="mailto:[email protected]">[email protected]</a>.

bullet-train-0.0.1.gem

6.5 KB
Binary file not shown.

bullet-train-ruby.gemspec

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Gem::Specification.new do |spec|
2+
spec.name = "bullet-train"
3+
spec.version = "0.0.1"
4+
spec.authors = ["Tom Stuart"]
5+
spec.email = ["[email protected]"]
6+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
7+
8+
spec.summary = "Bullet Train - Ship features with confidence"
9+
spec.description = "Ruby Client for Bullet-Train. Ship features with confidence using feature flags and remote config. Host yourself or use our hosted version at https://bullet-train.io"
10+
spec.homepage = "https://bullet-train.io"
11+
end

lib/bullet-train.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# require "bullet/train/ruby/version"
2+
require "open-uri"
3+
require "json"
4+
5+
class BulletTrain
6+
@@apiUrl = ""
7+
@@environmentKey = ""
8+
9+
def initialize(apiKey = nil, apiUrl = "https://api.bullet-train.io/api/v1/")
10+
@@environmentKey = apiKey
11+
@@apiUrl = apiUrl
12+
end
13+
14+
def getJSON(method = nil)
15+
response = open(@@apiUrl + "" + method.concat("?format=json"),
16+
"x-environment-key" => @@environmentKey).read
17+
return JSON.parse(response)
18+
end
19+
20+
def processFlags(inputFlags)
21+
flags = {}
22+
23+
for feature in inputFlags
24+
featureName = feature["feature"]["name"].downcase.gsub(/\s+/, "_")
25+
enabled = feature["enabled"]
26+
state = feature["feature_state_value"]
27+
flags[featureName] = {"enabled" => enabled, "value" => state}
28+
29+
return flags
30+
end
31+
32+
return flags
33+
end
34+
35+
def getFlagsForUser(identity = nil)
36+
processFlags(getJSON("flags/#{identity}"))
37+
end
38+
39+
def getFlags()
40+
processFlags(getJSON("flags"))
41+
end
42+
43+
def getValue(key, userId = nil)
44+
flags = nil
45+
# Get the features
46+
if userId != nil
47+
flags = getFlagsForUser(userId)
48+
else
49+
flags = getFlags()
50+
end
51+
# Return the value
52+
return flags[key]["value"]
53+
end
54+
55+
def hasFeature(key, userId = nil)
56+
# Get the features
57+
flags = nil
58+
if userId != nil
59+
flags = getFlagsForUser(userId)
60+
else
61+
flags = getFlags()
62+
end
63+
64+
# Work out if this feature exists
65+
if flags[key] == nil
66+
return false
67+
else
68+
return flags[key]["enabled"]
69+
end
70+
end
71+
end
72+
73+
# bt = BulletTrain.new("QjgYur4LQTwe5HpvbvhpzK")
74+
# print bt.getFlags()
75+
# print bt.getValue("font_size")
76+
# print bt.hasFeature("font_size")

test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require "bullet-train"
2+
3+
bt = BulletTrain.new("QjgYur4LQTwe5HpvbvhpzK")
4+
5+
if bt.getValue("font_size")
6+
# Do something awesome with the font size
7+
puts bt.getValue("font_size")
8+
end
9+
10+
if bt.hasFeature("does_not_exist")
11+
#do something
12+
else
13+
#do nothing, or something else
14+
end

0 commit comments

Comments
 (0)