Class: Tggl::LocalClient

Inherits:
Object
  • Object
show all
Defined in:
lib/tggl/local_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil, options = {}) ⇒ LocalClient

Returns a new instance of LocalClient.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tggl/local_client.rb', line 9

def initialize(api_key = nil, options = {})
  @api_key = api_key
  @config = options[:config] || {}
  @url = options[:url] || 'https://api.tggl.io/config'
  @reporter = api_key.nil? || options[:reporting] == false ? nil :  Reporting.new(
    api_key,
    app_prefix: "ruby-client:#{VERSION}/LocalClient",
    url: options[:reporting] == true ? nil : options[:reporting]&.[](:url),
    app: options[:reporting] == true ? nil : options[:reporting]&.[](:app)
  )
end

Class Method Details

.eval_condition(condition, context) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/tggl/local_client.rb', line 93

def LocalClient.eval_condition(condition, context)
  condition[:rules].each do |rule|
    unless LocalClient.eval_rule(rule, context)
      return false
    end
  end

  true
end

.eval_flag(flag, context) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/tggl/local_client.rb', line 83

def LocalClient.eval_flag(flag, context)
  flag[:conditions].each do |condition|
    if LocalClient.eval_condition(condition, context)
      return condition[:variation][:active] ? condition[:variation] : { active: false, value: nil }
    end
  end

  flag[:defaultVariation][:active] ? flag[:defaultVariation] : { active: false, value: nil }
end

.eval_rule(rule, context) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/tggl/local_client.rb', line 103

def LocalClient.eval_rule(rule, context)
  value = context[rule[:key].to_sym]

  if rule[:operator] === "EMPTY"
    is_empty = value === nil || value === ""
    return is_empty != rule[:negate]
  end

  if value === nil
    return false
  end

  if rule[:operator] === "STR_EQUAL"
    return false unless value.is_a?(String)
    return rule[:values].include?(value) != rule[:negate]
  end

  if rule[:operator] === "STR_EQUAL_SOFT"
    return false unless value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float)
    return rule[:values].include?(value.to_s.downcase) != rule[:negate]
  end

  if rule[:operator] === "STR_CONTAINS"
    return false unless value.is_a?(String)
    return rule[:values].any? { |val| value.include?(val) } != rule[:negate]
  end

  if rule[:operator] === "STR_STARTS_WITH"
    return false unless value.is_a?(String)
    return rule[:values].any? { |val| value.start_with?(val) } != rule[:negate]
  end

  if rule[:operator] === "STR_ENDS_WITH"
    return false unless value.is_a?(String)
    return rule[:values].any? { |val| value.end_with?(val) } != rule[:negate]
  end

  if rule[:operator] === "STR_AFTER"
    return false unless value.is_a?(String)
    return (value >= rule[:value]) != (rule[:negate].nil? ? false : rule[:negate])
  end

  if rule[:operator] === "STR_BEFORE"
    return false unless value.is_a?(String)
    return (value <= rule[:value]) != (rule[:negate].nil? ? false : rule[:negate])
  end

  if rule[:operator] === "REGEXP"
    return false unless value.is_a?(String)
    return (/#{rule[:value]}/.match?(value)) != rule[:negate]
  end

  if rule[:operator] === "TRUE"
    return value == !rule[:negate]
  end

  if rule[:operator] === "EQ"
    return false unless value.is_a?(Integer) || value.is_a?(Float)
    return (value == rule[:value]) != rule[:negate]
  end

  if rule[:operator] === "LT"
    return false unless value.is_a?(Integer) || value.is_a?(Float)
    return (value < rule[:value]) != rule[:negate]
  end

  if rule[:operator] === "GT"
    return false unless value.is_a?(Integer) || value.is_a?(Float)
    return (value > rule[:value]) != rule[:negate]
  end

  if rule[:operator] === "ARR_OVERLAP"
    return false unless value.is_a?(Array)
    return value.any? { |val| rule[:values].include?(val) } != rule[:negate]
  end

  if rule[:operator] === "DATE_AFTER"
    if value.is_a?(String)
      val = value[0, '2000-01-01T23:59:59'.length] + ('2000-01-01T23:59:59'[value.length..] || "")
      return (rule[:iso] <= val) != (rule[:negate].nil? ? false : rule[:negate])
    elsif value.is_a?(Integer) || value.is_a?(Float)
      return (value < 631_152_000_000 ? (value * 1000 >= rule[:timestamp]) : (value >= rule[:timestamp])) != (rule[:negate].nil? ? false : rule[:negate])
    end
    return false
  end

  if rule[:operator] === "DATE_BEFORE"
    if value.is_a?(String)
      val = value[0, '2000-01-01T00:00:00'.length] + ('2000-01-01T00:00:00'[value.length..] || "")
      return (rule[:iso] >= val) != (rule[:negate].nil? ? false : rule[:negate])
    elsif value.is_a?(Integer) || value.is_a?(Float)
      return (value < 631_152_000_000 ? (value * 1000 <= rule[:timestamp]) : (value <= rule[:timestamp])) != (rule[:negate].nil? ? false : rule[:negate])
    end
    return false
  end

  if rule[:operator] === "SEMVER_EQ"
    return false unless value.is_a?(String)
    sem_ver = value.split('.').map(&:to_i)
    return rule[:version].each_with_index.all? do |v, i|
      sem_ver[i] == v
    end != rule[:negate]
  end

  if rule[:operator] === "SEMVER_GTE"
    return false unless value.is_a?(String)
    sem_ver = value.split('.').map(&:to_i)
    rule[:version].each_with_index do |v, i|
      if i >= sem_ver.length
        return rule[:negate]
      end

      if sem_ver[i] > v
        return !rule[:negate]
      end

      if sem_ver[i] < v
        return rule[:negate]
      end
    end

    return !rule[:negate]
  end

  if rule[:operator] === "SEMVER_LTE"
    return false unless value.is_a?(String)
    sem_ver = value.split('.').map(&:to_i)
    rule[:version].each_with_index do |v, i|
      if i >= sem_ver.length
        return rule[:negate]
      end

      if sem_ver[i] < v
        return !rule[:negate]
      end

      if sem_ver[i] > v
        return rule[:negate]
      end
    end

    return !rule[:negate]
  end

  if rule[:operator] === "PERCENTAGE"
    return false unless value.is_a?(String) || value.is_a?(Integer) || value.is_a?(Float)
    probability = XXhash.xxh32(value.to_s, rule[:seed]) / 0xFFFFFFFF.to_f
    probability -= 1.0e-8 if probability == 1
    return (probability >= rule[:rangeStart] && probability < rule[:rangeEnd]) != (rule[:negate].nil? ? false : rule[:negate])
  end

  raise StandardError.new "Unsupported operator #{rule[:operator]}"
end

Instance Method Details

#all_active_flags(context) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/tggl/local_client.rb', line 72

def all_active_flags(context)
  if @reporter != nil
    @reporter.report_context(context)
  end

  @config.map do |slug, flag|
    result = LocalClient.eval_flag(flag, context)
    result[:active] ? [slug, result[:value]] : nil
  end.compact.to_h
end

#fetch_configObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tggl/local_client.rb', line 21

def fetch_config
  uri = URI(@url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Get.new(uri.path, {
    'X-Tggl-Api-Key' => @api_key,
  })

  response = http.request(request)
  result = JSON.parse(response.body, symbolize_names: true)

  if response.code.to_i > 200
    if result.nil?
      raise StandardError.new "Invalid response from Tggl: #{response.code}"
    end
    raise StandardError.new result['error']
  end

  @config = Hash.new

  result.each do |flag|
    @config[flag[:slug].to_sym] = flag
  end

  @config
end

#get(context, slug, default_value = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tggl/local_client.rb', line 60

def get(context, slug, default_value = nil)
  result = @config.key?(slug.to_sym) ? LocalClient.eval_flag(@config[slug.to_sym], context) : { active: false, value: nil }
  value = result[:active] ? result[:value] : default_value

  if @reporter != nil
    @reporter.report_flag(slug, result[:active], result[:value], default_value)
    @reporter.report_context(context)
  end

  value
end

#is_active?(context, slug) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
# File 'lib/tggl/local_client.rb', line 49

def is_active?(context, slug)
  result = @config.key?(slug.to_sym) ? LocalClient.eval_flag(@config[slug.to_sym], context) : { active: false, value: nil }

  if @reporter != nil
    @reporter.report_flag(slug, result[:active], result[:value])
    @reporter.report_context(context)
  end

  result[:active]
end