kameleoon-client-ruby 1.0.3 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d6757ccb01117c020dd6e555554cd7d02ee6649a107e309f2998e8abbca17ea
4
- data.tar.gz: 331997c2f44f3024d9080aff075462c48a6c013d4f939b79975ea1a6717748fc
3
+ metadata.gz: 1a893238d33ee61086e79cb093d442dfefb16fac9e757ea7353d60458869cd13
4
+ data.tar.gz: 8485bb0f875a2fa286a03fe872b88dea170477200fc1429d84b5d28711463885
5
5
  SHA512:
6
- metadata.gz: ccba30fc711816db2aa401159c1b110f97dbd5dd36ca5108cfa88aa5a7aedd6beef39d912a2351cff3d98e6e6ffd8ee2d394bf6394b9b5d8d6a23c978cf09789
7
- data.tar.gz: 3e1c47577cd4cc0889b26d9e5c38834f24c0bfc1b4c294b65ab57117a613204cc95f923caa4bafeb345245b860f37579cf0cbe51c4f034d0e79499f520449c2a
6
+ metadata.gz: 0ff7b4461c7c4bbf8a7c5399450db7920bae12e65ed9eb604718f9e51fd33a79f24c4803e144ba7260946b1f9c50e2832ff55459c190d33eb2a65658b5c0575e
7
+ data.tar.gz: f8e17afae3086d5b0402a30b7121febed07b16eb58a05e3082d73100c4e28d778746c353bce2bd50620b0dc267e8eb427b89e30dd066f9f951519cc5c67b77a4
@@ -116,16 +116,16 @@ module Kameleoon
116
116
  variation_id.to_i
117
117
  else
118
118
  visitor_data = @data.select { |key, value| key.to_s == visitor_code }.values.flatten! || []
119
- if experiment['targetingSegment'].check_tree(visitor_data)
119
+ if experiment['targetingSegment'].nil? || experiment['targetingSegment'].check_tree(visitor_data)
120
120
  threshold = obtain_hash_double(visitor_code, experiment['respoolTime'], experiment['id'])
121
121
  experiment['deviations'].each do |key, value|
122
122
  threshold -= value
123
123
  if threshold < 0
124
- post_beacon("experimentTracking", visitor_code, experiment_id, key)
124
+ track_experiment(visitor_code, experiment_id, key)
125
125
  return key.to_s.to_i
126
126
  end
127
127
  end
128
- post_beacon("experimentTracking", visitor_code, experiment_id, REFERENCE, true)
128
+ track_experiment(visitor_code, experiment_id, REFERENCE, true)
129
129
  raise Exception::NotActivated.new(visitor_code)
130
130
  end
131
131
  raise Exception::NotTargeted.new(visitor_code)
@@ -182,7 +182,7 @@ module Kameleoon
182
182
  # @param [String] visitor_code Optional field - Visitor code, without visitor code it flush all of the data
183
183
  #
184
184
  def flush(visitor_code = nil)
185
- post_beacon("dataTracking", visitor_code)
185
+ track_data(visitor_code)
186
186
  end
187
187
 
188
188
  ##
@@ -231,7 +231,7 @@ module Kameleoon
231
231
  connexion_options = { :connect_timeout => (timeout.to_f / 1000.0) }
232
232
  request_options = {
233
233
  :path => get_experiment_register_url(visitor_code, id),
234
- :body => (select_data_to_sent(visitor_code).values.map { |data| data.obtain_full_post_text_line }.join("\n") || "").encode("UTF-8")
234
+ :body => (data_not_sent(visitor_code).values.map { |data| data.obtain_full_post_text_line }.join("\n") || "").encode("UTF-8")
235
235
  }
236
236
  log "Activate feature request: " + request_options.inspect
237
237
  log "Activate feature connexion:" + connexion_options.inspect
@@ -251,10 +251,10 @@ module Kameleoon
251
251
  if feature_flag['targetingSegment'].nil? || feature_flag['targetingSegment'].check_tree(visitor_data)
252
252
  threshold = obtain_hash_double(visitor_code, {}, id)
253
253
  if threshold <= feature_flag['expositionRate']
254
- post_beacon("experimentTracking", visitor_code, id, feature_flag["variationsId"].first)
254
+ track_experiment(visitor_code, id, feature_flag["variationsId"].first)
255
255
  return true
256
256
  else
257
- post_beacon("experimentTracking", visitor_code, id, REFERENCE, true)
257
+ track_experiment(visitor_code, id, REFERENCE, true)
258
258
  return false
259
259
  end
260
260
  else
@@ -401,7 +401,7 @@ module Kameleoon
401
401
  query_values = { 'perPage' => per_page }
402
402
  filters = [
403
403
  hash_filter('siteId', 'EQUAL', [site_id]),
404
- hash_filter('status', 'EQUAL', ['ACTIVE']),
404
+ hash_filter('status', 'IN', ['ACTIVE', 'DEVIATED']),
405
405
  hash_filter('type', 'IN', ['SERVER_SIDE', 'HYBRID'])
406
406
  ]
407
407
  experiments = fetch_all('experiments', query_values, filters).map { |it| JSON.parse(it.response) }.flatten.map do |test|
@@ -472,6 +472,10 @@ module Kameleoon
472
472
  url
473
473
  end
474
474
 
475
+ def get_data_register_url(visitor_code)
476
+ "/dataTracking?" + URI.encode_www_form(get_common_ssx_parameters(visitor_code))
477
+ end
478
+
475
479
  def get_feature_flag(feature_key)
476
480
  if feature_key.is_a?(String)
477
481
  feature_flag = @feature_flags.select { |ff| ff['identificationKey'] == feature_key}.first
@@ -486,21 +490,48 @@ module Kameleoon
486
490
  feature_flag
487
491
  end
488
492
 
489
- def post_beacon(type = "dataTracking", visitor_code = nil, experiment_id = nil, variation_id = nil, none_variation = false)
493
+ def track_experiment(visitor_code, experiment_id, variation_id = nil, none_variation = false)
494
+ data_not_sent = data_not_sent(visitor_code)
495
+ options = {
496
+ :path => get_experiment_register_url(visitor_code, experiment_id, variation_id, none_variation),
497
+ :body => ((data_not_sent.values[0] || []).map{ |it| it.obtain_full_post_text_line }.join("\n") || "").encode("UTF-8"),
498
+ :head => { "Content-Type" => "text/plain"}
499
+ }
500
+ trial = 0
501
+ log "Start post tracking experiment: " + data_not_sent.inspect
502
+ Thread.new do
503
+ EM.synchrony do
504
+ while trial < 10
505
+ request = EM::Synchrony.sync post(options, @tracking_url)
506
+ log "Request " + request.inspect
507
+ if is_successful(request)
508
+ (data_not_sent.values[0] || []).each { |it| it.sent = true }
509
+ EM.stop
510
+ end
511
+ trial += 1
512
+ end
513
+ EM.stop
514
+ end
515
+ log "Post to experiment tracking is done after " + trial.to_s + " trials"
516
+ Thread.exit
517
+ end
518
+ end
519
+
520
+ def track_data(visitor_code = nil)
490
521
  Thread.new do
491
522
  EM.synchrony do
492
- entries = select_data_to_sent(visitor_code)
493
- log "Start post to tracking: " + entries.inspect
494
523
  trials = 10
495
524
  concurrency = 1
496
- while !entries.empty? && trials > 0
497
- log "Trials amount left: " + trials.to_s
498
- EM::Synchrony::Iterator.new(entries, concurrency).map do |entry, iter|
525
+ data_not_sent = data_not_sent(visitor_code)
526
+ log "Start post tracking data: " + data_not_sent.inspect
527
+ while trials > 0 && !data_not_sent.empty?
528
+ EM::Synchrony::Iterator.new(data_not_sent, concurrency).map do |entry, iter|
499
529
  options = {
500
- :path => build_beacon_path(type, entry.first || visitor_code, experiment_id, variation_id, none_variation),
530
+ :path => get_data_register_url(entry.first),
501
531
  :body => (entry.last.map { |data| data.obtain_full_post_text_line }.join("\n") || "").encode("UTF-8"),
502
532
  :head => { "Content-Type" => "text/plain" }
503
533
  }
534
+ log "Post tracking data for visitor_code: " + entry.first + " with options: " + options.inspect
504
535
  request = post(options, @tracking_url)
505
536
  request.callback {
506
537
  if is_successful(request)
@@ -510,17 +541,17 @@ module Kameleoon
510
541
  }
511
542
  request.errback { iter.return(request) }
512
543
  end
513
- entries = select_data_to_sent(visitor_code)
544
+ data_not_sent = data_not_sent(visitor_code)
514
545
  trials -= 1
515
546
  end
516
- log "Post to tracking done"
547
+ log "Post to data tracking is done."
517
548
  EM.stop
518
549
  end
519
550
  Thread.exit
520
551
  end
521
552
  end
522
553
 
523
- def select_data_to_sent(visitor_code)
554
+ def data_not_sent(visitor_code = nil)
524
555
  if visitor_code.nil?
525
556
  @data.select {|key, values| values.any? {|data| !data.sent}}
526
557
  else
@@ -528,16 +559,6 @@ module Kameleoon
528
559
  end
529
560
  end
530
561
 
531
- def build_beacon_path(type, visitor_code, experiment_id = nil, variation_id = nil, none_variation = nil)
532
- if type == "dataTracking"
533
- return "/dataTracking?" + URI.encode_www_form(get_common_ssx_parameters(visitor_code))
534
- elsif type == "experimentTracking"
535
- return get_experiment_register_url(visitor_code, experiment_id, variation_id, none_variation)
536
- else
537
- raise TypeError("Unknown type for post_beacon: " + type.to_s)
538
- end
539
- end
540
-
541
562
  def log(text)
542
563
  if @verbose_mode
543
564
  print "Kameleoon Log: " + text.to_s + "\n"
@@ -3,6 +3,7 @@ require 'em-http'
3
3
  require 'eventmachine'
4
4
  require 'bigdecimal'
5
5
  require 'kameleoon/utils'
6
+ require 'kameleoon/exceptions'
6
7
 
7
8
  module Kameleoon
8
9
  # @api private
@@ -33,16 +34,22 @@ module Kameleoon
33
34
  private
34
35
 
35
36
  KAMELEOON_KEY_JS_COOKIE = "_js_"
37
+ KAMELEOON_VISITOR_CODE_LENGTH = 255
36
38
  KAMELEOON_COOKIE_VALUE_LENGTH = 16
37
- KAMELEOON_VISITOR_CODE_LENGTH = 100
38
39
  KAMELEOON_COOKIE_NAME = 'kameleoonVisitorCode'
39
40
  EXPIRE_DAYS = 380
40
41
 
41
42
  def check_default_visitor_code(default_visitor_code)
42
43
  if default_visitor_code.nil?
43
- return nil
44
+ return
45
+ end
46
+ if default_visitor_code.length == 0
47
+ raise Kameleoon::Exception::VisitorCodeNotValid.new("Empty visitor Code")
48
+ end
49
+ if default_visitor_code.length > KAMELEOON_VISITOR_CODE_LENGTH
50
+ raise Kameleoon::Exception::VisitorCodeNotValid.new("Visitor Code is longer than 255 chars.")
44
51
  end
45
- default_visitor_code[0..(KAMELEOON_VISITOR_CODE_LENGTH - 1)]
52
+ default_visitor_code
46
53
  end
47
54
 
48
55
  def read(cookies)
@@ -54,10 +61,10 @@ module Kameleoon
54
61
  start_index = KAMELEOON_KEY_JS_COOKIE.length
55
62
  value = value[start_index..-1]
56
63
  end
57
- if value.length < KAMELEOON_COOKIE_VALUE_LENGTH
58
- return nil
64
+ if value.length == 0
65
+ nil
59
66
  end
60
- value[0..(KAMELEOON_COOKIE_VALUE_LENGTH - 1)]
67
+ value[0..(KAMELEOON_VISITOR_CODE_LENGTH - 1)]
61
68
  end
62
69
  end
63
70
  end
@@ -45,5 +45,10 @@ module Kameleoon
45
45
  super("Visitor " + visitor_code + " is not activated.")
46
46
  end
47
47
  end
48
+ class VisitorCodeNotValid < KameleoonError
49
+ def initialize(message = "")
50
+ super("Visitor code not valid: " + message)
51
+ end
52
+ end
48
53
  end
49
54
  end
@@ -42,9 +42,9 @@ module Kameleoon
42
42
 
43
43
  def add_user_agent(request_options)
44
44
  if request_options[:head].nil?
45
- request_options[:head] = {'User-Agent' => 'kameleoon-client-ruby/' + Kameleoon::VERSION}
45
+ request_options[:head] = {'Kameleoon-Client' => 'sdk/ruby/' + Kameleoon::VERSION}
46
46
  else
47
- request_options[:head].store('User-Agent', 'kameleoon-client-ruby/' + Kameleoon::VERSION)
47
+ request_options[:head].store('Kameleoon-Client', 'sdk/ruby/' + Kameleoon::VERSION)
48
48
  end
49
49
  end
50
50
  end
@@ -1,3 +1,3 @@
1
1
  module Kameleoon
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.7'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kameleoon-client-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kameleoon - Guillaume Grandjean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-19 00:00:00.000000000 Z
11
+ date: 2021-07-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: em-http-request