Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make bootstrapping more ruby idiomatic
- Loading branch information
Showing
11 changed files
with
124 additions
and
102 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
This file was deleted.
Oops, something went wrong.
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,18 @@ | ||
module Unleash | ||
module Bootstrap | ||
class NotImplemented < RuntimeError | ||
end | ||
|
||
class Base | ||
def read | ||
raise NotImplemented, "Bootstrap is not implemented" | ||
end | ||
|
||
def extract_features(bootstrap_hash) | ||
raise NotImplemented, "The provided bootstrap data doesn't seem to have a valid set of toggles" if bootstrap_hash['version'] < 1 | ||
|
||
bootstrap_hash['features'] | ||
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,18 @@ | ||
module Unleash | ||
module Bootstrap | ||
class FromFile < Base | ||
attr_accessor :file_path | ||
|
||
# @param file_path [String] | ||
def initialize(file_path) | ||
self.file_path = file_path | ||
end | ||
|
||
def read | ||
file_content = File.read(self.file_path) | ||
bootstrap_hash = JSON.parse(file_content) | ||
extract_features(bootstrap_hash) | ||
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,20 @@ | ||
module Unleash | ||
module Bootstrap | ||
class FromUri < Base | ||
attr_accessor :uri, :headers | ||
|
||
# @param uri [String] | ||
# @param headers [Hash, nil] HTTP headers to use. If not set, the unleash client SDK ones will be used. | ||
def initialize(uri, headers = nil) | ||
self.uri = URI(uri) | ||
self.headers = headers | ||
end | ||
|
||
def read | ||
response = Unleash::Util::Http.get(self.uri, nil, self.headers) | ||
bootstrap_hash = JSON.parse(response.body) | ||
extract_features(bootstrap_hash) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
require 'rspec/json_expectations' | ||
require 'unleash/bootstrap/base' | ||
require 'unleash/bootstrap/from_file' | ||
require 'json' | ||
|
||
RSpec.describe Unleash::Bootstrap::FromFile do | ||
before do | ||
Unleash.configuration = Unleash::Configuration.new | ||
Unleash.logger = Unleash.configuration.logger | ||
end | ||
|
||
it 'loads bootstrap toggle correctly from file' do | ||
bootstrap_file = './spec/unleash/bootstrap-resources/features-v1.json' | ||
|
||
bootstrapper = Unleash::Bootstrap::FromFile.new(bootstrap_file) | ||
file_contents = File.open(bootstrap_file).read | ||
file_features = JSON.parse(file_contents)['features'] | ||
|
||
expect(bootstrapper.read).to include_json(file_features) | ||
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,26 @@ | ||
require 'unleash/bootstrap/from_uri' | ||
require 'json' | ||
|
||
RSpec.describe Unleash::Bootstrap::FromUri do | ||
it 'loads bootstrap toggle correctly from URL' do | ||
bootstrap_file = './spec/unleash/bootstrap-resources/features-v1.json' | ||
|
||
file_contents = File.open(bootstrap_file).read | ||
file_features = JSON.parse(file_contents)['features'] | ||
|
||
WebMock.stub_request(:get, "http://test-url/bootstrap-goodness") | ||
.with( | ||
headers: { | ||
'Accept' => '*/*', | ||
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', | ||
'Content-Type' => 'application/json', | ||
'User-Agent' => 'Ruby' | ||
} | ||
) | ||
.to_return(status: 200, body: file_contents, headers: {}) | ||
|
||
bootstrapper = Unleash::Bootstrap::FromUri.new('http://test-url/bootstrap-goodness', {}) | ||
|
||
expect(bootstrapper.read).to include_json(file_features) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.