promoted-ruby-client 0.1.7 → 0.1.8

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38925e54b884c3e36db632faa2481cd99621846848c21c80def989e1539a87d7
4
- data.tar.gz: 6c0b3caedbef47b9ed6a5c5c1c9e58368031521c6f36181ae1a4938bd9f796fd
3
+ metadata.gz: e9911fa22cb387f4258e9261729f5b17baa6412ae2d9ddd82cb943e35c02aef0
4
+ data.tar.gz: 29a8ae5517ecbdf317671280c31942781b13cf7ba8d6f3f09b9658e3101ee24a
5
5
  SHA512:
6
- metadata.gz: eae233ce5d572a6e581a57572292edf212205ab15764ff41db98116506c548b0746a94be236d44dda1bb43d758005a25bef8bbcfb8816295f4de9f30efb75481
7
- data.tar.gz: 5085fa33d2e4d040d134a69cc8af872b4c209bda2ddee1be1f51f3d7c3b948ac8300152a790fbcdadaa5dbb75902b94727d1b20575398d563b5aa6757cfc3ea4
6
+ metadata.gz: 82d1b9721132cc813ddc614292111a0003423c881d836a8dd471115a20b2a2c0b2f0cbdee4d7eb2c12cae993000623012dd21bf8844bd05a9803796f60a99d56
7
+ data.tar.gz: 91e3466ee73398157ab6b1e41778a89f760aa9741b7a2fcb8c0edb4ad382933ab13a4526145290d70990b4cfa8599c28ca7d0c18d968de50b2aaa81af4768f6c
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- promoted-ruby-client (0.1.7)
4
+ promoted-ruby-client (0.1.8)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -88,7 +88,7 @@ Field Name | Type | Optional? | Description
88
88
  ---------- | ---- | --------- | -----------
89
89
  ```:user_info``` | UserInfo | Yes | The user info structure.
90
90
  ```:insertion_id``` | String | Yes | Generated by the SDK (*do not set*)
91
- ```:request_id``` | String | Yes | Generated by the SDK (*do not set*)
91
+ ```:request_id``` | String | Yes | Generated by the SDK when needed (*do not set*)
92
92
  ```:content_id``` | String | No | Identifier for the content to be shown, must be set.
93
93
  ```:properties``` | Properties | Yes | Any additional custom properties to associate. For v1 integrations, it is fine not to fill in all the properties.
94
94
 
@@ -101,7 +101,7 @@ A request for content insertions.
101
101
  Field Name | Type | Optional? | Description
102
102
  ---------- | ---- | --------- | -----------
103
103
  ```:user_info``` | UserInfo | Yes | The user info structure.
104
- ```:request_id``` | String | Yes | Generated by the SDK (*do not set*)
104
+ ```:request_id``` | String | Yes | Generated by the SDK when needed (*do not set*)
105
105
  ```:use_case``` | String | Yes | One of the use case values, i.e. 'FEED' (see [constants.rb](https://github.com/promotedai/promoted-ruby-client/blob/main/lib/promoted/ruby/client/constants.rb)).
106
106
  ```:properties``` | Properties | Yes | Any additional custom properties to associate.
107
107
  ```:paging``` | Paging | Yes | Paging parameters (see TODO)
data/dev.md CHANGED
@@ -4,5 +4,5 @@
4
4
  2. Get credentials for deployment from 1password.
5
5
  3. Modify `promoted-ruby-client.gemspec`'s push block.
6
6
  4. Run `gem build promoted-ruby-client.gemspec` to generate `gem`.
7
- 5. Run (using new output) `gem push promoted-ruby-client-0.1.7.gem`
7
+ 5. Run (using new output) `gem push promoted-ruby-client-0.1.8.gem`
8
8
  6. Update README with new version.
@@ -21,8 +21,14 @@ module Promoted
21
21
  attr_reader :perform_checks, :default_only_log, :delivery_timeout_millis, :metrics_timeout_millis, :should_apply_treatment_func,
22
22
  :default_request_headers, :http_client, :logger, :shadow_traffic_delivery_percent, :async_shadow_traffic
23
23
 
24
- attr_accessor :request_logging_on
24
+ attr_accessor :request_logging_on, :enabled
25
25
 
26
+ ##
27
+ # Whether or not the client is currently enabled for execution.
28
+ def enabled?
29
+ @enabled
30
+ end
31
+
26
32
  ##
27
33
  # A common compact method implementation.
28
34
  def self.copy_and_remove_properties
@@ -85,6 +91,11 @@ module Promoted
85
91
  fallback_policy: :discard
86
92
  )
87
93
  end
94
+
95
+ @enabled = true
96
+ if params[:enabled] != nil
97
+ @enabled = params[:enabled] || false
98
+ end
88
99
  end
89
100
 
90
101
  ##
@@ -101,6 +112,14 @@ module Promoted
101
112
  def deliver args, headers={}
102
113
  args = Promoted::Ruby::Client::Util.translate_args(args)
103
114
 
115
+ # Respect the enabled state
116
+ if !@enabled
117
+ return {
118
+ insertion: apply_paging(args[:full_insertion], args[:request][:paging])
119
+ # No log request returned when disabled
120
+ }
121
+ end
122
+
104
123
  delivery_request_builder = RequestBuilder.new
105
124
  delivery_request_builder.set_request_params(args)
106
125
 
@@ -110,7 +129,7 @@ module Promoted
110
129
 
111
130
  response_insertions = []
112
131
  cohort_membership_to_log = nil
113
- insertions_from_promoted = false
132
+ insertions_from_delivery = false
114
133
 
115
134
  only_log = delivery_request_builder.only_log != nil ? delivery_request_builder.only_log : @default_only_log
116
135
  deliver_err = false
@@ -130,22 +149,16 @@ module Promoted
130
149
  @logger.error("Error calling delivery: " + err.message) if @logger
131
150
  end
132
151
 
133
- insertions_from_promoted = (response != nil && !deliver_err);
152
+ insertions_from_delivery = (response != nil && !deliver_err);
134
153
  response_insertions = delivery_request_builder.fill_details_from_response(
135
154
  response ? response[:insertion] : [])
136
155
  end
137
156
  end
138
157
 
139
158
  request_to_log = nil
140
- if !insertions_from_promoted then
159
+ if !insertions_from_delivery then
141
160
  request_to_log = delivery_request_builder.request
142
- size = delivery_request_builder.request.dig(:paging, :size)
143
- response_insertions = size != nil ? delivery_request_builder.full_insertion[0..size] : delivery_request_builder.full_insertion
144
- end
145
-
146
- if request_to_log
147
- request_to_log[:request_id] = SecureRandom.uuid if not request_to_log[:request_id]
148
- add_missing_ids_on_insertions! request_to_log, response_insertions
161
+ response_insertions = apply_paging(delivery_request_builder.full_insertion, delivery_request_builder.request[:paging])
149
162
  end
150
163
 
151
164
  log_req = nil
@@ -169,8 +182,8 @@ module Promoted
169
182
  # On a successful delivery request, we don't log the insertions
170
183
  # or the request since they are logged on the server-side.
171
184
  log_req = log_request_builder.log_request_params(
172
- include_insertions: !insertions_from_promoted,
173
- include_request: !insertions_from_promoted)
185
+ include_insertions: !insertions_from_delivery,
186
+ include_request: !insertions_from_delivery)
174
187
  end
175
188
 
176
189
  client_response = {
@@ -186,6 +199,12 @@ module Promoted
186
199
  def prepare_for_logging args, headers={}
187
200
  args = Promoted::Ruby::Client::Util.translate_args(args)
188
201
 
202
+ if !@enabled
203
+ return {
204
+ insertion: args[:full_insertion]
205
+ }
206
+ end
207
+
189
208
  log_request_builder = RequestBuilder.new
190
209
 
191
210
  # Note: This method expects as JSON (string keys) but internally, RequestBuilder
@@ -223,6 +242,14 @@ module Promoted
223
242
 
224
243
  private
225
244
 
245
+ def apply_paging full_insertion, paging
246
+ size = nil
247
+ if paging
248
+ size = paging[:size]
249
+ end
250
+ return size != nil ? full_insertion[0..size - 1] : full_insertion
251
+ end
252
+
226
253
  def send_request payload, endpoint, timeout_millis, api_key, headers={}, send_async=false
227
254
  resp = nil
228
255
 
@@ -258,15 +285,6 @@ module Promoted
258
285
  return resp
259
286
  end
260
287
 
261
-
262
- def add_missing_ids_on_insertions! request, insertions
263
- insertions.each do |insertion|
264
- insertion[:insertion_id] = SecureRandom.uuid if not insertion[:insertion_id]
265
- insertion[:session_id] = request[:session_id] if request[:session_id]
266
- insertion[:request_id] = request[:request_id] if request[:request_id]
267
- end
268
- end
269
-
270
288
  def should_send_as_shadow_traffic?
271
289
  @sampler.sample_random?(@shadow_traffic_delivery_percent)
272
290
  end
@@ -0,0 +1,15 @@
1
+ module Promoted
2
+ module Ruby
3
+ module Client
4
+ class IdGenerator
5
+ def initialize;end
6
+
7
+ def newID
8
+ SecureRandom.uuid
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'securerandom'
@@ -8,7 +8,13 @@ module Promoted
8
8
 
9
9
  attr_accessor :timing, :user_info, :platform_id
10
10
 
11
- def initialize;end
11
+ def initialize args = {}
12
+ if args[:id_generator]
13
+ @id_generator = args[:id_generator]
14
+ else
15
+ @id_generator = IdGenerator.new
16
+ end
17
+ end
12
18
 
13
19
  # Populates request parameters from the given arguments, presumed to be a hash of symbols.
14
20
  def set_request_params args = {}
@@ -21,7 +27,6 @@ module Promoted
21
27
  @view_id = request[:view_id]
22
28
  @use_case = Promoted::Ruby::Client::USE_CASES[request[:use_case]] || Promoted::Ruby::Client::USE_CASES['UNKNOWN_USE_CASE']
23
29
  @full_insertion = args[:full_insertion]
24
- @request_id = SecureRandom.uuid
25
30
  @user_info = request[:user_info] || { :user_id => nil, :log_user_id => nil}
26
31
  @timing = request[:timing] || { :client_log_timestamp => Time.now.to_i }
27
32
  @to_compact_metrics_insertion_func = args[:to_compact_metrics_insertion_func]
@@ -50,7 +55,6 @@ module Promoted
50
55
  timing: timing,
51
56
  client_info: @client_info.merge({ :client_type => Promoted::Ruby::Client::CLIENT_TYPE['PLATFORM_SERVER'] }),
52
57
  platform_id: @platform_id,
53
- request_id: @request_id,
54
58
  view_id: @view_id,
55
59
  session_id: @session_id,
56
60
  use_case: @use_case,
@@ -94,8 +98,16 @@ module Promoted
94
98
  cohort_membership: @experiment,
95
99
  client_info: @client_info
96
100
  }
101
+
102
+ request[:request_id] = @id_generator.newID if not request[:request_id]
103
+
104
+ # Log request allows for multiple requests but here we only send one.
97
105
  params[:request] = [request] if include_request
98
- params[:insertion] = compact_metrics_insertions if include_insertions
106
+
107
+ if include_insertions
108
+ params[:insertion] = compact_metrics_insertions if include_insertions
109
+ add_missing_ids_on_insertions! request, params[:insertion]
110
+ end
99
111
 
100
112
  params.clean!
101
113
  end
@@ -126,7 +138,7 @@ module Promoted
126
138
  insertion_obj = Hash[insertion_obj]
127
139
  insertion_obj[:user_info] = user_info
128
140
  insertion_obj[:timing] = timing
129
- insertion_obj[:insertion_id] = SecureRandom.uuid # generate random UUID
141
+ insertion_obj[:insertion_id] = @id_generator.newID
130
142
  insertion_obj[:request_id] = request_id
131
143
  insertion_obj[:position] = offset + index
132
144
  insertion_obj = @to_compact_metrics_insertion_func.call(insertion_obj) if @to_compact_metrics_insertion_func
@@ -137,6 +149,14 @@ module Promoted
137
149
 
138
150
  private
139
151
 
152
+ def add_missing_ids_on_insertions! request, insertions
153
+ insertions.each do |insertion|
154
+ insertion[:insertion_id] = @id_generator.newID if not insertion[:insertion_id]
155
+ insertion[:session_id] = request[:session_id] if request[:session_id]
156
+ insertion[:request_id] = request[:request_id] if request[:request_id]
157
+ end
158
+ end
159
+
140
160
  # A list of the response Insertions. This client expects lists to be truncated
141
161
  # already to request.paging.size. If not truncated, this client will truncate
142
162
  # the list.
@@ -148,6 +168,6 @@ module Promoted
148
168
  end
149
169
  end
150
170
 
151
- require 'securerandom'
152
171
  require "promoted/ruby/client/constants"
153
172
  require "promoted/ruby/client/extensions"
173
+ require "promoted/ruby/client/id_generator"
@@ -1,7 +1,7 @@
1
1
  module Promoted
2
2
  module Ruby
3
3
  module Client
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.8"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promoted-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - scottmcmaster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-14 00:00:00.000000000 Z
11
+ date: 2021-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -75,6 +75,7 @@ files:
75
75
  - lib/promoted/ruby/client/errors.rb
76
76
  - lib/promoted/ruby/client/extensions.rb
77
77
  - lib/promoted/ruby/client/faraday_http_client.rb
78
+ - lib/promoted/ruby/client/id_generator.rb
78
79
  - lib/promoted/ruby/client/request_builder.rb
79
80
  - lib/promoted/ruby/client/sampler.rb
80
81
  - lib/promoted/ruby/client/util.rb