class Promoted::Ruby::Client::RequestBuilder
Attributes
client_info[R]
device[R]
experiment[R]
full_insertion[R]
insertion[R]
only_log[R]
platform_id[RW]
request[R]
request_id[R]
session_id[R]
timing[RW]
to_compact_delivery_properties_func[R]
to_compact_metrics_properties_func[R]
use_case[R]
user_info[RW]
view_id[R]
Public Class Methods
new(args = {})
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 11 def initialize args = {} if args[:id_generator] @id_generator = args[:id_generator] else @id_generator = IdGenerator.new end end
Public Instance Methods
add_missing_insertion_ids!(insertions)
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 189 def add_missing_insertion_ids! insertions insertions.each do |insertion| insertion[:insertion_id] = @id_generator.newID if not insertion[:insertion_id] end end
client_request_id()
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 195 def client_request_id request[:client_request_id] end
compact_metrics_properties()
click to toggle source
TODO: This looks overly complicated.
# File lib/promoted/ruby/client/request_builder.rb, line 164 def compact_metrics_properties @insertion = [] # insertion should be set according to the compact insertion paging = request[:paging] || {} size = paging[:size] ? paging[:size].to_i : 0 if size <= 0 size = full_insertion.length() end offset = paging[:offset] ? paging[:offset].to_i : 0 full_insertion.each_with_index do |insertion_obj, index| # TODO - this does not look performant. break if @insertion.length() >= size insertion_obj = insertion_obj.transform_keys{ |key| key.to_s.to_underscore.to_sym } insertion_obj = Hash[insertion_obj] insertion_obj[:user_info] = user_info insertion_obj[:timing] = timing insertion_obj[:request_id] = request_id insertion_obj[:position] = offset + index insertion_obj = compact_one_insertion(insertion_obj, @to_compact_metrics_properties_func) @insertion << insertion_obj.clean! end @insertion end
compact_one_insertion(insertion, compact_func)
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 129 def compact_one_insertion(insertion, compact_func) return insertion if (!compact_func || !insertion[:properties]) # Only need a copy if there are properties to compact. compact_insertion = insertion.dup # Let the custom function work with a deep copy of the properties. # There's really no way to work with a shallow copy and still be able # to restore the correct insertion properties after a call to delivery. new_props = Marshal.load(Marshal.dump(insertion[:properties])) compact_insertion[:properties] = compact_func.call(new_props) compact_insertion.clean! return compact_insertion end
delivery_request_params()
click to toggle source
Only used in delivery
# File lib/promoted/ruby/client/request_builder.rb, line 56 def delivery_request_params params = { user_info: user_info, timing: timing, client_info: merge_client_info_defaults, device: @device, platform_id: @platform_id, view_id: @view_id, session_id: @session_id, use_case: @use_case, search_query: request[:search_query], properties: request[:properties], paging: request[:paging], client_request_id: client_request_id } params[:insertion] = insertions_with_compact_props(@to_compact_delivery_properties_func) params.clean! end
ensure_client_timestamp()
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 157 def ensure_client_timestamp if timing[:client_log_timestamp].nil? timing[:client_log_timestamp] = Time.now.to_i end end
fill_details_from_response(response_insertions)
click to toggle source
Only used in delivery Maps the response insertions to the full insertions and re-insert the properties bag to the responses.
# File lib/promoted/ruby/client/request_builder.rb, line 79 def fill_details_from_response response_insertions if !response_insertions then response_insertions = [] end props = @full_insertion.each_with_object({}) do |insertion, hash| if insertion.has_key?(:properties) # Don't add nil properties to response insertions. hash[insertion[:content_id]] = insertion[:properties] end end filled_in_copy = [] response_insertions.each do |resp_insertion| copied_insertion = resp_insertion.clone if copied_insertion.has_key?(:content_id) && props.has_key?(copied_insertion[:content_id]) copied_insertion[:properties] = props[resp_insertion[:content_id]] end filled_in_copy << copied_insertion end filled_in_copy end
insertions_with_compact_props(compact_func)
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 144 def insertions_with_compact_props(compact_func) if !compact_func # Nothing to do, avoid copying the whole array. full_insertion else compact_insertions = Array.new(full_insertion.length) full_insertion.each_with_index {|insertion, index| compact_insertions[index] = compact_one_insertion(insertion, compact_func) } compact_insertions end end
log_request_params(include_insertions: true, include_request: true)
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 103 def log_request_params(include_insertions: true, include_request: true) params = { user_info: user_info, timing: timing, client_info: merge_client_info_defaults, device: @device } if @experiment params[:cohort_membership] = [@experiment] end # Log request allows for multiple requests but here we only send one. if include_request request[:request_id] = request[:request_id] || @id_generator.newID params[:request] = [request] end if include_insertions params[:insertion] = compact_metrics_properties if include_insertions add_missing_ids_on_insertions! request, params[:insertion] end params.clean! end
new_cohort_membership_to_log()
click to toggle source
Only used in delivery
# File lib/promoted/ruby/client/request_builder.rb, line 41 def new_cohort_membership_to_log return nil unless @experiment if !@experiment[:platform_id] && @platform_id @experiment[:platform_id] = @platform_id end if !@experiment[:user_info] && @user_info @experiment[:user_info] = @user_info end if !@experiment[:timing] && @timing @experiment[:timing] = @timing end return @experiment end
set_request_params(args = {})
click to toggle source
Populates request parameters from the given arguments, presumed to be a hash of symbols.
# File lib/promoted/ruby/client/request_builder.rb, line 20 def set_request_params args = {} @request = args[:request] || {} @experiment = args[:experiment] @only_log = args[:only_log] @session_id = request[:session_id] @platform_id = request[:platform_id] @client_info = request[:client_info] || {} @device = request[:device] || {} @view_id = request[:view_id] @use_case = Promoted::Ruby::Client::USE_CASES[request[:use_case]] || Promoted::Ruby::Client::USE_CASES['UNKNOWN_USE_CASE'] @full_insertion = args[:full_insertion] @user_info = request[:user_info] || { :user_id => nil, :log_user_id => nil} @timing = request[:timing] || { :client_log_timestamp => Time.now.to_i } @to_compact_metrics_properties_func = args[:to_compact_metrics_properties_func] @to_compact_delivery_properties_func = args[:to_compact_delivery_properties_func] # If the user didn't create a client request id, we do it for them. request[:client_request_id] = request[:client_request_id] || @id_generator.newID end
Private Instance Methods
add_missing_ids_on_insertions!(request, insertions)
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 208 def add_missing_ids_on_insertions! request, insertions insertions.each do |insertion| insertion[:session_id] = request[:session_id] if request[:session_id] insertion[:request_id] = request[:request_id] if request[:request_id] end add_missing_insertion_ids! insertions end
merge_client_info_defaults()
click to toggle source
# File lib/promoted/ruby/client/request_builder.rb, line 201 def merge_client_info_defaults return @client_info.merge({ :client_type => Promoted::Ruby::Client::CLIENT_TYPE['PLATFORM_SERVER'], :traffic_type => Promoted::Ruby::Client::TRAFFIC_TYPE['PRODUCTION'] }) end