Class: Assistant::EmailPreferenceUpdateRequest
- Inherits:
-
Object
- Object
- Assistant::EmailPreferenceUpdateRequest
- Defined in:
- app/services/assistant/email_preference_update_request.rb
Overview
Normalizes and validates one preference-update request.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
readonly
Keyword arguments received by the assistant tool.
Instance Method Summary collapse
-
#email ⇒ String
Normalized email address.
- #initialize(attributes) ⇒ void constructor
-
#preflight_error ⇒ String?
Validates the request before any database changes.
-
#resubscribe_all? ⇒ Boolean
Whether the tool asked to resubscribe to all categories.
-
#unsubscribe_all? ⇒ Boolean
Whether the tool asked to unsubscribe from all categories.
-
#updates ⇒ Hash{String => Object}
Requested per-flag updates.
Constructor Details
#initialize(attributes) ⇒ void
9 10 11 |
# File 'app/services/assistant/email_preference_update_request.rb', line 9 def initialize(attributes) @attributes = attributes end |
Instance Attribute Details
#attributes ⇒ Hash (readonly)
Returns 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
#email ⇒ String
Returns 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_error ⇒ String?
Validates the request before any database changes.
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.
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.
29 30 31 |
# File 'app/services/assistant/email_preference_update_request.rb', line 29 def unsubscribe_all? attributes[:unsubscribe_all].to_b end |
#updates ⇒ Hash{String => Object}
Returns 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 |