Class: Assistant::EmailPreferenceUpdateRequest

Inherits:
Object
  • Object
show all
Defined in:
app/services/assistant/email_preference_update_request.rb

Overview

Normalizes and validates one preference-update request.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ void

Parameters:

  • attributes (Hash)

    keyword arguments received by the assistant tool



9
10
11
# File 'app/services/assistant/email_preference_update_request.rb', line 9

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesHash (readonly)

Returns keyword arguments received by the assistant tool.

Returns:

  • (Hash)

    keyword arguments received by the assistant tool



14
15
16
# File 'app/services/assistant/email_preference_update_request.rb', line 14

def attributes
  @attributes
end

Instance Method Details

#emailString

Returns normalized email address.

Returns:

  • (String)

    normalized email address



17
18
19
# File 'app/services/assistant/email_preference_update_request.rb', line 17

def email
  @email ||= Heatwave::Normalizers.chain(attributes[:email], :strip, :blank, :downcase)
end

#preflight_errorString?

Validates the request before any database changes.

Returns:

  • (String, nil)

    JSON error payload, or nil if valid



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/services/assistant/email_preference_update_request.rb', line 41

def preflight_error
  return { error: 'email must be a non-empty address.' }.to_json if email.blank?
  return { error: 'unsubscribe_all and resubscribe_all conflict — pass only one.' }.to_json if unsubscribe_all? && resubscribe_all?
  if updates.any? && (unsubscribe_all? || resubscribe_all?)
    return { error: 'Pass individual flags OR unsubscribe_all/resubscribe_all — not both; ' \
                    'the all-flags switch would silently discard the individual ones.' }.to_json
  end
  return { error: 'Nothing to update — pass at least one flag, unsubscribe_all, or resubscribe_all.' }.to_json unless updates.any? || unsubscribe_all? || resubscribe_all?
  return resubscribe_gate if resubscribe_all? && !attributes[:confirm].to_b

  nil
end

#resubscribe_all?Boolean

Returns whether the tool asked to resubscribe to all categories.

Returns:

  • (Boolean)

    whether the tool asked to resubscribe to all categories



34
35
36
# File 'app/services/assistant/email_preference_update_request.rb', line 34

def resubscribe_all?
  attributes[:resubscribe_all].to_b
end

#unsubscribe_all?Boolean

Returns whether the tool asked to unsubscribe from all categories.

Returns:

  • (Boolean)

    whether the tool asked to unsubscribe from all categories



29
30
31
# File 'app/services/assistant/email_preference_update_request.rb', line 29

def unsubscribe_all?
  attributes[:unsubscribe_all].to_b
end

#updatesHash{String => Object}

Returns requested per-flag updates.

Returns:

  • (Hash{String => Object})

    requested per-flag updates



22
23
24
25
26
# File 'app/services/assistant/email_preference_update_request.rb', line 22

def updates
  @updates ||= (AudienceToolBuilder::CATEGORY_FLAGS + ['disable_email_tracking']).index_with do |flag|
    attributes[flag.to_sym]
  end.compact
end