Class: Confidence::OpenFeature::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/confidence/openfeature/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_secret:, region: Region::EU) ⇒ APIClient

Returns a new instance of APIClient.



24
25
26
27
28
29
# File 'lib/confidence/openfeature/api_client.rb', line 24

def initialize(client_secret:, region: Region::EU)
  uri = URI.parse(region.uri)
  @client_secret = client_secret
  @agent = Net::HTTP.new(uri.host, uri.port)
  @agent.use_ssl = uri.scheme == "https"
end

Instance Method Details

#resolve(flags: [], context: {}, apply: true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/confidence/openfeature/api_client.rb', line 43

def resolve(flags: [], context: {}, apply: true)
  result = post_json("/v1/flags:resolve", {
    clientSecret: @client_secret,
    evaluationContext: context || {},
    apply: apply,
    flags: flags,
    sdk: {id: "SDK_ID_RUBY_PROVIDER", version: VERSION}
  })

  resolved_flags = result["resolvedFlags"] || []
  resolved_flags.map do |flag|
    ResolvedFlag.new(
      flag: flag["flag"],
      variant: nil_if_empty(flag["variant"]),
      value: nil_if_empty(flag["value"])
    )
  end
end

#resolve_one(flag:, context: {}, apply: true) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/confidence/openfeature/api_client.rb', line 31

def resolve_one(flag:, context: {}, apply: true)
  result = resolve(flags: [flag], context: context, apply: apply)
  if result.empty?
    raise FlagNotFoundError.new("No active flag '#{flag}' found was found")
  end
  result = result[0]
  if result.flag != flag
    raise FlagNotFoundError.new("Unexpected flag '#{flag}' from remote")
  end
  result
end