flipt_client 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3fba410b0fcad63979b3cb5f1e0128afccf64054e3b4ad0712c3040c946beaff
4
+ data.tar.gz: 56b652a202967aae88db7c4915a259f4490f5f9be043e008215728acf018dcd4
5
+ SHA512:
6
+ metadata.gz: f64f29f65b557a948ee83e20dea471b21c7c7c55561145c223c94e7dd35090dc61265ae0017ce8975bf271552fe8ed09a1140b5f549edc483d9e73e8be1a9514
7
+ data.tar.gz: ed2f3b489f6931b4bcd057d4f60a75c04d26d6b1b618bd69c8583631f74b1405f47f86859ebab42d898b766c62b1b9b535c7e8eb68d59e93d5cc1c6114aada14
data/README.md ADDED
@@ -0,0 +1,90 @@
1
+ # Flipt Client Ruby
2
+
3
+ The `flipt-client-ruby` directory contains the Ruby source code for the Flipt client-side evaluation client.
4
+
5
+ ## Installation
6
+
7
+ Currently, to use this client, you'll need to build the dynamic library and the gem locally and install it. This is a temporary solution until we can figure out a better way to package and distribute the libraries.
8
+
9
+ The dynamic library will contain the functionality necessary for the client to make calls to the Flipt engine via FFI. See [flipt-engine](../flipt-engine) for more information on the Flipt engine and FFI.
10
+
11
+ ### Prerequisites
12
+
13
+ - [Rust](https://www.rust-lang.org/tools/install)
14
+ - [Ruby](https://www.ruby-lang.org/en/documentation/installation/)
15
+ - [Make](https://www.gnu.org/software/make/)
16
+
17
+ ### Automated Build
18
+
19
+ 1. Build and copy the dynamic library to the `flipt-client-ruby/lib/ext` directory for your platform. This will also build and install the `flipt_client` gem on your local machine. You can do this by running the following command from the root of the repository:
20
+
21
+ ```bash
22
+ make ruby
23
+ ```
24
+
25
+ ### Manual Build
26
+
27
+ 1. Build the Rust dynamic library
28
+
29
+ ```bash
30
+ cargo build --release
31
+ ```
32
+
33
+ This should generate a `target/` directory in the root of this repository, which contains the dynamically linked library built for your platform.
34
+
35
+ 2. You'll need to copy the dynamic library to the `flipt-client-ruby/lib/ext/$OS_$ARCH/` directory. This is a temporary solution until we can figure out a better way to package the libraries with the gem.
36
+
37
+ The `path/to/lib` will be the path to the dynamic library which will have the following paths depending on your platform.
38
+
39
+ - **Linux**: `{REPO_ROOT}/target/release/libfliptengine.so`
40
+ - **Windows**: `{REPO_ROOT}/target/release/libfliptengine.dll`
41
+ - **MacOS**: `{REPO_ROOT}/target/release/libfliptengine.dylib`
42
+
43
+ 3. You can then build the gem and install it locally. You can do this by running the following command from the `flipt-client-ruby` directory:
44
+
45
+ ```bash
46
+ rake build
47
+ gem install pkg/flipt_client-{version}.gem
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ In your Ruby code you can import this client and use it as so:
53
+
54
+ ```ruby
55
+ require 'flipt_client'
56
+
57
+ # namespace is the first positional argument and is optional here and will have a value of "default" if not specified.
58
+ # opts is the second positional argument and is also optional, the structure is:
59
+ # {
60
+ # "url": "http://localhost:8080",
61
+ # "update_interval": 120,
62
+ # "auth_token": "secret"
63
+ # }
64
+ #
65
+ # You can replace the url with where your upstream Flipt instance points to, the update interval for how long you are willing
66
+ # to wait for updated flag state, and the auth token if your Flipt instance requires it.
67
+ client = Flipt::EvaluationClient.new()
68
+ resp = client.evaluate_variant({ flag_key: 'buzz', entity_id: 'someentity', context: { fizz: 'buzz' } })
69
+
70
+ puts resp
71
+ ```
72
+
73
+ ## Load Test
74
+
75
+ 1. To run the load test, you'll need to have Flipt running locally. You can do this by running the following command from the root of the repository:
76
+
77
+ ```bash
78
+ docker run -d \
79
+ -p 8080:8080 \
80
+ -p 9000:9000 \
81
+ docker.flipt.io/flipt/flipt:latest
82
+ ```
83
+
84
+ 2. You'll also need to have the `flipt_client` gem installed locally. See [Installation](#installation) above.
85
+ 3. In the Flipt UI (<http://localhost:8080>) you'll also need to create a new boolean flag with the key `my-feature` in the default namespace.
86
+ 4. You can then run the load test by running the following command from this folder:
87
+
88
+ ```bash
89
+ bundle exec ruby load_test.rb
90
+ ```
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/flipt_client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'flipt_client'
7
+ spec.version = Flipt::VERSION
8
+ spec.authors = ['Flipt Devs']
9
+ spec.email = ['dev@flipt.io']
10
+ spec.summary = 'Flipt Client Evaluation SDK'
11
+ spec.description = 'Flipt Client Evaluation SDK'
12
+ spec.homepage = 'https://www.flipt.io'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata['homepage_uri'] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/flipt-io/flipt-client-sdks"
20
+
21
+ spec.files = Dir.glob('{lib}/**/*') + ['README.md', 'flipt-client-ruby.gemspec']
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+ end
@@ -0,0 +1 @@
1
+ /Users/runner/work/flipt-client-sdks/flipt-client-sdks/target/aarch64-apple-darwin/release/libfliptengine.rlib: /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/build.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/error/mod.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/evaluator/mod.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/lib.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/common.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/flipt.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/mod.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/source.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/parser/mod.rs /Users/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/store/mod.rs
@@ -0,0 +1,32 @@
1
+ #include <stdarg.h>
2
+ #include <stdbool.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+
6
+ /**
7
+ * # Safety
8
+ *
9
+ * This function will initialize an Engine and return a pointer back to the caller.
10
+ */
11
+ void *initialize_engine(const char *const *namespaces, const char *opts);
12
+
13
+ /**
14
+ * # Safety
15
+ *
16
+ * This function will take in a pointer to the engine and return a variant evaluation response.
17
+ */
18
+ const char *evaluate_variant(void *engine_ptr, const char *evaluation_request);
19
+
20
+ /**
21
+ * # Safety
22
+ *
23
+ * This function will take in a pointer to the engine and return a boolean evaluation response.
24
+ */
25
+ const char *evaluate_boolean(void *engine_ptr, const char *evaluation_request);
26
+
27
+ /**
28
+ * # Safety
29
+ *
30
+ * This function will free the memory occupied by the engine.
31
+ */
32
+ void destroy_engine(void *engine_ptr);
@@ -0,0 +1 @@
1
+ /target/aarch64-unknown-linux-gnu/release/libfliptengine.rlib: /project/flipt-engine/build.rs /project/flipt-engine/src/error/mod.rs /project/flipt-engine/src/evaluator/mod.rs /project/flipt-engine/src/lib.rs /project/flipt-engine/src/models/common.rs /project/flipt-engine/src/models/flipt.rs /project/flipt-engine/src/models/mod.rs /project/flipt-engine/src/models/source.rs /project/flipt-engine/src/parser/mod.rs /project/flipt-engine/src/store/mod.rs
@@ -0,0 +1,32 @@
1
+ #include <stdarg.h>
2
+ #include <stdbool.h>
3
+ #include <stdint.h>
4
+ #include <stdlib.h>
5
+
6
+ /**
7
+ * # Safety
8
+ *
9
+ * This function will initialize an Engine and return a pointer back to the caller.
10
+ */
11
+ void *initialize_engine(const char *const *namespaces, const char *opts);
12
+
13
+ /**
14
+ * # Safety
15
+ *
16
+ * This function will take in a pointer to the engine and return a variant evaluation response.
17
+ */
18
+ const char *evaluate_variant(void *engine_ptr, const char *evaluation_request);
19
+
20
+ /**
21
+ * # Safety
22
+ *
23
+ * This function will take in a pointer to the engine and return a boolean evaluation response.
24
+ */
25
+ const char *evaluate_boolean(void *engine_ptr, const char *evaluation_request);
26
+
27
+ /**
28
+ * # Safety
29
+ *
30
+ * This function will free the memory occupied by the engine.
31
+ */
32
+ void destroy_engine(void *engine_ptr);
@@ -0,0 +1 @@
1
+ /home/runner/work/flipt-client-sdks/flipt-client-sdks/target/x86_64-unknown-linux-gnu/release/libfliptengine.rlib: /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/build.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/error/mod.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/evaluator/mod.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/lib.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/common.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/flipt.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/mod.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/models/source.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/parser/mod.rs /home/runner/work/flipt-client-sdks/flipt-client-sdks/flipt-engine/src/store/mod.rs
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flipt
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'flipt_client/version'
4
+ require 'ffi'
5
+ require 'json'
6
+
7
+ module Flipt
8
+ class Error < StandardError; end
9
+
10
+ class EvaluationClient
11
+ extend FFI::Library
12
+
13
+ FLIPTENGINE = 'libfliptengine'
14
+
15
+ def self.libfile
16
+ case RbConfig::CONFIG['arch']
17
+ when /arm64-darwin/
18
+ "ext/darwin_arm64/#{FLIPTENGINE}.dylib"
19
+ when /arm64-linux|aarch64-linux/
20
+ "ext/linux_arm64/#{FLIPTENGINE}.so"
21
+ when /x86_64-linux/
22
+ "ext/linux_x86_64/#{FLIPTENGINE}.so"
23
+ else
24
+ raise "unsupported platform #{RbConfig::CONFIG['arch']}"
25
+ end
26
+ end
27
+
28
+ ffi_lib File.expand_path(libfile, __dir__)
29
+
30
+ # void *initialize_engine(const char *const *namespaces, const char *opts);
31
+ attach_function :initialize_engine, %i[pointer string], :pointer
32
+ # void destroy_engine(void *engine_ptr);
33
+ attach_function :destroy_engine, [:pointer], :void
34
+ # const char *evaluate_variant(void *engine_ptr, const char *evaluation_request);
35
+ attach_function :evaluate_variant, %i[pointer string], :string
36
+ # const char *evaluate_boolean(void *engine_ptr, const char *evaluation_request);
37
+ attach_function :evaluate_boolean, %i[pointer string], :string
38
+
39
+ def initialize(namespace = 'default', opts = {})
40
+ @namespace = namespace
41
+ namespace_list = [namespace]
42
+ ns = FFI::MemoryPointer.new(:pointer, namespace_list.size)
43
+ namespace_list.each_with_index do |namespace, i|
44
+ ns[i].put_pointer(0, FFI::MemoryPointer.from_string(namespace))
45
+ end
46
+
47
+ @engine = self.class.initialize_engine(ns, opts.to_json)
48
+ ObjectSpace.define_finalizer(self, self.class.finalize(@engine))
49
+ end
50
+
51
+ def self.finalize(engine)
52
+ proc { destroy_engine(engine) }
53
+ end
54
+
55
+ def evaluate_variant(evaluation_request = {})
56
+ evaluation_request[:namespace_key] = @namespace
57
+ validate_evaluation_request(evaluation_request)
58
+ resp = self.class.evaluate_variant(@engine, evaluation_request.to_json)
59
+ JSON.parse(resp)
60
+ end
61
+
62
+ def evaluate_boolean(evaluation_request = {})
63
+ evaluation_request[:namespace_key] = @namespace
64
+ validate_evaluation_request(evaluation_request)
65
+ resp = self.class.evaluate_boolean(@engine, evaluation_request.to_json)
66
+ JSON.parse(resp)
67
+ end
68
+
69
+ private
70
+ def validate_evaluation_request(evaluation_request)
71
+ if evaluation_request[:entity_id].nil? || evaluation_request[:entity_id].empty?
72
+ raise ArgumentError, 'entity_id is required'
73
+ elsif evaluation_request[:namespace_key].nil? || evaluation_request[:namespace_key].empty?
74
+ raise ArgumentError, 'namespace_key is required'
75
+ elsif evaluation_request[:flag_key].nil? || evaluation_request[:flag_key].empty?
76
+ raise ArgumentError, 'flag_key is required'
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flipt_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Flipt Devs
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Flipt Client Evaluation SDK
14
+ email:
15
+ - dev@flipt.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - flipt-client-ruby.gemspec
22
+ - lib/ext/darwin_arm64/libfliptengine.d
23
+ - lib/ext/darwin_arm64/libfliptengine.dylib
24
+ - lib/ext/darwin_arm64/libfliptengine.rlib
25
+ - lib/ext/flipt_engine.h
26
+ - lib/ext/linux_arm64/libfliptengine.d
27
+ - lib/ext/linux_arm64/libfliptengine.rlib
28
+ - lib/ext/linux_arm64/libfliptengine.so
29
+ - lib/ext/linux_x86_64/flipt_engine.h
30
+ - lib/ext/linux_x86_64/libfliptengine.d
31
+ - lib/ext/linux_x86_64/libfliptengine.rlib
32
+ - lib/ext/linux_x86_64/libfliptengine.so
33
+ - lib/flipt_client.rb
34
+ - lib/flipt_client/version.rb
35
+ homepage: https://www.flipt.io
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://www.flipt.io
40
+ source_code_uri: https://github.com/flipt-io/flipt-client-sdks
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.0
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubygems_version: 3.3.26
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: Flipt Client Evaluation SDK
60
+ test_files: []