Module: RideBuilder::Affiliate::Capture

Defined in:
lib/ridebuilder/affiliate/capture.rb

Overview

Framework-free capture helpers — turn an incoming request into a validated click_id (or nil). Every helper enforces ref == "ridebuilder" and the UUID-v4 click_id, the same rules the browser snippet uses.

Class Method Summary collapse

Class Method Details

Recover the click_id from the ridebuilder_attribution cookie the browser snippet set.



30
31
32
33
34
35
36
37
# File 'lib/ridebuilder/affiliate/capture.rb', line 30

def from_cookie_header(cookie_header)
  return nil if cookie_header.nil? || cookie_header.empty?

  raw = parse_cookie_header(cookie_header)[ClickId::COOKIE_NAME]
  return nil if raw.nil? || raw.empty?

  parse_attribution_cookie(raw)&.click_id
end

.from_headers(headers, name = "X-RideBuilder-Click-Id") ⇒ Object

Read a validated click_id off a forwarding header (case-insensitive) — the decoupled path.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ridebuilder/affiliate/capture.rb', line 40

def from_headers(headers, name = "X-RideBuilder-Click-Id")
  return nil unless headers

  target = name.downcase
  headers.each do |key, value|
    next unless key.to_s.downcase == target

    val = value.is_a?(Array) ? value.first : value
    return ClickId.valid?(val) ? val : nil
  end
  nil
end

.from_query(query) ⇒ Object

Read a validated click_id from a decoded query map.



22
23
24
25
26
27
# File 'lib/ridebuilder/affiliate/capture.rb', line 22

def from_query(query)
  return nil unless query && query["ref"] == ClickId::REF

  click_id = query["click_id"]
  ClickId.valid?(click_id) ? click_id : nil
end

.from_url(url) ⇒ Object

Read a validated click_id from a raw URL (absolute or relative like "/landing?...").



11
12
13
14
15
16
17
18
19
# File 'lib/ridebuilder/affiliate/capture.rb', line 11

def from_url(url)
  return nil if url.nil? || url.empty?

  query = url.split("?", 2)[1]
  return nil if query.nil?

  query = query.split("#", 2)[0]
  from_query(parse_query(query))
end

Parse the raw ridebuilder_attribution cookie value (URL-encoded JSON the snippet wrote).



54
55
56
57
58
59
60
61
# File 'lib/ridebuilder/affiliate/capture.rb', line 54

def parse_attribution_cookie(raw_value)
  parsed = JSON.parse(CGI.unescape(raw_value))
  return nil unless parsed.is_a?(Hash) && ClickId.valid?(parsed["click_id"]) && parsed["ref"] == ClickId::REF

  Attribution.new(parsed["click_id"], parsed["ref"], parsed["clicked_at"])
rescue JSON::ParserError
  nil
end

Split a Cookie header ("a=1; b=2") into a name=>value hash (first '=' separates).



64
65
66
67
68
69
70
71
# File 'lib/ridebuilder/affiliate/capture.rb', line 64

def parse_cookie_header(cookie_header)
  out = {}
  cookie_header.split(";").each do |part|
    i = part.index("=")
    out[part[0...i].strip] = part[(i + 1)..].strip if i && i.positive?
  end
  out
end

.parse_query(query) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ridebuilder/affiliate/capture.rb', line 73

def parse_query(query)
  out = {}
  return out if query.nil? || query.empty?

  query.split("&").each do |pair|
    next if pair.empty?

    key, value = pair.split("=", 2)
    key = CGI.unescape(key)
    out[key] = value.nil? ? "" : CGI.unescape(value) unless out.key?(key)
  end
  out
end