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
35
36
37
# 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

    @prefab_api_url = @options.prefab_api_url
    LOG.debug "Prefab Connecting to: #{@prefab_api_url}"
  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

#prefab_api_urlObject (readonly)

Returns the value of attribute prefab_api_url.



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

def prefab_api_url
  @prefab_api_url
end

Instance Method Details

#config_client(timeout: 5.0) ⇒ Object



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

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

#contextObject



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

def context
  Prefab::Context.current
end

#context_shape_aggregatorObject



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

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)


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

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

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

Returns:

  • (Boolean)


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

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

#evaluation_summary_aggregatorObject



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

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



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

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



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

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



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

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

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



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

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



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

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

#is_ff?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

  raw && raw.allowable_values.any?
end

#logObject



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

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

#log_path_aggregatorObject



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

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



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

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

#post(path, body) ⇒ Object



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

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

#resolverObject



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

def resolver
  config_client.resolver
end

#set_rails_loggersObject



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

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

#with_context(properties, &block) ⇒ Object



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

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