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

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = Prefab::Options.new)
  @options = options.is_a?(Prefab::Options) ? options : Prefab::Options.new(options)
  @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_keyObject (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_hashObject (readonly)

Returns the value of attribute instance_hash.



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

def instance_hash
  @instance_hash
end

#interceptorObject (readonly)

Returns the value of attribute interceptor.



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

def interceptor
  @interceptor
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#prefab_api_urlObject (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_cacheObject (readonly)

Returns the value of attribute shared_cache.



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

def shared_cache
  @shared_cache
end

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

#contextObject



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

def context
  Prefab::Context.current
end

#enabled?(feature_name, lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#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

#get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, properties = NO_DEFAULT_PROVIDED, ff_default = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/prefab/client.rb', line 92

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

#inspectObject



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

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

#logObject



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_collector: log_path_collector)
end

#log_internal(level, msg, path = nil) ⇒ Object



82
83
84
# File 'lib/prefab/client.rb', line 82

def log_internal(level, msg, path = nil)
  log.log_internal msg, path, nil, level
end

#log_path_collectorObject



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

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

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

#on_update(&block) ⇒ Object



78
79
80
# File 'lib/prefab/client.rb', line 78

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

#post(path, body) ⇒ Object



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

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

#resolverObject



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

def resolver
  config_client.resolver
end

#set_rails_loggersObject



69
70
71
72
73
74
75
76
# File 'lib/prefab/client.rb', line 69

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