fluidfeatures-rails 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module FluidFeatures
2
2
  module Rails
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
  end
@@ -118,25 +118,58 @@ module FluidFeatures
118
118
  # This will depend on the user_id and how many users each
119
119
  # feature is enabled for.
120
120
  #
121
- def self.get_user_features(user_id)
122
- if not user_id
123
- raise "user_id is not given for get_user_features"
121
+ def self.get_user_features(user)
122
+
123
+ if not user
124
+ raise "user object should be a Hash"
125
+ end
126
+ if not user[:id]
127
+ raise "user does not contain :id field"
128
+ end
129
+
130
+ # extract just attribute ids into simple hash
131
+ attribute_ids = {
132
+ :anonymous => !!user[:anonymous]
133
+ }
134
+ [:unique, :cohorts].each do |attr_type|
135
+ if user.has_key? attr_type
136
+ user[attr_type].each do |attr_key, attr|
137
+ if attr.is_a? Hash
138
+ if attr.has_key? :id
139
+ attribute_ids[attr_key] = attr[:id]
140
+ end
141
+ else
142
+ attribute_ids[attr_key] = attr
143
+ end
144
+ end
145
+ end
124
146
  end
147
+
148
+ # normalize attributes ids as strings
149
+ attribute_ids.each do |attr_key, attr_id|
150
+ if attr_id.is_a? FalseClass or attr_id.is_a? TrueClass
151
+ attribute_ids[attr_key] = attr_id.to_s.downcase
152
+ elsif not attr_id.is_a? String
153
+ attribute_ids[attr_key] = attr_id.to_s
154
+ end
155
+ end
156
+
125
157
  features = {}
126
158
  fetch_start_time = Time.now
127
159
  begin
128
- uri = URI(@@baseuri + "/app/" + @@app_id.to_s + "/user/" + user_id.to_s + "/features")
129
- request = Net::HTTP::Get.new uri.path
160
+ uri = URI("#{@@baseuri}/app/#{@@app_id}/user/#{user[:id]}/features")
161
+ uri.query = URI.encode_www_form( attribute_ids )
162
+ request = Net::HTTP::Get.new "#{uri.path}?#{uri.query}"
130
163
  request["Accept"] = "application/json"
131
164
  request['AUTHORIZATION'] = @@secret
132
165
  response = @@http.request request
133
166
  if response.is_a?(Net::HTTPSuccess)
134
167
  features = JSON.parse(response.body)
135
168
  else
136
- ::Rails.logger.error "[" + response.code.to_s + "] Failed to get user features : " + uri.to_s + " : " + response.body.to_s
169
+ ::Rails.logger.error "[#{response.code}] Failed to get user features : #{uri} : #{response.body}"
137
170
  end
138
171
  rescue
139
- ::Rails.logger.error "Request to get user features failed : " + uri.to_s
172
+ ::Rails.logger.error "Request to get user features failed : #{uri}"
140
173
  raise
141
174
  end
142
175
  @@last_fetch_duration = Time.now - fetch_start_time
@@ -220,6 +253,7 @@ module ActionController
220
253
  end
221
254
  else
222
255
  # We're an anonymous user
256
+ user[:anonymous] = true
223
257
 
224
258
  # if we were not given a user[:id] for this anonymous user, then get
225
259
  # it from an existing cookie or create a new one.
@@ -313,7 +347,8 @@ module ActionController
313
347
  # Returns the features enabled for this request's user.
314
348
  #
315
349
  def fluidfeatures_retrieve_user_features
316
- @ff_features = FluidFeatures::Rails.get_user_features(fluidfeatures_user[:id])
350
+ user = fluidfeatures_user
351
+ @ff_features = FluidFeatures::Rails.get_user_features(user)
317
352
  end
318
353
 
319
354
  #
@@ -341,11 +376,10 @@ module ActionController
341
376
  },
342
377
  :url => url
343
378
  }
344
- if fluidfeatures_user[:anonymous]
345
- (payload[:user] ||= {})[:anonymous] = true
346
- end
347
- if fluidfeatures_user[:attributes]
348
- (payload[:user] ||= {})[:attributes] = fluidfeatures_user[:attributes]
379
+ [:name, :anonymous, :unique, :cohorts].each do |key|
380
+ if fluidfeatures_user[key]
381
+ (payload[:user] ||= {})[key] = fluidfeatures_user[key]
382
+ end
349
383
  end
350
384
  FluidFeatures::Rails.log_request(fluidfeatures_user[:id], payload)
351
385
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluidfeatures-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-04 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: persistent_http