Class: Prefab::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/prefab/client.rb

Constant Summary collapse

LOG =
Prefab::InternalLogger.new(self)
MAX_SLEEP_SEC =
10
BASE_SLEEP_SEC =
0.5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = Prefab::Options.new) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/prefab/client.rb', line 13

def initialize(options = Prefab::Options.new)
  @options = options.is_a?(Prefab::Options) ? options : Prefab::Options.new(options)
  @namespace = @options.namespace
  @stubs = {}
  @instance_hash = ::UUID.new.generate

  if @options.local_only?
    LOG.debug 'Prefab Running in Local Mode'
  elsif @options.datafile?
    LOG.debug 'Prefab Running in DataFile Mode'
  else
    @api_key = @options.api_key
    raise Prefab::Errors::InvalidApiKeyError, @api_key if @api_key.nil? || @api_key.empty? || api_key.count('-') < 1
  end

  context.clear

  Prefab::Context.global_context = @options.global_context

  # start config client
  config_client
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



11
12
13
# File 'lib/prefab/client.rb', line 11

def api_key
  @api_key
end

#instance_hashObject (readonly)

Returns the value of attribute instance_hash.



11
12
13
# File 'lib/prefab/client.rb', line 11

def instance_hash
  @instance_hash
end

#interceptorObject (readonly)

Returns the value of attribute interceptor.



11
12
13
# File 'lib/prefab/client.rb', line 11

def interceptor
  @interceptor
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



11
12
13
# File 'lib/prefab/client.rb', line 11

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/prefab/client.rb', line 11

def options
  @options
end

Instance Method Details

#config_client(timeout: 5.0) ⇒ Object



44
45
46
# File 'lib/prefab/client.rb', line 44

def config_client(timeout: 5.0)
  @config_client ||= Prefab::ConfigClient.new(self, timeout)
end

#contextObject



40
41
42
# File 'lib/prefab/client.rb', line 40

def context
  Prefab::Context.current
end

#context_shape_aggregatorObject



67
68
69
70
71
72
# File 'lib/prefab/client.rb', line 67

def context_shape_aggregator
  return nil if @options.collect_max_shapes <= 0

  @context_shape_aggregator ||= ContextShapeAggregator.new(client: self, max_shapes: @options.collect_max_shapes,
                                                           sync_interval: @options.collect_sync_interval)
end

#defined?(key) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/prefab/client.rb', line 134

def defined?(key)
  !!config_client.send(:raw, key)
end

#enabled?(feature_name, jit_context = NO_DEFAULT_PROVIDED) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/prefab/client.rb', line 102

def enabled?(feature_name, jit_context = NO_DEFAULT_PROVIDED)
  feature_flag_client.feature_is_on_for?(feature_name, jit_context)
end

#evaluation_summary_aggregatorObject



84
85
86
87
88
89
90
91
92
# File 'lib/prefab/client.rb', line 84

def evaluation_summary_aggregator
  return nil if @options.collect_max_evaluation_summaries <= 0

  @evaluation_summary_aggregator ||= EvaluationSummaryAggregator.new(
    client: self,
    max_keys: @options.collect_max_evaluation_summaries,
    sync_interval: @options.collect_sync_interval
  )
end

#example_contexts_aggregatorObject



74
75
76
77
78
79
80
81
82
# File 'lib/prefab/client.rb', line 74

def example_contexts_aggregator
  return nil if @options.collect_max_example_contexts <= 0

  @example_contexts_aggregator ||= ExampleContextsAggregator.new(
    client: self,
    max_contexts: @options.collect_max_example_contexts,
    sync_interval: @options.collect_sync_interval
  )
end

#feature_flag_clientObject



52
53
54
# File 'lib/prefab/client.rb', line 52

def feature_flag_client
  @feature_flag_client ||= Prefab::FeatureFlagClient.new(self)
end

#forkObject

When starting a forked process, use this to re-use the options on_worker_boot do

Prefab.fork

end



130
131
132
# File 'lib/prefab/client.rb', line 130

def fork
  Prefab::Client.new(@options.for_fork)
end

#get(key, default = NO_DEFAULT_PROVIDED, jit_context = NO_DEFAULT_PROVIDED) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/prefab/client.rb', line 106

def get(key, default = NO_DEFAULT_PROVIDED, jit_context = NO_DEFAULT_PROVIDED)
  if is_ff?(key)
    feature_flag_client.get(key, jit_context, default: default)
  else
    config_client.get(key, default, jit_context)
  end
end

#inspectObject



118
119
120
# File 'lib/prefab/client.rb', line 118

def inspect
  "#<Prefab::Client:#{object_id} namespace=#{namespace}>"
end

#is_ff?(key) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/prefab/client.rb', line 138

def is_ff?(key)
  raw = config_client.send(:raw, key)

  raw && raw.allowable_values.any?
end

#logObject



63
64
65
# File 'lib/prefab/client.rb', line 63

def log
  @log ||= Prefab::LoggerClient.new(client: self, log_path_aggregator: log_path_aggregator)
end

#log_path_aggregatorObject



56
57
58
59
60
61
# File 'lib/prefab/client.rb', line 56

def log_path_aggregator
  return nil if @options.collect_max_paths <= 0

  @log_path_aggregator ||= LogPathAggregator.new(client: self, max_paths: @options.collect_max_paths,
                                                 sync_interval: @options.collect_sync_interval)
end

#on_update(&block) ⇒ Object



98
99
100
# File 'lib/prefab/client.rb', line 98

def on_update(&block)
  resolver.on_update(&block)
end

#post(path, body) ⇒ Object



114
115
116
# File 'lib/prefab/client.rb', line 114

def post(path, body)
  Prefab::HttpConnection.new(@options.telemetry_destination, @api_key).post(path, body)
end

#resolverObject



122
123
124
# File 'lib/prefab/client.rb', line 122

def resolver
  config_client.resolver
end

#set_rails_loggersObject



94
95
96
# File 'lib/prefab/client.rb', line 94

def set_rails_loggers
  warn '[DEPRECATION] `set_rails_loggers` is deprecated since 1.6. Please use semantic_logger or `Prefab.log_filter` instead.'
end

#stopObject



48
49
50
# File 'lib/prefab/client.rb', line 48

def stop
  @config_client.stop
end

#with_context(properties, &block) ⇒ Object



36
37
38
# File 'lib/prefab/client.rb', line 36

def with_context(properties, &block)
  Prefab::Context.with_context(properties, &block)
end