Class: Api::ReviewsIo::DynamicLinkBuilder

Inherits:
Object
  • Object
show all
Defined in:
app/services/api/reviews_io/dynamic_link_builder.rb

Overview

Builds Reviews.io dynamic links for company review invitations.

Dynamic links let you send review invitations via your own email platform.
The link pre-fills customer data so Reviews.io recognises the reviewer as verified.

Identification strategy:
Prefer customer email when a single unambiguous email is available.
Fall back to CN (customer_identifier) when email is missing or ambiguous.
Exception: the customer-level liquid tag always uses CN since the link
is embedded in email templates where we don't know the recipient context.

Usage:
Api::ReviewsIo::DynamicLinkBuilder.for_customer(customer)
Api::ReviewsIo::DynamicLinkBuilder.for_support_case(support_case)
Api::ReviewsIo::DynamicLinkBuilder.for_order(order)
Api::ReviewsIo::DynamicLinkBuilder.for_order_confirmation(order, email:)

Constant Summary collapse

BASE_URL =
'https://www.reviews.io/store/landing_new_review'
STORE_KEY =
'warmlyyours-com'
DEFAULT_REVIEW_TYPE =
'company'
DEFAULT_RATING =
5

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user:, order_id:, email: nil, customer_identifier: nil, review_type: DEFAULT_REVIEW_TYPE, rating: DEFAULT_RATING) ⇒ DynamicLinkBuilder

Returns a new instance of DynamicLinkBuilder.



103
104
105
106
107
108
109
110
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 103

def initialize(user:, order_id:, email: nil, customer_identifier: nil, review_type: DEFAULT_REVIEW_TYPE, rating: DEFAULT_RATING)
  @user = user
  @order_id = order_id
  @email = email
  @customer_identifier = customer_identifier
  @review_type = review_type
  @rating = rating
end

Class Method Details

.for_customer(customer) ⇒ Object

Build a link for customer-level email templates (liquid tags).
Always uses CN — the link is inserted into templates where the
recipient email is handled by the email platform, not by us.



31
32
33
34
35
36
37
38
39
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 31

def self.for_customer(customer)
  return nil unless customer

  new(
    user: customer.full_name,
    order_id: customer.reference_number,
    customer_identifier: customer.reference_number
  ).build
end

.for_order(order) ⇒ Object

Build a link for an order (CRM context).
Uses order reference_number as order_id. Prefers customer email; falls back to CN.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 59

def self.for_order(order)
  return nil unless order

  customer = order.customer
  email, identifier = resolve_identification(customer)

  new(
    user: customer&.full_name,
    order_id: order.reference_number,
    email: email,
    customer_identifier: identifier
  ).build
end

.for_order_confirmation(order, email: nil) ⇒ Object

Build a link for the order thank-you / confirmation page.
Always uses the customer email (the one used for order confirmation notifications).



75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 75

def self.for_order_confirmation(order, email: nil)
  return nil unless order

  customer = order.customer
  confirmation_email = email || order.tracking_email&.first || customer&.email

  new(
    user: customer&.full_name,
    order_id: order.reference_number,
    email: confirmation_email
  ).build
end

.for_support_case(support_case) ⇒ Object

Build a link for a support case.
Uses case_number as order_id. Prefers customer email; falls back to CN.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 43

def self.for_support_case(support_case)
  return nil unless support_case

  customer = support_case.customer
  email, identifier = resolve_identification(customer)

  new(
    user: customer&.full_name,
    order_id: support_case.case_number,
    email: email,
    customer_identifier: identifier
  ).build
end

Instance Method Details

#buildObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/services/api/reviews_io/dynamic_link_builder.rb', line 112

def build
  params = {
    store: STORE_KEY,
    user: encode(@user.presence || 'Customer'),
    order_id: encode(@order_id),
    email: encode(@email),
    products: '',
    type: @review_type,
    customer_identifier: encode(@customer_identifier),
    rating: @rating
  }

  query = params.map { |k, v| "#{k}=#{v}" }.join('&')
  "#{BASE_URL}?#{query}"
end