Class: FeatureHub::Sdk::ClientContext

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

Overview

the context holding user data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ ClientContext

Returns a new instance of ClientContext.



20
21
22
23
# File 'lib/feature_hub/sdk/context.rb', line 20

def initialize(repo)
  @repo = repo
  @attributes = {}
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

#attribute_value(key, values) ⇒ Object

this takes an array parameter



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/feature_hub/sdk/context.rb', line 56

def attribute_value(key, values)
  if 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



123
124
125
# File 'lib/feature_hub/sdk/context.rb', line 123

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

#buildObject



131
132
133
# File 'lib/feature_hub/sdk/context.rb', line 131

def build
  self
end

#build_syncObject



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

def build_sync
  self
end

#clearObject



70
71
72
73
# File 'lib/feature_hub/sdk/context.rb', line 70

def clear
  @attributes = {}
  self
end

#country(value) ⇒ Object



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

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

#default_percentage_keyObject



79
80
81
82
83
84
85
86
# File 'lib/feature_hub/sdk/context.rb', line 79

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



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

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

#enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/feature_hub/sdk/context.rb', line 88

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

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/feature_hub/sdk/context.rb', line 127

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

#feature(key) ⇒ Object



92
93
94
# File 'lib/feature_hub/sdk/context.rb', line 92

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

#flag(key) ⇒ Object



119
120
121
# File 'lib/feature_hub/sdk/context.rb', line 119

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

#get_attr(key, default_val = nil) ⇒ Object



75
76
77
# File 'lib/feature_hub/sdk/context.rb', line 75

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

#json(key) ⇒ Object



108
109
110
111
112
113
# File 'lib/feature_hub/sdk/context.rb', line 108

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

  nil
end

#number(key) ⇒ Object



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

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

#platform(value) ⇒ Object



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

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

#raw_json(key) ⇒ Object



115
116
117
# File 'lib/feature_hub/sdk/context.rb', line 115

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

#session_key(value) ⇒ Object



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

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

#set?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#string(key) ⇒ Object



104
105
106
# File 'lib/feature_hub/sdk/context.rb', line 104

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

#user_key(value) ⇒ Object



25
26
27
28
# File 'lib/feature_hub/sdk/context.rb', line 25

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

#version(value) ⇒ Object



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

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