Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
606 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'unleash' | ||
require 'unleash/context' | ||
require 'unleash/bootstrap/configuration' | ||
|
||
puts ">> START bootstrap.rb" | ||
|
||
@unleash = Unleash::Client.new( | ||
url: 'http://unleash.herokuapp.com/api', | ||
custom_http_headers: { 'Authorization': '943ca9171e2c884c545c5d82417a655fb77cec970cc3b78a8ff87f4406b495d0' }, | ||
app_name: 'bootstrap-test', | ||
instance_id: 'local-test-cli', | ||
refresh_interval: 2, | ||
disable_client: true, | ||
disable_metrics: true, | ||
metrics_interval: 2, | ||
retry_limit: 2, | ||
bootstrap_config: Unleash::Bootstrap::Configuration.new(file_path: "examples/default-toggles.json") | ||
) | ||
|
||
feature_name = "featureX" | ||
unleash_context = Unleash::Context.new | ||
unleash_context.user_id = 123 | ||
|
||
sleep 1 | ||
3.times do | ||
if @unleash.is_enabled?(feature_name, unleash_context) | ||
puts "> #{feature_name} is enabled" | ||
else | ||
puts "> #{feature_name} is not enabled" | ||
end | ||
sleep 1 | ||
puts "---" | ||
puts "" | ||
puts "" | ||
end | ||
|
||
sleep 3 | ||
feature_name = "foobar" | ||
if @unleash.is_enabled?(feature_name, unleash_context, true) | ||
puts "> #{feature_name} is enabled" | ||
else | ||
puts "> #{feature_name} is not enabled" | ||
end | ||
|
||
puts "> shutting down client..." | ||
|
||
@unleash.shutdown | ||
|
||
puts ">> END bootstrap.rb" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"version": 1, | ||
"features": [ | ||
{ | ||
"name": "featureX", | ||
"enabled": true, | ||
"strategies": [ | ||
{ | ||
"name": "default" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "featureY", | ||
"enabled": false, | ||
"strategies": [ | ||
{ | ||
"name": "baz", | ||
"parameters": { | ||
"foo": "bar" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "featureZ", | ||
"enabled": true, | ||
"strategies": [ | ||
{ | ||
"name": "default" | ||
}, | ||
{ | ||
"name": "hola", | ||
"parameters": { | ||
"name": "val" | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Unleash | ||
module Bootstrap | ||
class Configuration | ||
attr_accessor :data, :file_path, :url, :url_headers, :block | ||
|
||
def initialize(opts = {}) | ||
self.file_path = resolve_value_indifferently(opts, 'file_path') || ENV['UNLEASH_BOOTSTRAP_FILE'] || nil | ||
self.url = resolve_value_indifferently(opts, 'url') || ENV['UNLEASH_BOOTSTRAP_URL'] || nil | ||
self.url_headers = resolve_value_indifferently(opts, 'url_headers') | ||
self.data = resolve_value_indifferently(opts, 'data') | ||
self.block = resolve_value_indifferently(opts, 'block') | ||
end | ||
|
||
def valid? | ||
![self.data, self.file_path, self.url, self.block].all?(&:nil?) | ||
end | ||
|
||
private | ||
|
||
def resolve_value_indifferently(opts, key) | ||
opts[key] || opts[key.to_sym] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'unleash/bootstrap/provider/from_url' | ||
require 'unleash/bootstrap/provider/from_file' | ||
|
||
module Unleash | ||
module Bootstrap | ||
class Handler | ||
attr_accessor :configuration | ||
|
||
def initialize(configuration) | ||
self.configuration = configuration | ||
end | ||
|
||
# @return [String] JSON string representing data returned from an Unleash server | ||
def retrieve_toggles | ||
return configuration.data unless self.configuration.data.nil? | ||
return configuration.block.call if self.configuration.block.is_a?(Proc) | ||
return Provider::FromFile.read(configuration.file_path) unless self.configuration.file_path.nil? | ||
return Provider::FromUrl.read(configuration.url, configuration.url_headers) unless self.configuration.url.nil? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Unleash | ||
module Bootstrap | ||
module Provider | ||
class NotImplemented < RuntimeError | ||
end | ||
|
||
class Base | ||
def read | ||
raise NotImplemented, "Bootstrap is not implemented" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'unleash/bootstrap/provider/base' | ||
|
||
module Unleash | ||
module Bootstrap | ||
module Provider | ||
class FromFile < Base | ||
# @param file_path [String] | ||
def self.read(file_path) | ||
File.read(file_path) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'unleash/bootstrap/provider/base' | ||
|
||
module Unleash | ||
module Bootstrap | ||
module Provider | ||
class FromUrl < Base | ||
# @param url [String] | ||
# @param headers [Hash, nil] HTTP headers to use. If not set, the unleash client SDK ones will be used. | ||
def self.read(url, headers = nil) | ||
response = Unleash::Util::Http.get(URI.parse(url), nil, headers) | ||
|
||
return nil if response.code != '200' | ||
|
||
response.body | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.