Class: Flagsmith::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/flagsmith.rb

Overview

Ruby client for flagsmith.com

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/flagsmith.rb', line 56

def initialize(config)
  @_mutex = Mutex.new
  @config = Flagsmith::Config.new(config)

  validate_offline_mode!

  api_client
  analytics_processor
  environment_data_polling_manager
  engine
  load_offline_handler
end

Instance Attribute Details

#configObject (readonly)

Available Configs.

:environment_key, :api_url, :custom_headers, :request_timeout_seconds, :enable_local_evaluation, :environment_refresh_interval_seconds, :retries, :enable_analytics, :default_flag_handler, :offline_mode, :offline_handler

You can see full description in the Flagsmith::Config



52
53
54
# File 'lib/flagsmith.rb', line 52

def config
  @config
end

#environmentObject (readonly)

Available Configs.

:environment_key, :api_url, :custom_headers, :request_timeout_seconds, :enable_local_evaluation, :environment_refresh_interval_seconds, :retries, :enable_analytics, :default_flag_handler, :offline_mode, :offline_handler

You can see full description in the Flagsmith::Config



52
53
54
# File 'lib/flagsmith.rb', line 52

def environment
  @environment
end

Instance Method Details

#analytics_processorObject



88
89
90
91
92
93
94
95
96
# File 'lib/flagsmith.rb', line 88

def analytics_processor
  return nil unless @config.enable_analytics?

  @analytics_processor ||=
    Flagsmith::AnalyticsProcessor.new(
      api_client: api_client,
      timeout: request_timeout_seconds
    )
end

#api_clientObject



80
81
82
# File 'lib/flagsmith.rb', line 80

def api_client
  @api_client ||= Flagsmith::ApiClient.new(@config)
end

#engineObject



84
85
86
# File 'lib/flagsmith.rb', line 84

def engine
  @engine ||= Flagsmith::Engine::Engine.new
end

#environment_data_polling_managerObject



102
103
104
105
106
107
108
109
110
# File 'lib/flagsmith.rb', line 102

def environment_data_polling_manager
  return nil unless @config.local_evaluation?

  update_environment

  @environment_data_polling_manager ||= Flagsmith::EnvironmentDataPollingManager.new(
    self, environment_refresh_interval_seconds
  ).tap(&:start)
end

#environment_from_apiObject



118
119
120
121
# File 'lib/flagsmith.rb', line 118

def environment_from_api
  environment_data = api_client.get(@config.environment_url).body
  Flagsmith::Engine::Environment.build(environment_data)
end

#feature_enabled?(feature_name, default: false) ⇒ Boolean

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'lib/flagsmith.rb', line 146

def feature_enabled?(feature_name, default: false)
  flag = get_environment_flags[feature_name]
  return default if flag.nil?

  flag.enabled?
end

#feature_enabled_for_identity?(feature_name, user_id, default: false) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
# File 'lib/flagsmith.rb', line 153

def feature_enabled_for_identity?(feature_name, user_id, default: false)
  flag = get_identity_flags(user_id)[feature_name]
  return default if flag.nil?

  flag.enabled?
end

#get_environment_flagsObject

Get all the default for flags for the current environment.



125
126
127
128
129
# File 'lib/flagsmith.rb', line 125

def get_environment_flags # rubocop:disable Naming/AccessorMethodName
  return environment_flags_from_document if @config.local_evaluation? || @config.offline_mode

  environment_flags_from_api
end

#get_identity_flags(identifier, **traits) ⇒ Object

Get all the flags for the current environment for a given identity. Will also upsert all traits to the Flagsmith API for future evaluations. Providing a trait with a value of None will remove the trait from the identity if it exists.

identifier a unique identifier for the identity in the current environment, e.g. email address, username, uuid traits { key => value } is a dictionary of traits to add / update on the identity in Flagsmith, e.g. { “num_orders”: 10 } returns Flags object holding all the flags for the given identity.



140
141
142
143
144
# File 'lib/flagsmith.rb', line 140

def get_identity_flags(identifier, **traits)
  return get_identity_flags_from_document(identifier, traits) if environment

  get_identity_flags_from_api(identifier, traits)
end

#get_identity_segments(identifier, traits = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/flagsmith.rb', line 174

def get_identity_segments(identifier, traits = {})
  unless environment
    raise Flagsmith::ClientError,
          'Local evaluation or offline handler is required to obtain identity segments.'
  end

  identity_model = build_identity_model(identifier, traits)
  segment_models = engine.get_identity_segments(environment, identity_model)
  segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact
end

#get_value(feature_name, default: nil) ⇒ Object



160
161
162
163
164
165
# File 'lib/flagsmith.rb', line 160

def get_value(feature_name, default: nil)
  flag = get_environment_flags[feature_name]
  return default if flag.nil?

  flag.value
end

#get_value_for_identity(feature_name, user_id = nil, default: nil) ⇒ Object



167
168
169
170
171
172
# File 'lib/flagsmith.rb', line 167

def get_value_for_identity(feature_name, user_id = nil, default: nil)
  flag = get_identity_flags(user_id)[feature_name]
  return default if flag.nil?

  flag.value
end

#load_offline_handlerObject



98
99
100
# File 'lib/flagsmith.rb', line 98

def load_offline_handler
  @environment = offline_handler.environment if offline_handler
end

#update_environmentObject

Updates the environment state for local flag evaluation. You only need to call this if you wish to bypass environment_refresh_interval_seconds.



114
115
116
# File 'lib/flagsmith.rb', line 114

def update_environment
  @_mutex.synchronize { @environment = environment_from_api }
end

#validate_offline_mode!Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/flagsmith.rb', line 69

def validate_offline_mode!
  if @config.offline_mode? && !@config.offline_handler
    raise Flagsmith::ClientError,
          'The offline_mode config param requires a matching offline_handler.'
  end
  return unless @config.offline_handler && @config.default_flag_handler

  raise Flagsmith::ClientError,
        'Cannot use offline_handler and default_flag_handler at the same time.'
end