openfeature-ruby-sdk-contrib 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +8 -2
- data/lib/open_feature/sdk/contrib/client.rb +13 -0
- data/lib/open_feature/sdk/contrib/providers/common.rb +36 -18
- data/lib/open_feature/sdk/contrib/providers/file_provider.rb +7 -5
- data/lib/open_feature/sdk/contrib/version.rb +1 -1
- data/lib/open_feature/sdk/contrib.rb +2 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33279a645ab02c54060f2842ae586d244b779d20c5a3bd275486f2dbecaf0687
|
4
|
+
data.tar.gz: 6a9c0a4ce90d78978ddb4ef95e2cc880ab2601ef03bf23b7e58700e2a4b9c809
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbf841634b916502ff751778337d6b81e8472eb1186b20a687bf7fa3accfe82a96cef52ede9a42567f04b4cdbd274d44733d07c4c043d15dc7340d0397c7e402
|
7
|
+
data.tar.gz: b52c456508a087aab56ef26a42f20177e3a71145c785ba44dff405462164d112c18a3a6e30dbd93d0c22a49690085ed0ed218f521ba56f9ff1ff83b366b8b544
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -68,7 +68,13 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
68
68
|
|
69
69
|
## Usage
|
70
70
|
|
71
|
-
|
71
|
+
```
|
72
|
+
require "open_feature/sdk/contrib"
|
73
|
+
|
74
|
+
OpenFeature::SDK.configure do |config|
|
75
|
+
config.provider = OpenFeature::SDK::Contrib::Providers::FileProvider.new(source: "path/to/file", format: :yaml)
|
76
|
+
end
|
77
|
+
```
|
72
78
|
|
73
79
|
## Development
|
74
80
|
|
@@ -78,7 +84,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
78
84
|
|
79
85
|
## Contributing
|
80
86
|
|
81
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ehowe/openfeature-ruby-sdk-contrib.
|
82
88
|
|
83
89
|
## License
|
84
90
|
|
@@ -3,8 +3,15 @@ module OpenFeature
|
|
3
3
|
module Contrib
|
4
4
|
module Providers
|
5
5
|
module Common
|
6
|
-
|
7
|
-
|
6
|
+
ResolutionDetails = Struct.new(
|
7
|
+
:enabled,
|
8
|
+
:error_code,
|
9
|
+
:error_message,
|
10
|
+
:reason,
|
11
|
+
:value,
|
12
|
+
:variant,
|
13
|
+
keyword_init: true
|
14
|
+
)
|
8
15
|
|
9
16
|
# @return [String]
|
10
17
|
attr_reader :source
|
@@ -109,6 +116,27 @@ module OpenFeature
|
|
109
116
|
assert_type(value: source_value, default_value: default_value, return_types: [Hash])
|
110
117
|
end
|
111
118
|
|
119
|
+
def read_all_values_with_cache
|
120
|
+
now = Time.now.to_i
|
121
|
+
|
122
|
+
read_from_cache = if !@flag_contents
|
123
|
+
false
|
124
|
+
elsif !@last_cache
|
125
|
+
false
|
126
|
+
elsif cache_duration == Float::INFINITY
|
127
|
+
true
|
128
|
+
else
|
129
|
+
now - @last_cache < cache_duration
|
130
|
+
end
|
131
|
+
|
132
|
+
unless read_from_cache
|
133
|
+
read_and_parse_flags
|
134
|
+
@last_cache = Time.now.to_i
|
135
|
+
end
|
136
|
+
|
137
|
+
deep_keys.empty? ? @flag_contents : @flag_contents.dig(*deep_keys)
|
138
|
+
end
|
139
|
+
|
112
140
|
private
|
113
141
|
|
114
142
|
# Returns a value for the requested flag_key
|
@@ -116,17 +144,7 @@ module OpenFeature
|
|
116
144
|
# @param flag_key [String] requested flag key
|
117
145
|
# @param type ["boolean", "number", "float", "string"]
|
118
146
|
def read_value_with_cache(flag_key:, type:)
|
119
|
-
|
120
|
-
|
121
|
-
resolve_keys = lambda do |contents|
|
122
|
-
flag_contents = deep_keys.empty? ? contents : contents.dig(*deep_keys)
|
123
|
-
|
124
|
-
flag_contents.detect { |f| f["kind"] == type && f["name"] == flag_key }
|
125
|
-
end
|
126
|
-
|
127
|
-
return resolve_keys.call(@flag_contents) if @flag_contents && (cache_duration == Float::INFINITY || (@last_cache && @flag_contents && now - @last_cache < cache_duration))
|
128
|
-
|
129
|
-
@flag_contents = resolve_keys.call(read_and_parse_flags)
|
147
|
+
read_all_values_with_cache.detect { |f| f["kind"] == type && f["name"] == flag_key }
|
130
148
|
end
|
131
149
|
|
132
150
|
def read_and_parse_flags
|
@@ -140,14 +158,14 @@ module OpenFeature
|
|
140
158
|
{ "value" => default_value }
|
141
159
|
end
|
142
160
|
|
143
|
-
return
|
161
|
+
return ResolutionDetails.new(value: nil) if actual_value.nil?
|
144
162
|
|
145
|
-
return_types
|
146
|
-
|
163
|
+
return_types = Array(return_types)
|
164
|
+
variant_value = actual_value["variants"] ? actual_value["variants"][actual_value["value"]] : actual_value["value"]
|
147
165
|
|
148
|
-
raise OpenFeature::SDK::Contrib::InvalidReturnValueError, "Invalid flag value found: #{
|
166
|
+
raise OpenFeature::SDK::Contrib::InvalidReturnValueError, "Invalid flag value found: #{variant_value} is not in #{return_types.join(', ')}" unless return_types.include?(variant_value.class)
|
149
167
|
|
150
|
-
actual_value
|
168
|
+
ResolutionDetails.new(value: variant_value, enabled: actual_value["enabled"], variant: actual_value["variant"])
|
151
169
|
end
|
152
170
|
end
|
153
171
|
end
|
@@ -31,11 +31,13 @@ module OpenFeature
|
|
31
31
|
|
32
32
|
return custom_parser.call(file_contents) if custom_parser
|
33
33
|
|
34
|
-
if format == :yaml
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
@flag_contents = if format == :yaml
|
35
|
+
YAML.safe_load(file_contents)
|
36
|
+
else
|
37
|
+
JSON.parse(file_contents)
|
38
|
+
end
|
39
|
+
|
40
|
+
@flag_contents
|
39
41
|
end
|
40
42
|
end
|
41
43
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative "contrib/client"
|
3
4
|
require_relative "contrib/version"
|
4
5
|
require_relative "contrib/providers/common"
|
5
6
|
require_relative "contrib/providers/file_provider"
|
@@ -10,4 +11,4 @@ module OpenFeature
|
|
10
11
|
InvalidReturnValueError = Class.new(StandardError)
|
11
12
|
end
|
12
13
|
end
|
13
|
-
end
|
14
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: openfeature-ruby-sdk-contrib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eugene Howe
|
@@ -179,6 +179,7 @@ files:
|
|
179
179
|
- README.md
|
180
180
|
- Rakefile
|
181
181
|
- lib/open_feature/sdk/contrib.rb
|
182
|
+
- lib/open_feature/sdk/contrib/client.rb
|
182
183
|
- lib/open_feature/sdk/contrib/providers/common.rb
|
183
184
|
- lib/open_feature/sdk/contrib/providers/file_provider.rb
|
184
185
|
- lib/open_feature/sdk/contrib/version.rb
|