Class: OpenFeature::GoFeatureFlag::GoFeatureFlagApi

Inherits:
Object
  • Object
show all
Defined in:
lib/openfeature/go-feature-flag/goff_api.rb

Overview

This class is the entry point for the GoFeatureFlagProvider

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options: {}) ⇒ GoFeatureFlagApi

Returns a new instance of GoFeatureFlagApi.



15
16
17
18
19
20
21
# File 'lib/openfeature/go-feature-flag/goff_api.rb', line 15

def initialize(options: {})
  @options = options
  @faraday_connection = Faraday.new(
    url: @options.endpoint,
    headers: {"Content-Type" => "application/json"}.merge(@options.custom_headers || {})
  )
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/openfeature/go-feature-flag/goff_api.rb', line 14

def options
  @options
end

Instance Method Details

#evaluate_ofrep_api(flag_key:, evaluation_context:) ⇒ Object



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
48
49
50
51
52
53
54
55
56
# File 'lib/openfeature/go-feature-flag/goff_api.rb', line 23

def evaluate_ofrep_api(flag_key:, evaluation_context:)
  unless @retry_after.nil?
    if Time.now < @retry_after
      raise OpenFeature::GoFeatureFlag::RateLimited.new(nil)
    else
      @retry_after = nil
    end
  end

  evaluation_context = OpenFeature::SDK::EvaluationContext.new if evaluation_context.nil?
  # replace targeting_key by targetingKey
  evaluation_context.fields["targetingKey"] = evaluation_context.targeting_key
  evaluation_context.fields.delete("targeting_key")

  response = @faraday_connection.post("/ofrep/v1/evaluate/flags/#{flag_key}") do |req|
    req.body = {context: evaluation_context.fields}.to_json
  end

  case response.status
  when 200
    parse_success_response(response)
  when 400
    parse_error_response(response)
  when 401, 403
    raise OpenFeature::GoFeatureFlag::UnauthorizedError.new(response)
  when 404
    raise OpenFeature::GoFeatureFlag::FlagNotFoundError.new(response, flag_key)
  when 429
    parse_retry_later_header(response)
    raise OpenFeature::GoFeatureFlag::RateLimited.new(response)
  else
    raise OpenFeature::GoFeatureFlag::InternalServerError.new(response)
  end
end