Class: Unleash::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/context.rb

Constant Summary collapse

ATTRS =
[:app_name, :environment, :user_id, :session_id, :remote_address, :current_time].freeze

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Context

Returns a new instance of Context.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/unleash/context.rb', line 7

def initialize(params = {})
  raise ArgumentError, "Unleash::Context must be initialized with a hash." unless params.is_a?(Hash)

  self.app_name = value_for("appName", params, Unleash&.configuration&.app_name)
  self.environment = value_for("environment", params, Unleash&.configuration&.environment || "default")
  self.user_id = value_for("userId", params)&.to_s
  self.session_id = value_for("sessionId", params)
  self.remote_address = value_for("remoteAddress", params)
  self.current_time = value_for("currentTime", params, Time.now.utc.iso8601.to_s)

  properties = value_for("properties", params)
  self.properties = properties.is_a?(Hash) ? properties.transform_keys(&:to_sym) : {}
end

Instance Method Details

#as_json(*_options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/unleash/context.rb', line 26

def as_json(*_options)
  {
    appName: to_safe_value(self.app_name),
    environment: to_safe_value(self.environment),
    userId: to_safe_value(self.user_id),
    sessionId: to_safe_value(self.session_id),
    remoteAddress: to_safe_value(self.remote_address),
    currentTime: to_safe_value(self.current_time),
    properties: self.properties.transform_values{ |value| to_safe_value(value) }
  }
end

#get_by_name(name) ⇒ Object

returns the value found for the key in the context, or raises a KeyError exception if not found.



47
48
49
50
51
52
53
54
55
# File 'lib/unleash/context.rb', line 47

def get_by_name(name)
  normalized_name = underscore(name).to_sym

  if ATTRS.include? normalized_name
    self.send(normalized_name)
  else
    self.properties.fetch(normalized_name, nil) || self.properties.fetch(name.to_sym)
  end
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/unleash/context.rb', line 57

def include?(name)
  normalized_name = underscore(name)
  return self.instance_variable_defined? "@#{normalized_name}" if ATTRS.include? normalized_name.to_sym

  self.properties.include?(normalized_name.to_sym) || self.properties.include?(name.to_sym)
end

#to_hObject



42
43
44
# File 'lib/unleash/context.rb', line 42

def to_h
  ATTRS.map{ |attr| [attr, self.send(attr)] }.to_h.merge(properties: @properties)
end

#to_json(*options) ⇒ Object



38
39
40
# File 'lib/unleash/context.rb', line 38

def to_json(*options)
  as_json(*options).to_json(*options)
end

#to_sObject



21
22
23
24
# File 'lib/unleash/context.rb', line 21

def to_s
  "<Context: user_id=#{@user_id},session_id=#{@session_id},remote_address=#{@remote_address},properties=#{@properties}" \
  ",app_name=#{@app_name},environment=#{@environment},current_time=#{@current_time}>"
end