Class: FeatBit::OpenFeature::Provider

Inherits:
Object
  • Object
show all
Includes:
OpenFeature::SDK::Provider::EventEmitter
Defined in:
lib/featbit/openfeature/provider.rb

Overview

Owns the supplied or newly created client and closes it on shutdown.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, client: nil) ⇒ Provider

Returns a new instance of Provider.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/featbit/openfeature/provider.rb', line 11

def initialize(options = nil, client: nil)
  raise ArgumentError, "Provide options or client, not both" if options && client
  unless client || options.is_a?(FeatBit::Options)
    raise ArgumentError, "FeatBit::Options or a FeatBit client is required"
  end

  @client = client || FeatBit::Client.new(options)
  @metadata = ::OpenFeature::SDK::Provider::ProviderMetadata.new(name: "FeatBit").freeze
  @status_listener = @client.status_provider.add_listener { |status, message| status_changed(status, message) }
  @flag_listener = @client.add_flag_change_listener do |key|
    emit_event(::OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, flags_changed: [key])
  end
end

Instance Attribute Details

#client ⇒ Object (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/featbit/openfeature/provider.rb', line 9

def client
  @client
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



9
10
11
# File 'lib/featbit/openfeature/provider.rb', line 9

def 
  @metadata
end

Instance Method Details

#init(_evaluation_context = nil) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/featbit/openfeature/provider.rb', line 25

def init(_evaluation_context = nil)
  return if client.initialized? && [FeatBit::Status::READY, FeatBit::Status::INTERRUPTED,
                                   FeatBit::Status::OFFLINE].include?(client.status_provider.status)

  raise ContextConverter::InvalidContext.new(
    ::OpenFeature::SDK::Provider::ErrorCode::PROVIDER_NOT_READY, "FeatBit client is not ready"
  )
end

#shutdown ⇒ Object



34
35
36
37
38
39
# File 'lib/featbit/openfeature/provider.rb', line 34

def shutdown
  client.status_provider.remove_listener(@status_listener)
  client.remove_flag_change_listener(@flag_listener)
  detach
  client.close
end

#track(tracking_event_name, evaluation_context: nil, tracking_event_details: nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/featbit/openfeature/provider.rb', line 47

def track(tracking_event_name, evaluation_context: nil, tracking_event_details: nil)
  user = ContextConverter.convert(evaluation_context)
  client.track(user, tracking_event_name, tracking_event_details&.value || 1.0)
rescue ContextConverter::InvalidContext
  false
end