class MailchimpNotificationHandler
Constants
- NO_OP_LAMBDA
Public Class Methods
new(params, logger, logic, bool_cols=[])
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 4 def initialize(params, logger, logic, bool_cols=[]) @params = params @logger = logger @bool_columns = bool_cols @logic = logic @logic.default = NO_OP_LAMBDA # These attrs get populated when the @params hash is parsed. @notification_type = '' @data = {} @message = '' end
Public Instance Methods
process()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 17 def process if valid_notification? parse_params @logger.info("[Mailchimp (#{@params['fired_at']})] #{@message}") @logic[@notification_type].call(@data) else @logger.error("[Mailchimp] Invalid notification (possible reasons: forged notification from somebody besides Mailchimp, incorrect secret code, invalid notification from Mailchimp, or invalid handling of notification.)\nparams => #{@params}") end end
Private Instance Methods
cleaned_reason()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 74 def cleaned_reason case @params['data']['reason'] when 'hard' then "because of a hard bounce on the recipient's server." when 'abuse' then "due to abuse." else 'for some UNKNOWN reason.' end end
human_readable_merge_vars()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 82 def human_readable_merge_vars result = 'Merge variables are:' merge_vars.each_pair do |key, value| result << "\n\t#{key} => #{value}" end return result end
merge_vars()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 91 def merge_vars if @params['data'].has_key?('merges') result = {} @params['data']['merges'].each_pair do |key, value| value = num_to_bool(value) if @bool_columns.include?(key.to_s) result[key] = value end return result end return nil end
num_to_bool(string)
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 103 def num_to_bool(string) (string == '0' || sttring.nil? || string.empty?) ? false : true end
parse_params()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 40 def parse_params @notification_type = @params['type'] @data = @params['data'] @data['fired_at'] = @params['fired_at'] case @notification_type when 'subscribe' then @message << "#{@data['email']} subscribed on Mailchimp! " @message << human_readable_merge_vars when 'unsubscribe' then @message << "#{@data['email']} was unsubscribed from Mailchimp #{unsubscription_reason} " @message << human_readable_merge_vars when 'profile' then @message << "#{@data['email']}'s profile was updated on Mailchimp. " @message << human_readable_merge_vars when 'upemail' then @message << "the owner of #{@data['old_email']} changed their email address to #{@data['new_email']} on Mailchimp." when 'cleaned' then @message << "#{@params['data']['email']} was cleaned from Mailchimp #{cleaned_reason}" when 'campaign' then @message << "the campaign with subject '#{@data['subject']}' was #{@data['status']}." else @message << "WARNING: Unrecognized Mailchimp notification type '#{@notification_type}'!" end end
unsubscription_reason()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 66 def unsubscription_reason case @params['data']['reason'] when 'manual' then 'by request.' when 'abuse' then 'due to abuse.' else 'for some UNKNOWN reason.' end end
valid_notification?()
click to toggle source
# File lib/mcmailer/mailchimp_notification_handler.rb, line 30 def valid_notification? # NOTE: False cases below aren't exhaustive. Implement more # false cases as necessary. return false unless @params.has_key?('type') return false unless @params.has_key?('fired_at') return false unless @params.has_key?('data') return true end