Class: FluidFeatures::AppUser

Inherits:
Object
  • Object
show all
Defined in:
lib/fluidfeatures/app/user.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, user_id, display_name, is_anonymous, unique_attrs, cohort_attrs) ⇒ AppUser

Returns a new instance of AppUser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fluidfeatures/app/user.rb', line 10

def initialize(app, user_id, display_name, is_anonymous, unique_attrs, cohort_attrs)

  raise "app invalid : #{app}" unless app.is_a? ::FluidFeatures::App

  @app = app
  @unique_id = user_id
  @display_name = display_name
  @anonymous = is_anonymous
  @unique_attrs = unique_attrs
  @cohort_attrs = cohort_attrs

  if not unique_id or is_anonymous

    # We're an anonymous user
    @anonymous = true

    # if we were not given a user[:id] for this anonymous user, then get
    # it from an existing cookie or create a new one.
    unless unique_id
      # Create new unique id (for cookie). Use rand + micro-seconds of current time
      @unique_id = "anon-" + Random.rand(9999999999).to_s + "-" + ((Time.now.to_f * 1000000).to_i % 1000000).to_s
    end
  end

end

Instance Attribute Details

#anonymousObject

Returns the value of attribute anonymous.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def anonymous
  @anonymous
end

#appObject

Returns the value of attribute app.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def app
  @app
end

#cohort_attrsObject

Returns the value of attribute cohort_attrs.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def cohort_attrs
  @cohort_attrs
end

#display_nameObject

Returns the value of attribute display_name.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def display_name
  @display_name
end

#unique_attrsObject

Returns the value of attribute unique_attrs.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def unique_attrs
  @unique_attrs
end

#unique_idObject

Returns the value of attribute unique_id.



8
9
10
# File 'lib/fluidfeatures/app/user.rb', line 8

def unique_id
  @unique_id
end

Instance Method Details

#featuresObject

Returns all the features enabled for a specific user. This will depend on the user’s unique_id and how many users each feature is enabled for.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluidfeatures/app/user.rb', line 49

def features

  # extract just attribute ids into simple hash
  attribute_ids = {
    :anonymous => anonymous
  }
  [unique_attrs, cohort_attrs].each do |attrs|
    if attrs
      attrs.each do |attr_key, attr|
        if attr.is_a? Hash
          if attr.has_key? :id
            attribute_ids[attr_key] = attr[:id]
          end
        else
          attribute_ids[attr_key] = attr
        end
      end
    end
  end

  # normalize attributes ids as strings
  attribute_ids.each do |attr_key, attr_id|
    if attr_id.is_a? FalseClass or attr_id.is_a? TrueClass
      attribute_ids[attr_key] = attr_id.to_s.downcase
    elsif not attr_id.is_a? String
      attribute_ids[attr_key] = attr_id.to_s
    end
  end

  if ENV["FLUIDFEATURES_USER_FEATURES_FROM_API"]
    success, features_enabled = get("/features", attribute_ids) || {}
  else
    features_enabled = {}
    app.state.features.each do |feature_name, feature|
      feature["versions"].keys.each do |version_name|
        features_enabled[feature_name] ||= {}
        features_enabled[feature_name][version_name] = \
          app.state.feature_version_enabled_for_user(
            feature_name,
            version_name,
            unique_id,
            attribute_ids
          )
      end
    end
  end
  features_enabled
end

#get(path, params = nil) ⇒ Object



36
37
38
# File 'lib/fluidfeatures/app/user.rb', line 36

def get(path, params=nil)
  app.get("/user/#{unique_id}#{path}", params)
end

#post(path, payload) ⇒ Object



40
41
42
# File 'lib/fluidfeatures/app/user.rb', line 40

def post(path, payload)
  app.post("/user/#{unique_id}#{path}", payload)
end

#transaction(url) ⇒ Object



98
99
100
# File 'lib/fluidfeatures/app/user.rb', line 98

def transaction(url)
  ::FluidFeatures::AppUserTransaction.new(self, url)
end