Class: Promoted::Ruby::Client::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/promoted/ruby/client/request_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ RequestBuilder

Returns a new instance of RequestBuilder.



10
11
12
13
14
15
16
# File 'lib/promoted/ruby/client/request_builder.rb', line 10

def initialize args = {}
  if args[:id_generator]
    @id_generator = args[:id_generator]
  else
    @id_generator = IdGenerator.new
  end
end

Instance Attribute Details

#client_infoObject (readonly)

Returns the value of attribute client_info.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def client_info
  @client_info
end

#deviceObject (readonly)

Returns the value of attribute device.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def device
  @device
end

#experimentObject (readonly)

Returns the value of attribute experiment.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def experiment
  @experiment
end

#insertionObject

Returns the value of attribute insertion.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def insertion
  @insertion
end

#only_logObject (readonly)

Returns the value of attribute only_log.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def only_log
  @only_log
end

#platform_idObject

Returns the value of attribute platform_id.



8
9
10
# File 'lib/promoted/ruby/client/request_builder.rb', line 8

def platform_id
  @platform_id
end

#requestObject (readonly)

Returns the value of attribute request.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def request
  @request
end

#request_idObject (readonly)

Returns the value of attribute request_id.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def request_id
  @request_id
end

#session_idObject (readonly)

Returns the value of attribute session_id.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def session_id
  @session_id
end

#timingObject

Returns the value of attribute timing.



8
9
10
# File 'lib/promoted/ruby/client/request_builder.rb', line 8

def timing
  @timing
end

#use_caseObject (readonly)

Returns the value of attribute use_case.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def use_case
  @use_case
end

#user_infoObject

Returns the value of attribute user_info.



8
9
10
# File 'lib/promoted/ruby/client/request_builder.rb', line 8

def 
  @user_info
end

#view_idObject (readonly)

Returns the value of attribute view_id.



5
6
7
# File 'lib/promoted/ruby/client/request_builder.rb', line 5

def view_id
  @view_id
end

Instance Method Details

#add_missing_insertion_ids!(insertions) ⇒ Object



78
79
80
81
82
# File 'lib/promoted/ruby/client/request_builder.rb', line 78

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_idObject



84
85
86
# File 'lib/promoted/ruby/client/request_builder.rb', line 84

def client_request_id
  request[:client_request_id]
end

#delivery_request_paramsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/promoted/ruby/client/request_builder.rb', line 52

def delivery_request_params
  params = {
    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] = insertion

  params.clean!
end

#ensure_client_timestampObject



72
73
74
75
76
# File 'lib/promoted/ruby/client/request_builder.rb', line 72

def ensure_client_timestamp
  if timing[:client_log_timestamp].nil?
    timing[:client_log_timestamp] = (Time.now.to_f * 1000).to_i
  end
end

#new_cohort_membership_to_logObject

Only used in delivery



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/promoted/ruby/client/request_builder.rb', line 38

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 = {}) ⇒ Object

Populates request parameters from the given arguments, presumed to be a hash of symbols.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/promoted/ruby/client/request_builder.rb', line 19

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']
  @insertion               = request[:insertion] || []
  @user_info               = request[:user_info] || { :user_id => nil, :anon_user_id => nil}
  @timing                  = request[:timing] || { :client_log_timestamp => (Time.now.to_f * 1000).to_i }

  # 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