kameleoon-client-ruby 2.1.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/kameleoon/client.rb +105 -151
  3. data/lib/kameleoon/configuration/rule.rb +1 -1
  4. data/lib/kameleoon/data/browser.rb +39 -0
  5. data/lib/kameleoon/data/conversion.rb +33 -0
  6. data/lib/kameleoon/data/custom_data.rb +60 -0
  7. data/lib/kameleoon/data/data.rb +38 -0
  8. data/lib/kameleoon/data/device.rb +31 -0
  9. data/lib/kameleoon/data/page_view.rb +34 -0
  10. data/lib/kameleoon/data/user_agent.rb +14 -0
  11. data/lib/kameleoon/network/activity_event.rb +31 -0
  12. data/lib/kameleoon/network/content_type.rb +11 -0
  13. data/lib/kameleoon/network/experiment_event.rb +35 -0
  14. data/lib/kameleoon/network/method.rb +10 -0
  15. data/lib/kameleoon/network/net_provider.rb +91 -0
  16. data/lib/kameleoon/network/network_manager.rb +114 -0
  17. data/lib/kameleoon/network/request.rb +20 -0
  18. data/lib/kameleoon/network/response.rb +30 -0
  19. data/lib/kameleoon/network/uri_helper.rb +37 -0
  20. data/lib/kameleoon/network/url_provider.rb +71 -0
  21. data/lib/kameleoon/targeting/condition.rb +40 -11
  22. data/lib/kameleoon/targeting/condition_factory.rb +35 -12
  23. data/lib/kameleoon/targeting/conditions/browser_condition.rb +71 -0
  24. data/lib/kameleoon/targeting/conditions/conversion_condition.rb +21 -0
  25. data/lib/kameleoon/targeting/conditions/custom_datum.rb +60 -65
  26. data/lib/kameleoon/targeting/conditions/device_condition.rb +21 -0
  27. data/lib/kameleoon/targeting/conditions/exclusive_experiment.rb +0 -12
  28. data/lib/kameleoon/targeting/conditions/page_title_condition.rb +21 -0
  29. data/lib/kameleoon/targeting/conditions/page_url_condition.rb +21 -0
  30. data/lib/kameleoon/targeting/conditions/sdk_language_condition.rb +65 -0
  31. data/lib/kameleoon/targeting/conditions/string_value_condition.rb +40 -0
  32. data/lib/kameleoon/targeting/conditions/target_experiment.rb +4 -8
  33. data/lib/kameleoon/targeting/conditions/unknown_condition.rb +15 -0
  34. data/lib/kameleoon/targeting/conditions/visitor_code_condition.rb +16 -0
  35. data/lib/kameleoon/targeting/models.rb +0 -24
  36. data/lib/kameleoon/utils.rb +1 -1
  37. data/lib/kameleoon/version.rb +28 -1
  38. metadata +28 -4
  39. data/lib/kameleoon/data.rb +0 -175
  40. data/lib/kameleoon/request.rb +0 -90
@@ -1,175 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'kameleoon/exceptions'
4
-
5
- module Kameleoon
6
- NONCE_LENGTH = 16
7
-
8
- module DataType
9
- CUSTOM = 'CUSTOM'
10
- BROWSER = 'BROWSER'
11
- CONVERSION = 'CONVERSION'
12
- DEVICE = 'DEVICE'
13
- PAGE_VIEW = 'PAGE_VIEW'
14
- end
15
-
16
- module BrowserType
17
- CHROME = 0
18
- INTERNET_EXPLORER = 1
19
- FIREFOX = 2
20
- SAFARI = 3
21
- OPERA = 4
22
- OTHER = 5
23
- end
24
-
25
- module DeviceType
26
- PHONE = 'PHONE'
27
- TABLET = 'TABLET'
28
- DESKTOP = 'DESKTOP'
29
- end
30
-
31
- class Data
32
- attr_accessor :instance, :sent
33
-
34
- def initialize(*args)
35
- raise KameleoonError.new("Cannot initialise interface.")
36
- end
37
-
38
- def obtain_full_post_text_line
39
- raise KameleoonError.new("ToDo: implement this method.")
40
- end
41
-
42
- def encode(url)
43
- encoded_url = CGI.escape(url)
44
- encoded_url.gsub! "%27", "'"
45
- encoded_url.gsub! "%21", "!"
46
- encoded_url.gsub! "+", "%20"
47
- encoded_url.gsub! "%2A", "*"
48
- encoded_url.gsub! "%28", "("
49
- encoded_url.gsub! "%29", ")"
50
- encoded_url
51
- end
52
- end
53
-
54
- class CustomData < Data
55
- attr_reader :id, :values
56
-
57
- # @param [Integer] id Id of the custom data
58
- # @param [String] value Value of the custom data
59
- #
60
- # @overload
61
- # @param [Hash] hash Json value encoded in a hash.
62
- def initialize(arg0, *args)
63
- @instance = DataType::CUSTOM
64
- @sent = false
65
- if arg0.is_a?(Hash)
66
- hash = arg0
67
- id = hash['id']
68
- raise Kameleoon::Exception::NotFound.new('id') if id.nil?
69
- @id = id.to_s
70
- value = hash['value']
71
- values = hash['values']
72
- raise Kameleoon::Exception::NotFound.new('values') if values.nil? && value.nil?
73
- if values.nil?
74
- @values = [value]
75
- else
76
- @values = values.dup
77
- @values.append(value) unless value.nil?
78
- end
79
- else
80
- @id = arg0.to_s
81
- @values = args
82
- end
83
- end
84
-
85
- def obtain_full_post_text_line
86
- return '' if @values.empty?
87
-
88
- str_values = "[[\"#{@values.join('",1],["')}\",1]]"
89
- nonce = Kameleoon::Utils.generate_random_string(NONCE_LENGTH)
90
- "eventType=customData&index=#{@id}&valueToCount=#{encode(str_values)}&overwrite=true&nonce=#{nonce}"
91
- end
92
- end
93
-
94
- class Browser < Data
95
- attr_accessor :browser
96
-
97
- # @param [BrowserType] browser_type Browser type, can be: CHROME, INTERNET_EXPLORER, FIREFOX, SAFARI, OPERA, OTHER
98
- def initialize(browser_type)
99
- @instance = DataType::BROWSER
100
- @sent = false
101
- @browser = browser_type
102
- end
103
-
104
- def obtain_full_post_text_line
105
- nonce = Kameleoon::Utils.generate_random_string(NONCE_LENGTH)
106
- "eventType=staticData&browserIndex=" + @browser.to_s + "&nonce=" + nonce
107
- end
108
- end
109
-
110
- class PageView < Data
111
- attr_accessor :url, :title, :referrer
112
-
113
- # @param [String] url Url of the page
114
- # @param [String] title Title of the page
115
- # @param [Array] referrers Optional field - Referrer ids
116
- def initialize(url, title, referrers = nil)
117
- @instance = DataType::PAGE_VIEW
118
- @sent = false
119
- @url = url
120
- @title = title
121
- @referrers = referrers
122
- end
123
-
124
- def obtain_full_post_text_line
125
- nonce = Kameleoon::Utils.generate_random_string(NONCE_LENGTH)
126
- referrer_text = ''
127
- unless @referrers.nil?
128
- referrer_text = "&referrersIndices=" + @referrers.to_s
129
- end
130
- "eventType=page&href=" + encode(@url) + "&title=" + @title + referrer_text + "&nonce=" + nonce
131
- end
132
- end
133
-
134
- class Conversion < Data
135
- attr_accessor :goal_id, :revenue, :negative
136
-
137
- # @param [Integer] goal_id Id of the goal associated to the conversion
138
- # @param [Float] revenue Optional field - Revenue associated to the conversion.
139
- # @param [Boolean] negative Optional field - If the revenue is negative. By default it's positive.
140
- def initialize(goal_id, revenue = 0.0, negative = false)
141
- @instance = DataType::CONVERSION
142
- @sent = false
143
- @goal_id = goal_id
144
- @revenue = revenue
145
- @negative = negative
146
- end
147
-
148
- def obtain_full_post_text_line
149
- nonce = Kameleoon::Utils.generate_random_string(NONCE_LENGTH)
150
- "eventType=conversion&goalId=" + @goal_id.to_s + "&revenue=" + @revenue.to_s + "&negative=" + @negative.to_s + "&nonce=" + nonce
151
- end
152
- end
153
-
154
- # Device uses for sending deviceType parameter for tracking calls
155
- class Device < Data
156
- def initialize(device_type)
157
- @instance = DataType::DEVICE
158
- @sent = false
159
- @device_type = device_type
160
- end
161
-
162
- def obtain_full_post_text_line
163
- nonce = Kameleoon::Utils.generate_random_string(NONCE_LENGTH)
164
- "eventType=staticData&deviceType=#{@device_type}&nonce=#{nonce}"
165
- end
166
- end
167
-
168
- # UserAgent uses for changing User-Agent header for tracking calls
169
- class UserAgent
170
- attr_accessor :value
171
- def initialize(value)
172
- @value = value
173
- end
174
- end
175
- end
@@ -1,90 +0,0 @@
1
- require 'em-synchrony/em-http'
2
- require 'kameleoon/version'
3
- require 'net/http'
4
-
5
- module Kameleoon
6
- # @api private
7
- module Request
8
- protected
9
-
10
- API_URL = 'https://api.kameleoon.com'.freeze
11
- CLIENT_CONFIG_URL = 'https://client-config.kameleoon.com'.freeze
12
-
13
- module Method
14
- GET = 'get'.freeze
15
- POST = 'post'.freeze
16
- end
17
-
18
- def get(request_options, url = API_URL, connexion_options = {})
19
- request(Method::GET, request_options, url, connexion_options)
20
- end
21
-
22
- def post(request_options, url = API_URL, connexion_options = {})
23
- request(Method::POST, request_options, url, connexion_options)
24
- end
25
-
26
- def get_sync(url = API_URL, connexion_options = {})
27
- request_sync(Method::GET, url, connexion_options, {})
28
- end
29
-
30
- def post_sync(request_options, url = API_URL, connexion_options = {})
31
- request_sync(Method::POST, url, connexion_options, request_options)
32
- end
33
-
34
- private
35
-
36
- def request(method, request_options, url, connexion_options)
37
- connexion_options[:tls] = { verify_peer: false }
38
- add_user_agent(request_options)
39
- case method
40
- when Method::POST then
41
- return EventMachine::HttpRequest.new(url, connexion_options).apost request_options
42
- when Method::GET then
43
- return EventMachine::HttpRequest.new(url, connexion_options).get request_options
44
- else
45
- print 'Unknown request type'
46
- return false
47
- end
48
- end
49
-
50
- def request_sync(method, url, connexion_options, request_options)
51
- request_options = {} if request_options.nil?
52
- add_user_agent(request_options)
53
- case method
54
- when Method::GET then
55
- uri = URI(url)
56
- request = Net::HTTP.new(url)
57
- response = Net::HTTP.get_response(uri)
58
- response
59
- when Method::POST then
60
- uri = URI.parse(url)
61
- http = Net::HTTP.new(uri.host, uri.port)
62
- http.use_ssl = true
63
- request = Net::HTTP::Post.new(request_options[:path], initheader = request_options[:head])
64
- request.body = request_options[:body]
65
- response = http.request(request)
66
- response
67
- else
68
- print "Unknown request type"
69
- return false
70
- end
71
- end
72
-
73
- def successful?(request)
74
- !request.nil? && request != false && /20\d/.match(request.response_header.status.to_s)
75
- end
76
-
77
- def successful_sync?(response)
78
- !response.nil? && response != false && response.is_a?(Net::HTTPSuccess)
79
- end
80
-
81
- def add_user_agent(request_options)
82
- sdk_version = "sdk/ruby/#{Kameleoon::VERSION}"
83
- if request_options[:head].nil?
84
- request_options[:head] = { 'Kameleoon-Client' => sdk_version }
85
- else
86
- request_options[:head].store('Kameleoon-Client', sdk_version)
87
- end
88
- end
89
- end
90
- end