tggl 0.1.0

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: cbb38936e03d4a7db6f476eff96402f4cff1e1a77f543df54e206ae5838f6089
4
+ data.tar.gz: 4c44d480fa26bb4beb22eeb8e18eb9b3a7eb763e75444b13eb89094e441b6d55
5
+ SHA512:
6
+ metadata.gz: 4ed25464e700635d8bc60f6afa5f13d62bfe1b67197a4d3c5c3c802e90ff663086f0f6be654880617b1bed447af6bbbcb10b57dabdd53c639ce85880ba2d9e9f
7
+ data.tar.gz: cefcc8813e1231125bce0491e189c3a315695289f6fb07d343d6d978889ac60237c35a28aaea88faf0b5fcb0640c85d3423b6048a4485a6ab1e66a8825491cbd
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
data/.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="ruby-2.6.10-p210" project-jdk-type="RUBY_SDK" />
4
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/ruby-tggl-client.iml" filepath="$PROJECT_DIR$/.idea/ruby-tggl-client.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,15 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$">
8
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
9
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
10
+ <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
11
+ </content>
12
+ <orderEntry type="inheritedJdk" />
13
+ <orderEntry type="sourceFolder" forTests="false" />
14
+ </component>
15
+ </module>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tggl.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem 'xxhash' "~> 0.5"
11
+
12
+ group :test do
13
+ gem 'webmock', '~> 3.23', '>= 3.23.1'
14
+ gem "minitest", "~> 5.0"
15
+ end
16
+
17
+ group :development do
18
+ gem "rubocop", "~> 1.21"
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,65 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tggl (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.7)
10
+ public_suffix (>= 2.0.2, < 7.0)
11
+ ast (2.4.2)
12
+ bigdecimal (3.1.8)
13
+ crack (1.0.0)
14
+ bigdecimal
15
+ rexml
16
+ hashdiff (1.1.1)
17
+ json (2.7.2)
18
+ language_server-protocol (3.17.0.3)
19
+ minitest (5.25.1)
20
+ parallel (1.26.3)
21
+ parser (3.3.4.2)
22
+ ast (~> 2.4.1)
23
+ racc
24
+ public_suffix (6.0.1)
25
+ racc (1.8.1)
26
+ rainbow (3.1.1)
27
+ rake (13.2.1)
28
+ regexp_parser (2.9.2)
29
+ rexml (3.3.6)
30
+ strscan
31
+ rubocop (1.65.1)
32
+ json (~> 2.3)
33
+ language_server-protocol (>= 3.17.0)
34
+ parallel (~> 1.10)
35
+ parser (>= 3.3.0.2)
36
+ rainbow (>= 2.2.2, < 4.0)
37
+ regexp_parser (>= 2.4, < 3.0)
38
+ rexml (>= 3.2.5, < 4.0)
39
+ rubocop-ast (>= 1.31.1, < 2.0)
40
+ ruby-progressbar (~> 1.7)
41
+ unicode-display_width (>= 2.4.0, < 3.0)
42
+ rubocop-ast (1.32.1)
43
+ parser (>= 3.3.1.0)
44
+ ruby-progressbar (1.13.0)
45
+ strscan (3.1.0)
46
+ unicode-display_width (2.5.0)
47
+ webmock (3.23.1)
48
+ addressable (>= 2.8.0)
49
+ crack (>= 0.3.2)
50
+ hashdiff (>= 0.4.0, < 2.0.0)
51
+ xxhash (0.5.0)
52
+
53
+ PLATFORMS
54
+ arm64-darwin-23
55
+
56
+ DEPENDENCIES
57
+ minitest (~> 5.0)
58
+ rake (~> 13.0)
59
+ rubocop (~> 1.21)
60
+ tggl!
61
+ webmock (~> 3.23, >= 3.23.1)
62
+ xxhash
63
+
64
+ BUNDLED WITH
65
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 nick-keller
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Tggl
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tggl`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tggl.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ module Tggl
7
+ class Client
8
+ def initialize(api_key = nil, options = {})
9
+ @api_key = api_key
10
+ @url = options[:url] || 'https://api.tggl.io/flags'
11
+ @reporter = api_key.nil? || options[:reporting] == false ? nil : Reporting.new(
12
+ api_key,
13
+ app_prefix: "ruby-client:#{VERSION}/Client",
14
+ url: options[:reporting] == true ? nil : options[:reporting]&.[](:url),
15
+ app: options[:reporting] == true ? nil : options[:reporting]&.[](:app)
16
+ )
17
+ end
18
+
19
+ def eval_context(context)
20
+ response = eval_contexts([context]).first
21
+ raise "Unexpected empty response from Tggl" if response.nil?
22
+
23
+ response
24
+ end
25
+
26
+ def eval_contexts(contexts)
27
+ uri = URI(@url)
28
+ http = Net::HTTP.new(uri.host, uri.port)
29
+ http.use_ssl = true
30
+
31
+ request = Net::HTTP::Post.new(uri.path, {
32
+ 'X-Tggl-Api-Key' => @api_key,
33
+ 'Content-Type' => 'application/json'
34
+ })
35
+ request.body = contexts.to_json
36
+
37
+ response = http.request(request)
38
+ result = JSON.parse(response.body, symbolize_names: true)
39
+
40
+ if response.code.to_i > 200
41
+ if result.nil?
42
+ raise "Invalid response from Tggl: #{response.code}"
43
+ end
44
+ raise result['error']
45
+ end
46
+
47
+ result.map { |flags| Response.new(flags) }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,257 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'xxhash'
6
+
7
+ module Tggl
8
+ class LocalClient
9
+ def initialize(api_key = nil, options = {})
10
+ @api_key = api_key
11
+ @config = options[:config] || {}
12
+ @url = options[:url] || 'https://api.tggl.io/config'
13
+ @reporter = api_key.nil? || options[:reporting] == false ? nil : Reporting.new(
14
+ api_key,
15
+ app_prefix: "ruby-client:#{VERSION}/LocalClient",
16
+ url: options[:reporting] == true ? nil : options[:reporting]&.[](:url),
17
+ app: options[:reporting] == true ? nil : options[:reporting]&.[](:app)
18
+ )
19
+ end
20
+
21
+ def fetch_config
22
+ uri = URI(@url)
23
+ http = Net::HTTP.new(uri.host, uri.port)
24
+ http.use_ssl = true
25
+
26
+ request = Net::HTTP::Get.new(uri.path, {
27
+ 'X-Tggl-Api-Key' => @api_key,
28
+ })
29
+
30
+ response = http.request(request)
31
+ result = JSON.parse(response.body, symbolize_names: true)
32
+
33
+ if response.code.to_i > 200
34
+ if result.nil?
35
+ raise "Invalid response from Tggl: #{response.code}"
36
+ end
37
+ raise result['error']
38
+ end
39
+
40
+ @config = Hash.new
41
+
42
+ result.each do |flag|
43
+ @config[flag[:slug].to_sym] = flag
44
+ end
45
+
46
+ @config
47
+ end
48
+
49
+ def is_active?(context, slug)
50
+ result = @config.key?(slug.to_sym) ? LocalClient.eval_flag(@config[slug.to_sym], context) : { active: false, value: nil }
51
+
52
+ if @reporter != nil
53
+ @reporter.report_flag(slug, result[:active], result[:value])
54
+ @reporter.report_context(context)
55
+ end
56
+
57
+ result[:active]
58
+ end
59
+
60
+ def get(context, slug, default_value = nil)
61
+ result = @config.key?(slug.to_sym) ? LocalClient.eval_flag(@config[slug.to_sym], context) : { active: false, value: nil }
62
+ value = result[:active] ? result[:value] : default_value
63
+
64
+ if @reporter != nil
65
+ @reporter.report_flag(slug, result[:active], result[:value], default_value)
66
+ @reporter.report_context(context)
67
+ end
68
+
69
+ value
70
+ end
71
+
72
+ def all_active_flags(context)
73
+ if @reporter != nil
74
+ @reporter.report_context(context)
75
+ end
76
+
77
+ @config.map do |slug, flag|
78
+ result = LocalClient.eval_flag(flag, context)
79
+ result[:active] ? [slug, result[:value]] : nil
80
+ end.compact.to_h
81
+ end
82
+
83
+ def LocalClient.eval_flag(flag, context)
84
+ flag[:conditions].each do |condition|
85
+ if LocalClient.eval_condition(condition, context)
86
+ return condition[:variation][:active] ? condition[:variation] : { active: false, value: nil }
87
+ end
88
+ end
89
+
90
+ flag[:defaultVariation][:active] ? flag[:defaultVariation] : { active: false, value: nil }
91
+ end
92
+
93
+ def LocalClient.eval_condition(condition, context)
94
+ condition[:rules].each do |rule|
95
+ unless LocalClient.eval_rule(rule, context)
96
+ return false
97
+ end
98
+ end
99
+
100
+ true
101
+ end
102
+
103
+ def LocalClient.eval_rule(rule, context)
104
+ value = context[rule[:key].to_sym]
105
+
106
+ if rule[:operator] === "EMPTY"
107
+ is_empty = value === nil || value === ""
108
+ return is_empty != rule[:negate]
109
+ end
110
+
111
+ if value === nil
112
+ return false
113
+ end
114
+
115
+ if rule[:operator] === "STR_EQUAL"
116
+ return false unless value.is_a?(String)
117
+ return rule[:values].include?(value) != rule[:negate]
118
+ end
119
+
120
+ if rule[:operator] === "STR_EQUAL_SOFT"
121
+ return false unless value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float)
122
+ return rule[:values].include?(value.to_s.downcase) != rule[:negate]
123
+ end
124
+
125
+ if rule[:operator] === "STR_CONTAINS"
126
+ return false unless value.is_a?(String)
127
+ return rule[:values].any? { |val| value.include?(val) } != rule[:negate]
128
+ end
129
+
130
+ if rule[:operator] === "STR_STARTS_WITH"
131
+ return false unless value.is_a?(String)
132
+ return rule[:values].any? { |val| value.start_with?(val) } != rule[:negate]
133
+ end
134
+
135
+ if rule[:operator] === "STR_ENDS_WITH"
136
+ return false unless value.is_a?(String)
137
+ return rule[:values].any? { |val| value.end_with?(val) } != rule[:negate]
138
+ end
139
+
140
+ if rule[:operator] === "STR_AFTER"
141
+ return false unless value.is_a?(String)
142
+ return value >= rule[:value]
143
+ end
144
+
145
+ if rule[:operator] === "STR_BEFORE"
146
+ return false unless value.is_a?(String)
147
+ return value <= rule[:value]
148
+ end
149
+
150
+ if rule[:operator] === "REGEXP"
151
+ return false unless value.is_a?(String)
152
+ return (/#{rule[:value]}/.match?(value)) != rule[:negate]
153
+ end
154
+
155
+ if rule[:operator] === "TRUE"
156
+ return value == !rule[:negate]
157
+ end
158
+
159
+ if rule[:operator] === "EQ"
160
+ return false unless value.is_a?(Integer) || value.is_a?(Float)
161
+ return (value == rule[:value]) != rule[:negate]
162
+ end
163
+
164
+ if rule[:operator] === "LT"
165
+ return false unless value.is_a?(Integer) || value.is_a?(Float)
166
+ return (value < rule[:value]) != rule[:negate]
167
+ end
168
+
169
+ if rule[:operator] === "GT"
170
+ return false unless value.is_a?(Integer) || value.is_a?(Float)
171
+ return (value > rule[:value]) != rule[:negate]
172
+ end
173
+
174
+ if rule[:operator] === "ARR_OVERLAP"
175
+ return false unless value.is_a?(Array)
176
+ return value.any? { |val| rule[:values].include?(val) } != rule[:negate]
177
+ end
178
+
179
+ if rule[:operator] === "DATE_AFTER"
180
+ if value.is_a?(String)
181
+ val = value[0, '2000-01-01T23:59:59'.length] + ('2000-01-01T23:59:59'[value.length..] || "")
182
+ return rule[:iso] <= val
183
+ elsif value.is_a?(Integer) || value.is_a?(Float)
184
+ return value < 631_152_000_000 ? (value * 1000 >= rule[:timestamp]) : (value >= rule[:timestamp])
185
+ end
186
+ return false
187
+ end
188
+
189
+ if rule[:operator] === "DATE_BEFORE"
190
+ if value.is_a?(String)
191
+ val = value[0, '2000-01-01T00:00:00'.length] + ('2000-01-01T00:00:00'[value.length..] || "")
192
+ return rule[:iso] >= val
193
+ elsif value.is_a?(Integer) || value.is_a?(Float)
194
+ return value < 631_152_000_000 ? (value * 1000 <= rule[:timestamp]) : (value <= rule[:timestamp])
195
+ end
196
+ return false
197
+ end
198
+
199
+ if rule[:operator] === "SEMVER_EQ"
200
+ return false unless value.is_a?(String)
201
+ sem_ver = value.split('.').map(&:to_i)
202
+ return rule[:version].each_with_index.all? do |v, i|
203
+ sem_ver[i] == v
204
+ end != rule[:negate]
205
+ end
206
+
207
+ if rule[:operator] === "SEMVER_GTE"
208
+ return false unless value.is_a?(String)
209
+ sem_ver = value.split('.').map(&:to_i)
210
+ rule[:version].each_with_index do |v, i|
211
+ if i >= sem_ver.length
212
+ return rule[:negate]
213
+ end
214
+
215
+ if sem_ver[i] > v
216
+ return !rule[:negate]
217
+ end
218
+
219
+ if sem_ver[i] < v
220
+ return rule[:negate]
221
+ end
222
+ end
223
+
224
+ return !rule[:negate]
225
+ end
226
+
227
+ if rule[:operator] === "SEMVER_LTE"
228
+ return false unless value.is_a?(String)
229
+ sem_ver = value.split('.').map(&:to_i)
230
+ rule[:version].each_with_index do |v, i|
231
+ if i >= sem_ver.length
232
+ return rule[:negate]
233
+ end
234
+
235
+ if sem_ver[i] < v
236
+ return !rule[:negate]
237
+ end
238
+
239
+ if sem_ver[i] > v
240
+ return rule[:negate]
241
+ end
242
+ end
243
+
244
+ return !rule[:negate]
245
+ end
246
+
247
+ if rule[:operator] === "PERCENTAGE"
248
+ return false unless value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float)
249
+ probability = XXhash.xxh32(value.to_s, rule[:seed]) / 0xFFFFFFFF.to_f
250
+ probability -= 1.0e-8 if probability == 1
251
+ return probability >= rule[:rangeStart] && probability < rule[:rangeEnd]
252
+ end
253
+
254
+ raise "Unsupported operator #{rule[:operator]}"
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+
6
+ def constant_case(str)
7
+ str
8
+ .gsub(/([a-z])([A-Z])/, '\1_\2')
9
+ .gsub(/[\W_]+/, '_')
10
+ .upcase
11
+ end
12
+
13
+ module Tggl
14
+ class Reporting
15
+ def initialize(api_key = nil, options = {})
16
+ @api_key = api_key
17
+ @url = options[:url] || 'https://api.tggl.io/report'
18
+ @app = options[:app]
19
+ @app_prefix = options[:app_prefix]
20
+ @last_report_time = Time.now.to_i
21
+ @flags_to_report = Hash.new
22
+ @received_properties_to_report = Hash.new
23
+ @received_values_to_report = Hash.new
24
+ end
25
+
26
+ def send_report
27
+ payload = {}
28
+
29
+ unless @flags_to_report.empty?
30
+ flags_to_report = @flags_to_report
31
+ @flags_to_report = Hash.new
32
+
33
+ client_id = (@app_prefix || '') + (!@app.nil? && !@app_prefix.nil? ? '/' : '') + (@app || '')
34
+ payload[:clients] = [
35
+ {
36
+ flags: flags_to_report.keys.reduce({}) do |acc, key|
37
+ acc[key] = flags_to_report[key].values
38
+ acc
39
+ end
40
+ }
41
+ ]
42
+
43
+ unless client_id.empty?
44
+ payload[:clients][0][:id] = client_id
45
+ end
46
+ end
47
+
48
+ unless @received_properties_to_report.empty?
49
+ received_properties = @received_properties_to_report
50
+ @received_properties_to_report = Hash.new
51
+
52
+ payload[:receivedProperties] = received_properties
53
+ end
54
+
55
+ unless @received_values_to_report.empty?
56
+ received_values = @received_values_to_report
57
+ @received_values_to_report = Hash.new
58
+
59
+ data = received_values.keys.reduce([]) do |acc, key|
60
+ received_values[key].keys.each do |value|
61
+ label = received_values[key][value]
62
+ if label.nil?
63
+ acc << [key, value]
64
+ else
65
+ acc << [key, value, label]
66
+ end
67
+ end
68
+ acc
69
+ end
70
+
71
+ page_size = 2000
72
+
73
+ payload[:receivedValues] = data.first(page_size).reduce({}) do |acc, cur|
74
+ acc[cur[0]] ||= []
75
+ acc[cur[0]] << cur.drop(1).map { |v| v[0...240] }
76
+ acc
77
+ end
78
+
79
+ (page_size...data.size).step(page_size) do |i|
80
+ # @api_client.call(@url, true, @api_key, {
81
+ # receivedValues: data.slice(i, page_size).reduce({}) do |acc, cur|
82
+ # acc[cur[0]] ||= []
83
+ # acc[cur[0]] << cur.drop(1).map { |v| v[0...240] }
84
+ # acc
85
+ # end
86
+ # })
87
+ end
88
+ end
89
+
90
+ unless payload.empty?
91
+ uri = URI(@url)
92
+ http = Net::HTTP.new(uri.host, uri.port)
93
+ http.use_ssl = true
94
+
95
+ request = Net::HTTP::Post.new(uri.path, {
96
+ 'X-Tggl-Api-Key' => @api_key,
97
+ 'Content-Type' => 'application/json'
98
+ })
99
+ request.body = payload.to_json
100
+
101
+ http.request(request)
102
+ end
103
+ end
104
+
105
+ def report_flag(slug, active, value = nil, default = nil)
106
+ key = "#{active ? '1' : '0'}#{value.to_json}#{default.to_json}"
107
+
108
+ @flags_to_report[slug] ||= Hash.new
109
+
110
+ if @flags_to_report[slug].key?(key)
111
+ @flags_to_report[slug][key][:count] += 1
112
+ else
113
+ @flags_to_report[slug][key] = {
114
+ active: active,
115
+ value: value,
116
+ default: default,
117
+ count: 1
118
+ }
119
+ end
120
+ end
121
+
122
+ def report_context(context)
123
+ now = Time.now.to_i
124
+
125
+ context.each do |key, value|
126
+ if @received_properties_to_report.key?(key)
127
+ @received_properties_to_report[key][1] = now
128
+ else
129
+ @received_properties_to_report[key] = [now, now]
130
+ end
131
+
132
+ if value.is_a?(String) && !value.empty?
133
+ constant_case_key = constant_case(key.to_s).gsub(/_I_D$/, '_ID')
134
+ label_key_target = constant_case_key.end_with?('_ID') ? constant_case_key.gsub(/_ID$/, '_NAME') : nil
135
+ label_key = label_key_target.nil? ? nil : context.keys.find { |k| constant_case(k.to_s) == label_key_target }
136
+
137
+ @received_values_to_report[key] ||= Hash.new
138
+ @received_values_to_report[key][value] = label_key && context[label_key].is_a?(String) ? context[label_key] : nil
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tggl
4
+ class Response
5
+ def initialize(flags, reporter = nil)
6
+ @flags = flags
7
+ @reporter = reporter
8
+ end
9
+
10
+ def is_active?(slug)
11
+ active = @flags.key?(slug.to_sym)
12
+
13
+ if @reporter != nil
14
+ @reporter.report_flag(slug, active, active ? @flags[slug.to_sym] : nil)
15
+ end
16
+
17
+ active
18
+ end
19
+
20
+ def get(slug, default_value = nil)
21
+ value = @flags.key?(slug.to_sym) ? @flags[slug.to_sym] : default_value
22
+
23
+ if @reporter != nil
24
+ @reporter.report_flag(slug, @flags.key?(slug.to_sym), value, default_value)
25
+ end
26
+
27
+ value
28
+ end
29
+
30
+ def all_active_flags
31
+ @flags
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tggl
4
+ VERSION = "0.1.0"
5
+ end
data/lib/tggl.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tggl/version"
4
+ require_relative "tggl/client"
5
+ require_relative "tggl/response"
6
+ require_relative "tggl/local_client"
7
+ require_relative "tggl/reporting"
8
+
9
+ module Tggl
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,12 @@
1
+ module Tggl
2
+ class Client
3
+ @api_key: String | nil
4
+ @url: String
5
+ @reporter: Reporting | nil
6
+
7
+ def initialize: (String | nil api_key, { url?: String, reporting?: bool | { app?: String, url?: String } } options) -> void
8
+
9
+ def eval_context: (Hash[Symbol, any] context) -> Response
10
+ def eval_contexts: (Array[Hash[Symbol, any]] contexts) -> Array[Response]
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ module Tggl
2
+ class LocalClient
3
+ type variation = { active: bool, value: any }
4
+ type rule = {
5
+ key: String,
6
+ operator: String,
7
+ negate?: bool,
8
+ rangeStart?: float,
9
+ rangeEnd?: float,
10
+ seed?: int,
11
+ values?: Array[String],
12
+ value?: String | float,
13
+ version?: Array[int],
14
+ timestamp?: int,
15
+ iso?: String,
16
+ }
17
+ type condition = { rules: Array[rule], variation: variation }
18
+ type flag = { defaultVariation: variation, conditions: Array[condition] }
19
+
20
+ @api_key: String | nil
21
+ @config: Hash[Symbol, flag]
22
+ @url: String
23
+ @reporter: Reporting | nil
24
+
25
+ def initialize: (String | nil api_key, { url?: String, config?: Hash[Symbol, flag], reporting?: bool | { app?: String, url?: String } } options) -> void
26
+
27
+
28
+ def fetch_config: -> Hash[Symbol, flag]
29
+ def get: (Hash[Symbol, any] context, String slug, any default_value) -> untyped
30
+ def is_active?: (Hash[Symbol, any] context, String slug) -> bool
31
+ def all_active_flags: (Hash[Symbol, any] context)-> Hash[Symbol, any]
32
+
33
+ def self.eval_flag: (flag flag, Hash[Symbol, any] context) -> variation
34
+ def self.eval_condition: (condition condition, Hash[Symbol, any] context) -> bool
35
+ def self.eval_rule: (rule rule, Hash[Symbol, any] context) -> bool
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ module Tggl
2
+ class Reporting
3
+ @api_key: String
4
+ @url: String
5
+ @app: String | nil
6
+ @app_prefix: String | nil
7
+ @last_report_time: int
8
+ @flags_to_report: Hash[
9
+ String,
10
+ Hash[
11
+ String,
12
+ {
13
+ active: bool,
14
+ value?: any,
15
+ default?: any,
16
+ count: int
17
+ }
18
+ ]
19
+ ]
20
+ @received_properties_to_report: Hash[Symbol, Array[int]]
21
+ @received_values_to_report: Hash[Symbol, Hash[String, String | nil]]
22
+
23
+ def initialize: (String api_key, { url?: String, app?: String, app_prefix?: String } options) -> void
24
+
25
+ def send_report: -> void
26
+ def report_flag: (String slug, bool active, any value, any default) -> void
27
+ def report_context: (Hash[Symbol, any] context) -> void
28
+ end
29
+ end
@@ -0,0 +1,12 @@
1
+ module Tggl
2
+ class Response
3
+ @flags: Hash[Symbol, any]
4
+ @reporter: Reporting | nil
5
+
6
+ def initialize: (Hash[Symbol, untyped] flags, Reporting | nil reporter) -> void
7
+
8
+ def is_active?: (String slug) -> bool
9
+ def get: (String slug, untyped default_value) -> untyped
10
+ def all_active_flags: () -> Hash[Symbol, untyped]
11
+ end
12
+ end
data/sig/tggl.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Tggl
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/tggl.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/tggl/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tggl"
7
+ spec.version = Tggl::VERSION
8
+ spec.authors = ["nick-keller"]
9
+ spec.email = ["hello@tggl.io"]
10
+
11
+ spec.summary = "Tggl client for Ruby"
12
+ spec.homepage = "https://tggl.io/developers/sdks/ruby"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/tggl/ruby-tggl-client"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "xxhash", '~> 0.5'
32
+
33
+ spec.add_development_dependency 'minitest', '~> 5.0'
34
+ spec.add_development_dependency 'rubocop', '~> 1.21'
35
+ spec.add_development_dependency 'webmock', '~> 3.23'
36
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tggl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nick-keller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: xxhash
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.21'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.21'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.23'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.23'
69
+ description:
70
+ email:
71
+ - hello@tggl.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".idea/.gitignore"
77
+ - ".idea/misc.xml"
78
+ - ".idea/modules.xml"
79
+ - ".idea/ruby-tggl-client.iml"
80
+ - ".idea/vcs.xml"
81
+ - ".rubocop.yml"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - lib/tggl.rb
88
+ - lib/tggl/client.rb
89
+ - lib/tggl/local_client.rb
90
+ - lib/tggl/reporting.rb
91
+ - lib/tggl/response.rb
92
+ - lib/tggl/version.rb
93
+ - sig/tggl.rbs
94
+ - sig/tggl/client.rbs
95
+ - sig/tggl/local_client.rbs
96
+ - sig/tggl/reporting.rbs
97
+ - sig/tggl/response.rbs
98
+ - tggl.gemspec
99
+ homepage: https://tggl.io/developers/sdks/ruby
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ homepage_uri: https://tggl.io/developers/sdks/ruby
104
+ source_code_uri: https://github.com/tggl/ruby-tggl-client
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 2.6.0
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubygems_version: 3.4.10
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Tggl client for Ruby
124
+ test_files: []