Class: FeatureHub::Sdk::ClientContext

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

Overview

the context holding user data

Constant Summary collapse

WELL_KNOWN_KEY_METHODS =
{
  ContextKeys::USERKEY => :user_key,
  ContextKeys::SESSION => :session_key,
  ContextKeys::COUNTRY => :country,
  ContextKeys::PLATFORM => :platform,
  ContextKeys::DEVICE => :device,
  ContextKeys::VERSION => :version
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, attrs = nil) ⇒ ClientContext

Returns a new instance of ClientContext.



29
30
31
32
33
# File 'lib/feature_hub/sdk/context.rb', line 29

def initialize(repo, attrs = nil)
  @repo = repo
  @attributes = {}
  assign(attrs) if attrs
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



18
19
20
# File 'lib/feature_hub/sdk/context.rb', line 18

def repo
  @repo
end

Instance Method Details

#assign(attrs) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/feature_hub/sdk/context.rb', line 82

def assign(attrs)
  attrs.each do |key, value|
    sym_key = key.to_sym
    method_name = WELL_KNOWN_KEY_METHODS[sym_key]
    if method_name
      send(method_name, value)
    else
      attribute_value(sym_key, value)
    end
  end
  self
end

#attribute_value(key, values) ⇒ Object

this takes an array parameter



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/feature_hub/sdk/context.rb', line 66

def attribute_value(key, values)
  return self if key.nil? || key.to_s.empty?

  if values.nil? || values.empty?
    @attributes.delete(key.to_sym)
  else
    @attributes[key.to_sym] = if values.is_a?(Array)
                                values
                              else
                                [values]
                              end
  end

  self
end

#boolean(key) ⇒ Object



148
149
150
# File 'lib/feature_hub/sdk/context.rb', line 148

def boolean(key)
  feature(key).boolean
end

#buildObject



156
157
158
# File 'lib/feature_hub/sdk/context.rb', line 156

def build
  self
end

#build_syncObject



160
161
162
# File 'lib/feature_hub/sdk/context.rb', line 160

def build_sync
  self
end

#clearObject



95
96
97
98
# File 'lib/feature_hub/sdk/context.rb', line 95

def clear
  @attributes = {}
  self
end

#country(value) ⇒ Object



45
46
47
48
# File 'lib/feature_hub/sdk/context.rb', line 45

def country(value)
  @attributes[ContextKeys::COUNTRY] = [value]
  self
end

#default_percentage_keyObject



104
105
106
107
108
109
110
111
# File 'lib/feature_hub/sdk/context.rb', line 104

def default_percentage_key
  key = @attributes[ContextKeys::SESSION] || @attributes[ContextKeys::USERKEY]
  if key.nil? || key.empty?
    nil
  else
    key[0]
  end
end

#device(value) ⇒ Object



50
51
52
53
# File 'lib/feature_hub/sdk/context.rb', line 50

def device(value)
  @attributes[ContextKeys::DEVICE] = [value]
  self
end

#enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/feature_hub/sdk/context.rb', line 113

def enabled?(key)
  feature(key).enabled?
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/feature_hub/sdk/context.rb', line 152

def exists?(key)
  feature(key).exists?
end

#feature(key) ⇒ Object



117
118
119
# File 'lib/feature_hub/sdk/context.rb', line 117

def feature(key)
  @repo.feature(key).with_context(self)
end

#flag(key) ⇒ Object



144
145
146
# File 'lib/feature_hub/sdk/context.rb', line 144

def flag(key)
  feature(key).flag
end

#get_attr(key, default_val = nil) ⇒ Object



100
101
102
# File 'lib/feature_hub/sdk/context.rb', line 100

def get_attr(key, default_val = nil)
  (@attributes[key.to_sym] || [default_val]).compact
end

#json(key) ⇒ Object



133
134
135
136
137
138
# File 'lib/feature_hub/sdk/context.rb', line 133

def json(key)
  data = feature(key).raw_json
  return JSON.parse(data) if data

  nil
end

#number(key) ⇒ Object



125
126
127
# File 'lib/feature_hub/sdk/context.rb', line 125

def number(key)
  feature(key).number
end

#platform(value) ⇒ Object



55
56
57
58
# File 'lib/feature_hub/sdk/context.rb', line 55

def platform(value)
  @attributes[ContextKeys::PLATFORM] = [value]
  self
end

#raw_json(key) ⇒ Object



140
141
142
# File 'lib/feature_hub/sdk/context.rb', line 140

def raw_json(key)
  feature(key).raw_json
end

#session_key(value) ⇒ Object



40
41
42
43
# File 'lib/feature_hub/sdk/context.rb', line 40

def session_key(value)
  @attributes[ContextKeys::SESSION] = [value]
  self
end

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/feature_hub/sdk/context.rb', line 121

def set?(key)
  feature(key).set?
end

#string(key) ⇒ Object



129
130
131
# File 'lib/feature_hub/sdk/context.rb', line 129

def string(key)
  feature(key).string
end

#user_key(value) ⇒ Object



35
36
37
38
# File 'lib/feature_hub/sdk/context.rb', line 35

def user_key(value)
  @attributes[ContextKeys::USERKEY] = [value]
  self
end

#version(value) ⇒ Object



60
61
62
63
# File 'lib/feature_hub/sdk/context.rb', line 60

def version(value)
  @attributes[ContextKeys::VERSION] = [value]
  self
end