Class: Growthbook::FeatureRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/growthbook/feature_repository.rb

Overview

Optional class for fetching features from the GrowthBook API

Defined Under Namespace

Classes: FeatureFetchError, FeatureParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:, decryption_key:) ⇒ FeatureRepository

Returns a new instance of FeatureRepository.



18
19
20
21
22
# File 'lib/growthbook/feature_repository.rb', line 18

def initialize(endpoint:, decryption_key:)
  @endpoint = endpoint
  @decryption_key = decryption_key
  @features_json = {}
end

Instance Attribute Details

#decryption_keyObject (readonly)

String, nil

Optional key for decrypting an encrypted payload



13
14
15
# File 'lib/growthbook/feature_repository.rb', line 13

def decryption_key
  @decryption_key
end

#endpointObject (readonly)

String

The SDK endpoint



10
11
12
# File 'lib/growthbook/feature_repository.rb', line 10

def endpoint
  @endpoint
end

#features_jsonObject (readonly)

Parsed features JSON



16
17
18
# File 'lib/growthbook/feature_repository.rb', line 16

def features_json
  @features_json
end

Instance Method Details

#fetchObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/growthbook/feature_repository.rb', line 24

def fetch
  uri = URI(endpoint)
  res = Net::HTTP.get_response(uri)

  @response = res.is_a?(Net::HTTPSuccess) ? res.body : nil

  return nil if response.nil?

  if use_decryption?
    parsed_decrypted_response
  else
    parsed_plain_text_response
  end
rescue StandardError
  nil
end

#fetch!Object

Raises:



41
42
43
44
45
46
47
48
# File 'lib/growthbook/feature_repository.rb', line 41

def fetch!
  fetch

  raise FeatureFetchError if response.nil?
  raise FeatureParseError if features_json.nil? || features_json.empty?

  features_json
end