Class: Prefab::Client
- Inherits:
-
Object
- Object
- Prefab::Client
- Defined in:
- lib/prefab/client.rb
Constant Summary collapse
- MAX_SLEEP_SEC =
10- BASE_SLEEP_SEC =
0.5
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#instance_hash ⇒ Object
readonly
Returns the value of attribute instance_hash.
-
#interceptor ⇒ Object
readonly
Returns the value of attribute interceptor.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prefab_api_url ⇒ Object
readonly
Returns the value of attribute prefab_api_url.
-
#shared_cache ⇒ Object
readonly
Returns the value of attribute shared_cache.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #config_client(timeout: 5.0) ⇒ Object
- #context ⇒ Object
- #context_shape_aggregator ⇒ Object
- #enabled?(feature_name, lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) ⇒ Boolean
- #evaluated_keys_aggregator ⇒ Object
- #evaluation_summary_aggregator ⇒ Object
- #example_contexts_aggregator ⇒ Object
- #feature_flag_client ⇒ Object
-
#fork ⇒ Object
When starting a forked process, use this to re-use the options on_worker_boot do $prefab = $prefab.fork $prefab.set_rails_loggers end.
- #get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED, ff_default = nil) ⇒ Object
-
#initialize(options = Prefab::Options.new) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #log ⇒ Object
- #log_internal(level, msg, path = nil) ⇒ Object
- #log_path_aggregator ⇒ Object
- #on_update(&block) ⇒ Object
- #post(path, body) ⇒ Object
- #resolver ⇒ Object
- #set_rails_loggers ⇒ Object
- #with_context(properties, &block) ⇒ Object
- #with_log_context(_lookup_key, properties, &block) ⇒ Object
Constructor Details
#initialize(options = Prefab::Options.new) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/prefab/client.rb', line 12 def initialize( = Prefab::Options.new) @options = .is_a?(Prefab::Options) ? : Prefab::Options.new() @shared_cache = @options.shared_cache @stats = @options.stats @namespace = @options.namespace @stubs = {} @instance_hash = UUID.new.generate if @options.local_only? log_internal ::Logger::INFO, 'Prefab Running in Local 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_internal ::Logger::INFO, "Prefab Connecting to: #{@prefab_api_url}" end context.clear # start config client config_client end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def api_key @api_key end |
#instance_hash ⇒ Object (readonly)
Returns the value of attribute instance_hash.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def instance_hash @instance_hash end |
#interceptor ⇒ Object (readonly)
Returns the value of attribute interceptor.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def interceptor @interceptor end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def namespace @namespace end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def @options end |
#prefab_api_url ⇒ Object (readonly)
Returns the value of attribute prefab_api_url.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def prefab_api_url @prefab_api_url end |
#shared_cache ⇒ Object (readonly)
Returns the value of attribute shared_cache.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def shared_cache @shared_cache end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
10 11 12 |
# File 'lib/prefab/client.rb', line 10 def stats @stats 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 |
#context ⇒ Object
44 45 46 |
# File 'lib/prefab/client.rb', line 44 def context Prefab::Context.current end |
#context_shape_aggregator ⇒ Object
69 70 71 72 73 74 |
# File 'lib/prefab/client.rb', line 69 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, lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) ⇒ Boolean
120 121 122 123 124 |
# File 'lib/prefab/client.rb', line 120 def enabled?(feature_name, lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) _, properties = handle_positional_arguments(lookup_key, properties, :enabled?) feature_flag_client.feature_is_on_for?(feature_name, properties) end |
#evaluated_keys_aggregator ⇒ Object
76 77 78 79 80 81 |
# File 'lib/prefab/client.rb', line 76 def evaluated_keys_aggregator return nil if @options.collect_max_keys <= 0 @evaluated_keys_aggregator ||= EvaluatedKeysAggregator.new(client: self, max_keys: @options.collect_max_keys, sync_interval: @options.collect_sync_interval) end |
#evaluation_summary_aggregator ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/prefab/client.rb', line 93 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_aggregator ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/prefab/client.rb', line 83 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_client ⇒ Object
52 53 54 |
# File 'lib/prefab/client.rb', line 52 def feature_flag_client @feature_flag_client ||= Prefab::FeatureFlagClient.new(self) end |
#fork ⇒ Object
When starting a forked process, use this to re-use the options on_worker_boot do $prefab = $prefab.fork $prefab.set_rails_loggers end
153 154 155 |
# File 'lib/prefab/client.rb', line 153 def fork Prefab::Client.new(@options) end |
#get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED, ff_default = nil) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/prefab/client.rb', line 126 def get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED, ff_default = nil) if is_ff?(key) default, properties = handle_positional_arguments(default_or_lookup_key, properties, :get) feature_flag_client.get(key, properties, default: ff_default) else config_client.get(key, default_or_lookup_key, properties) end end |
#inspect ⇒ Object
140 141 142 |
# File 'lib/prefab/client.rb', line 140 def inspect "#<Prefab::Client:#{object_id} namespace=#{namespace}>" end |
#log ⇒ Object
63 64 65 66 67 |
# File 'lib/prefab/client.rb', line 63 def log @logger_client ||= Prefab::LoggerClient.new(@options.logdev, formatter: @options.log_formatter, prefix: @options.log_prefix, log_path_aggregator: log_path_aggregator) end |
#log_internal(level, msg, path = nil) ⇒ Object
116 117 118 |
# File 'lib/prefab/client.rb', line 116 def log_internal(level, msg, path = nil) log.log_internal msg, path, nil, level end |
#log_path_aggregator ⇒ Object
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
112 113 114 |
# File 'lib/prefab/client.rb', line 112 def on_update(&block) resolver.on_update(&block) end |
#post(path, body) ⇒ Object
136 137 138 |
# File 'lib/prefab/client.rb', line 136 def post(path, body) Prefab::HttpConnection.new(@options.prefab_api_url, @api_key).post(path, body) end |
#resolver ⇒ Object
144 145 146 |
# File 'lib/prefab/client.rb', line 144 def resolver config_client.resolver end |
#set_rails_loggers ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/prefab/client.rb', line 103 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) 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 |
#with_log_context(_lookup_key, properties, &block) ⇒ Object
35 36 37 38 |
# File 'lib/prefab/client.rb', line 35 def with_log_context(_lookup_key, properties, &block) warn '[DEPRECATION] `$prefab.with_log_context` is deprecated. Please use `with_context` instead.' with_context(properties, &block) end |