Class: Prefab::Client

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

Constant Summary collapse

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

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
38
# 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
  Prefab::LoggerClient.new(@options.logdev, formatter: @options.log_formatter,
                            prefix: @options.log_prefix,
                           log_path_aggregator: log_path_aggregator
  )

  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
  # 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



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

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

#contextObject



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

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

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

Returns:

  • (Boolean)


109
110
111
# File 'lib/prefab/client.rb', line 109

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 = $prefab.fork
$prefab.set_rails_loggers

end



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

def fork
  log_options = self.log.context_keys.to_a # get keys pre-fork
  Prefab::Client.new(@options.for_fork).tap do |client|
    client.log.add_context_keys(*log_options)
  end
end

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



113
114
115
116
117
118
119
# File 'lib/prefab/client.rb', line 113

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



125
126
127
# File 'lib/prefab/client.rb', line 125

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

#logObject



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

def log
  Prefab::LoggerClient.instance
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



105
106
107
# File 'lib/prefab/client.rb', line 105

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

#post(path, body) ⇒ Object



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

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

#resolverObject



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

def resolver
  config_client.resolver
end

#set_rails_loggersObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/prefab/client.rb', line 94

def set_rails_loggers
  Rails.logger = log
  ActionView::Base.logger = log
  ActionController::Base.logger = log
  ActiveJob::Base.logger = log if defined?(ActiveJob)
  ActiveRecord::Base.logger = log
  ActiveStorage.logger = log if defined?(ActiveStorage)

  LogSubscribers::ActionControllerSubscriber.attach_to :action_controller unless @options.disable_action_controller_logging
end

#with_context(properties, &block) ⇒ Object



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

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