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, **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
-
#update_environment ⇒ Object
Updates the environment state for local flag evaluation.
- #update_identity_overrides ⇒ Object
- #validate_offline_mode! ⇒ Object
Constructor Details
#initialize(config) ⇒ Client
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/flagsmith.rb', line 56 def initialize(config) @_mutex = Mutex.new @config = Flagsmith::Config.new(config) @identity_overrides_by_identifier = {} validate_offline_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
You can see full description in the Flagsmith::Config
52 53 54 |
# File 'lib/flagsmith.rb', line 52 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
You can see full description in the Flagsmith::Config
52 53 54 |
# File 'lib/flagsmith.rb', line 52 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
You can see full description in the Flagsmith::Config
52 53 54 |
# File 'lib/flagsmith.rb', line 52 def identity_overrides_by_identifier @identity_overrides_by_identifier end |
Instance Method Details
#analytics_processor ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/flagsmith.rb', line 89 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
81 82 83 |
# File 'lib/flagsmith.rb', line 81 def api_client @api_client ||= Flagsmith::ApiClient.new(@config) end |
#engine ⇒ Object
85 86 87 |
# File 'lib/flagsmith.rb', line 85 def engine @engine ||= Flagsmith::Engine::Engine.new end |
#environment_data_polling_manager ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/flagsmith.rb', line 104 def environment_data_polling_manager return nil unless @config.local_evaluation? update_environment if @environment_data_polling_manager.nil? @environment_data_polling_manager ||= Flagsmith::EnvironmentDataPollingManager.new( self, environment_refresh_interval_seconds ).tap(&:start) end |
#environment_from_api ⇒ Object
130 131 132 133 |
# File 'lib/flagsmith.rb', line 130 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
158 159 160 161 162 163 |
# File 'lib/flagsmith.rb', line 158 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
165 166 167 168 169 170 |
# File 'lib/flagsmith.rb', line 165 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.
137 138 139 140 141 |
# File 'lib/flagsmith.rb', line 137 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.
152 153 154 155 156 |
# File 'lib/flagsmith.rb', line 152 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
186 187 188 189 190 191 192 193 194 195 |
# File 'lib/flagsmith.rb', line 186 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
172 173 174 175 176 177 |
# File 'lib/flagsmith.rb', line 172 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
179 180 181 182 183 184 |
# File 'lib/flagsmith.rb', line 179 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
100 101 102 |
# File 'lib/flagsmith.rb', line 100 def load_offline_handler @environment = offline_handler.environment if offline_handler 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.
116 117 118 119 |
# File 'lib/flagsmith.rb', line 116 def update_environment @_mutex.synchronize { @environment = environment_from_api } update_identity_overrides end |
#update_identity_overrides ⇒ Object
121 122 123 124 125 126 127 128 |
# File 'lib/flagsmith.rb', line 121 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
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/flagsmith.rb', line 70 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 |