Module: Lucerna

Defined in:
lib/lucerna.rb,
lib/lucerna/http.rb,
lib/lucerna/config.rb,
lib/lucerna/errors.rb,
lib/lucerna/traits.rb,
lib/lucerna/railtie.rb,
lib/lucerna/version.rb,
lib/lucerna/identity.rb,
lib/lucerna/reporting.rb,
lib/lucerna/gates/client.rb,
lib/lucerna/gates/hashing.rb,
lib/lucerna/gates/runtime.rb,
lib/lucerna/gates/evaluate.rb,
lib/lucerna/gates/exposures.rb,
lib/lucerna/identity/client.rb

Overview

Lucerna SDK for Ruby — Gates feature flags/experiments and Identity.

Lucerna.configure do |c|
c.server_key = ENV["LUCERNA_SERVER_KEY"]
end

identity = Lucerna::Identity.new(user_id: user.id.to_s, traits: { plan: "pro" })
Lucerna.gates.flag("new_billing", identity)
Lucerna.identity.identify(identity)

Defined Under Namespace

Modules: Gates, HTTP, Reporting, Traits Classes: AuthError, Config, Error, Identity, Railtie, RequestError

Constant Summary collapse

VERSION =
"0.2.0"
SDK_IDENTITY =

Value of the x-lucerna-sdk header on every request.

"ruby/#{VERSION}"

Class Method Summary collapse

Class Method Details

.close(timeout: 2) ⇒ Object

Stops polling/delivery and flushes queued exposures and identifies. Also registered via at_exit when the first singleton is built. The clients keep answering stale reads after close.



66
67
68
69
70
71
# File 'lib/lucerna.rb', line 66

def close(timeout: 2)
  gates_client, identity_client = @mutex.synchronize { [@gates, @identity] }
  gates_client&.close(timeout: timeout)
  identity_client&.close(timeout: timeout)
  nil
end

.configObject



33
34
35
# File 'lib/lucerna.rb', line 33

def config
  @config || @mutex.synchronize { @config ||= Config.new }
end

.configure {|configuration| ... } ⇒ Object

Yields the Config. Must run before the first use of Lucerna.gates / Lucerna.identity — configuring an already-built client would be a silent no-op, so it raises instead.

Yields:

  • (configuration)


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lucerna.rb', line 40

def configure
  configuration = @mutex.synchronize do
    if @gates || @identity
      raise Error,
        "Lucerna.configure must run before the first use of Lucerna.gates / " \
        "Lucerna.identity — or call Lucerna.restart first"
    end
    @config ||= Config.new
  end
  yield configuration if block_given?
  configuration
end

.gatesObject

The Gates client singleton, built lazily from config on first use.



54
55
56
# File 'lib/lucerna.rb', line 54

def gates
  @gates || @mutex.synchronize { @gates ||= build_gates }
end

.identityObject

The Identity delivery client singleton, built lazily on first use.



59
60
61
# File 'lib/lucerna.rb', line 59

def identity
  @identity || @mutex.synchronize { @identity ||= build_identity }
end

.restartObject

Abandons the current clients (no flush — the parent process owns its own at_exit flush) and lets the next access rebuild them. For Puma on_worker_boot / Unicorn after_fork / Sidekiq startup; optional, since the clients also self-heal on first read after a fork.



77
78
79
80
81
82
83
# File 'lib/lucerna.rb', line 77

def restart
  @mutex.synchronize do
    @gates = nil
    @identity = nil
  end
  nil
end