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.



54
55
56
57
58
59
60
61
62
# File 'lib/flagsmith.rb', line 54

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

  api_client
  analytics_processor
  environment_data_polling_manager
  engine
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 You can see full description in the Flagsmith::Config



50
51
52
# File 'lib/flagsmith.rb', line 50

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 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_processorObject



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_clientObject



64
65
66
# File 'lib/flagsmith.rb', line 64

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

#engineObject



68
69
70
# File 'lib/flagsmith.rb', line 68

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

#environment_data_polling_managerObject



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_apiObject



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

Returns:

  • (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

Returns:

  • (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_flagsObject

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)
  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_environmentObject

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