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
NO_DEFAULT_PROVIDED =
:no_default_provided

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/prefab/client.rb', line 20

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
  # start config client
  config_client
end

Instance Attribute Details

#api_key ⇒ Object (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/prefab/client.rb', line 15

def api_key
  @api_key
end

#instance_hash ⇒ Object (readonly)

Returns the value of attribute instance_hash.



18
19
20
# File 'lib/prefab/client.rb', line 18

def instance_hash
  @instance_hash
end

#interceptor ⇒ Object (readonly)

Returns the value of attribute interceptor.



14
15
16
# File 'lib/prefab/client.rb', line 14

def interceptor
  @interceptor
end

#namespace ⇒ Object (readonly)

Returns the value of attribute namespace.



13
14
15
# File 'lib/prefab/client.rb', line 13

def namespace
  @namespace
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/prefab/client.rb', line 17

def options
  @options
end

#prefab_api_url ⇒ Object (readonly)

Returns the value of attribute prefab_api_url.



16
17
18
# File 'lib/prefab/client.rb', line 16

def prefab_api_url
  @prefab_api_url
end

#shared_cache ⇒ Object (readonly)

Returns the value of attribute shared_cache.



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

def shared_cache
  @shared_cache
end

#stats ⇒ Object (readonly)

Returns the value of attribute stats.



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

def stats
  @stats
end

Instance Method Details

#config_client(timeout: 5.0) ⇒ Object



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

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

#enabled?(feature_name, lookup_key = nil, attributes = {}) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/prefab/client.rb', line 85

def enabled?(feature_name, lookup_key = nil, attributes = {})
  feature_flag_client.feature_is_on_for?(feature_name, lookup_key, attributes: attributes)
end

#feature_flag_client ⇒ Object



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

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

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



89
90
91
92
93
94
95
# File 'lib/prefab/client.rb', line 89

def get(key, default_or_lookup_key = NO_DEFAULT_PROVIDED, properties = {}, ff_default = nil)
  if is_ff?(key)
    feature_flag_client.get(key, default_or_lookup_key, properties, default: ff_default)
  else
    config_client.get(key, default_or_lookup_key, properties)
  end
end

#log ⇒ Object



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

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



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

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

#log_path_collector ⇒ Object



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

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

#post(path, body) ⇒ Object



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

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

#set_rails_loggers ⇒ Object



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

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

#with_log_context(lookup_key, properties) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/prefab/client.rb', line 41

def with_log_context(lookup_key, properties)
  Thread.current[:prefab_log_lookup_key] = lookup_key
  Thread.current[:prefab_log_properties] = properties

  yield
ensure
  Thread.current[:prefab_log_lookup_key] = nil
  Thread.current[:prefab_log_properties] = {}
end