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.
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.
-
#update_environment ⇒ Object
Updates the environment state for local flag evaluation.
Constructor Details
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 You can see full description in the Flagsmith::Config
50 51 52 |
# File 'lib/flagsmith.rb', line 50 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 You can see full description in the Flagsmith::Config
50 51 52 |
# File 'lib/flagsmith.rb', line 50 def environment @environment end |
Instance Method Details
#analytics_processor ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/flagsmith.rb', line 72 def analytics_processor return nil unless @config.enable_analytics? @analytics_processor ||= Flagsmith::AnalyticsProcessor.new( api_client: api_client, timeout: request_timeout_seconds ) end |
#api_client ⇒ Object
64 65 66 |
# File 'lib/flagsmith.rb', line 64 def api_client @api_client ||= Flagsmith::ApiClient.new(@config) end |
#engine ⇒ Object
68 69 70 |
# File 'lib/flagsmith.rb', line 68 def engine @engine ||= Flagsmith::Engine::Engine.new end |
#environment_data_polling_manager ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/flagsmith.rb', line 82 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_api ⇒ Object
98 99 100 101 |
# File 'lib/flagsmith.rb', line 98 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
126 127 128 129 130 131 |
# File 'lib/flagsmith.rb', line 126 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
133 134 135 136 137 138 |
# File 'lib/flagsmith.rb', line 133 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.
105 106 107 108 109 |
# File 'lib/flagsmith.rb', line 105 def get_environment_flags # rubocop:disable Naming/AccessorMethodName return environment_flags_from_document if @config.local_evaluation? 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.
120 121 122 123 124 |
# File 'lib/flagsmith.rb', line 120 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
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/flagsmith.rb', line 154 def get_identity_segments(identifier, traits = {}) unless environment raise Flagsmith::ClientError, 'Local evaluation required to obtain identity segments.' end identity_model = build_identity_model(identifier, traits) segment_models = engine.get_identity_segments(environment, identity_model) return segment_models.map { |sm| Flagsmith::Segments::Segment.new(id: sm.id, name: sm.name) }.compact end |
#get_value(feature_name, default: nil) ⇒ Object
140 141 142 143 144 145 |
# File 'lib/flagsmith.rb', line 140 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
147 148 149 150 151 152 |
# File 'lib/flagsmith.rb', line 147 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 |
#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.
94 95 96 |
# File 'lib/flagsmith.rb', line 94 def update_environment @_mutex.synchronize { @environment = environment_from_api } end |