Class: Flagsmith::Client
- Inherits:
-
Object
- Object
- Flagsmith::Client
- Extended by:
- Forwardable
- Defined in:
- lib/flagsmith.rb
Overview
Ruby client for flagsmith.com
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Available Configs.
-
#environment ⇒ Object
readonly
Available Configs.
-
#identity_overrides_by_identifier ⇒ Object
readonly
Available Configs.
Instance Method Summary collapse
- #analytics_processor ⇒ Object
- #api_client ⇒ Object
- #engine ⇒ Object
- #environment_data_polling_manager ⇒ Object
- #environment_from_api ⇒ Object
- #feature_enabled?(feature_name, default: false) ⇒ Boolean
- #feature_enabled_for_identity?(feature_name, user_id, default: false) ⇒ Boolean
-
#get_environment_flags ⇒ Object
Get all the default for flags for the current environment.
-
#get_identity_flags(identifier, transient = false, **traits) ⇒ Object
Get all the flags for the current environment for a given identity.
- #get_identity_segments(identifier, traits = {}) ⇒ Object
- #get_value(feature_name, default: nil) ⇒ Object
- #get_value_for_identity(feature_name, user_id = nil, default: nil) ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #load_offline_handler ⇒ Object
- #realtime_client ⇒ Object
-
#update_environment ⇒ Object
Updates the environment state for local flag evaluation.
- #update_identity_overrides ⇒ Object
- #validate_offline_mode! ⇒ Object
- #validate_realtime_mode! ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/flagsmith.rb', line 58 def initialize(config) @_mutex = Mutex.new @config = Flagsmith::Config.new(config) @identity_overrides_by_identifier = {} validate_offline_mode! validate_realtime_mode! api_client analytics_processor environment_data_polling_manager engine load_offline_handler end |
Instance Attribute Details
#config ⇒ Object (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, :polling_manager_failure_limit :realtime_api_url, :enable_realtime_updates, :logger
You can see full description in the Flagsmith::Config
54 55 56 |
# File 'lib/flagsmith.rb', line 54 def config @config end |
#environment ⇒ Object (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, :polling_manager_failure_limit :realtime_api_url, :enable_realtime_updates, :logger
You can see full description in the Flagsmith::Config
54 55 56 |
# File 'lib/flagsmith.rb', line 54 def environment @environment end |
#identity_overrides_by_identifier ⇒ Object (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, :polling_manager_failure_limit :realtime_api_url, :enable_realtime_updates, :logger
You can see full description in the Flagsmith::Config
54 55 56 |
# File 'lib/flagsmith.rb', line 54 def identity_overrides_by_identifier @identity_overrides_by_identifier end |
Instance Method Details
#analytics_processor ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/flagsmith.rb', line 103 def analytics_processor return nil unless @config.enable_analytics? @analytics_processor ||= Flagsmith::AnalyticsProcessor.new( api_client: api_client, timeout: request_timeout_seconds, logger: @config.logger ) end |
#api_client ⇒ Object
91 92 93 |
# File 'lib/flagsmith.rb', line 91 def api_client @api_client ||= Flagsmith::ApiClient.new(@config) end |
#engine ⇒ Object
99 100 101 |
# File 'lib/flagsmith.rb', line 99 def engine @engine ||= Flagsmith::Engine::Engine.new end |
#environment_data_polling_manager ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/flagsmith.rb', line 118 def environment_data_polling_manager return nil unless @config.local_evaluation? # Bypass the environment data polling manager if realtime # is present in the configuration. if @config.realtime_mode? update_environment realtime_client.listen self unless realtime_client.running return end update_environment if @environment_data_polling_manager.nil? @environment_data_polling_manager ||= Flagsmith::EnvironmentDataPollingManager.new( self, environment_refresh_interval_seconds, @config.polling_manager_failure_limit ).tap(&:start) end |
#environment_from_api ⇒ Object
152 153 154 155 |
# File 'lib/flagsmith.rb', line 152 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
182 183 184 185 186 187 |
# File 'lib/flagsmith.rb', line 182 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
189 190 191 192 193 194 |
# File 'lib/flagsmith.rb', line 189 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_flags ⇒ Object
Get all the default for flags for the current environment.
159 160 161 162 163 |
# File 'lib/flagsmith.rb', line 159 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, transient = false, **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 } in lieu of a trait value, a trait coniguration dictionary can be provided, e.g. { “num_orders”: { “value”: 10, “transient”: true } } returns Flags object holding all the flags for the given identity.
176 177 178 179 180 |
# File 'lib/flagsmith.rb', line 176 def get_identity_flags(identifier, transient = false, **traits) # rubocop:disable Style/OptionalBooleanParameter return get_identity_flags_from_document(identifier, traits) if environment get_identity_flags_from_api(identifier, traits, transient) end |
#get_identity_segments(identifier, traits = {}) ⇒ Object
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/flagsmith.rb', line 210 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 = get_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
196 197 198 199 200 201 |
# File 'lib/flagsmith.rb', line 196 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
203 204 205 206 207 208 |
# File 'lib/flagsmith.rb', line 203 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_handler ⇒ Object
114 115 116 |
# File 'lib/flagsmith.rb', line 114 def load_offline_handler @environment = offline_handler.environment if offline_handler end |
#realtime_client ⇒ Object
95 96 97 |
# File 'lib/flagsmith.rb', line 95 def realtime_client @realtime_client ||= Flagsmith::RealtimeClient.new(@config) end |
#update_environment ⇒ Object
Updates the environment state for local flag evaluation. You only need to call this if you wish to bypass environment_refresh_interval_seconds.
138 139 140 141 |
# File 'lib/flagsmith.rb', line 138 def update_environment @_mutex.synchronize { @environment = environment_from_api } update_identity_overrides end |
#update_identity_overrides ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/flagsmith.rb', line 143 def update_identity_overrides return unless @environment @identity_overrides_by_identifier = {} @environment.identity_overrides.each do |identity| @identity_overrides_by_identifier[identity.identifier] = identity end end |
#validate_offline_mode! ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/flagsmith.rb', line 73 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 |
#validate_realtime_mode! ⇒ Object
84 85 86 87 88 89 |
# File 'lib/flagsmith.rb', line 84 def validate_realtime_mode! return unless @config.realtime_mode? && !@config.local_evaluation? raise Flagsmith::ClientError, 'The enable_realtime_updates config param requires a matching enable_local_evaluation param.' end |