docbrown/app/labor/mailchimp_bot.rb
Jacob Herrington 099b188530 Rename uncommunicative variables (#3890)
* Clarify uncommunicative variables in labor classes

Single letter variable names are largely a question of preference, in
some cases I think that convention mitigates the opaque nature of single
letter variable names (e.g., e for error, i for index, etc).

However, in some cases they can be unclear and there isn't much reason
to use single letter variables unless for some reason character length is
really important.

In this case, I would prefer clarity in variable names over brevity of
code so I've used Reek to identify short variable names and I'm changing
them.

It's pretty boring, but hopefully incremental code love changes like
this one add up and improve readability and accessibility for those
interested in reading this codebase.

* Remove commented code from 2 years ago

* Clarify uncommunicative naming in services classes

There is some context for this change in 6b81880f

* Clarify uncommunicative naming in models

There is some context for this change in 6b81880f

* Clarify uncommunicative naming in liquid_tags

There is some context for this change in 6b81880f
2019-09-02 13:05:07 -04:00

173 lines
4.7 KiB
Ruby

class MailchimpBot
attr_reader :user, :saved_changes, :gibbon
def initialize(user)
@user = user
@saved_changes = user.saved_changes
@gibbon = Gibbon::Request.new
end
def upsert
return true unless Rails.env.production? || Rails.env.test?
manage_community_moderator_list
manage_tag_moderator_list
upsert_to_newsletter
end
def upsert_to_newsletter
# attempt to update email if user changed email addresses
success = false
begin
gibbon.lists(ApplicationConfig["MAILCHIMP_NEWSLETTER_ID"]).members(target_md5_email).upsert(
body: {
email_address: user.email,
status: user.email_newsletter ? "subscribed" : "unsubscribed",
merge_fields: {
NAME: user.name.to_s,
USERNAME: user.username.to_s,
TWITTER: user.twitter_username.to_s,
GITHUB: user.github_username.to_s,
IMAGE_URL: user.profile_image_url.to_s,
ARTICLES: user.articles.size,
COMMENTS: user.comments.size,
ONBOARD_PK: user.onboarding_package_requested.to_s,
EXPERIENCE: user.experience_level || 666,
COUNTRY: user.shipping_country.to_s,
STATE: user.shipping_state.to_s,
POSTAL_ZIP: user.shipping_postal_code.to_s
}
},
)
success = true
rescue Gibbon::MailChimpError => e
report_error(e)
end
success
end
def manage_community_moderator_list
return false unless user.has_role?(:trusted)
success = false
status = user.email_community_mod_newsletter ? "subscribed" : "unsubscribed"
begin
gibbon.lists(ApplicationConfig["MAILCHIMP_COMMUNITY_MODERATORS_ID"]).members(target_md5_email).upsert(
body: {
email_address: user.email,
status: status,
merge_fields: {
NAME: user.name.to_s,
USERNAME: user.username.to_s,
TWITTER: user.twitter_username.to_s,
GITHUB: user.github_username.to_s,
IMAGE_URL: user.profile_image_url.to_s
}
},
)
success = true
rescue Gibbon::MailChimpError => e
report_error(e)
end
success
end
def manage_tag_moderator_list
return false unless user.tag_moderator?
success = false
tags = user.roles.where(name: "tag_moderator").map { |tag| Tag.find(tag.resource_id).name }
status = user.email_tag_mod_newsletter ? "subscribed" : "unsubscribed"
begin
gibbon.lists(ApplicationConfig["MAILCHIMP_TAG_MODERATORS_ID"]).members(target_md5_email).upsert(
body: {
email_address: user.email,
status: status,
merge_fields: {
NAME: user.name.to_s,
USERNAME: user.username.to_s,
TWITTER: user.twitter_username.to_s,
GITHUB: user.github_username.to_s,
IMAGE_URL: user.profile_image_url.to_s,
TAGS: tags.join(", ")
}
},
)
success = true
rescue Gibbon::MailChimpError => e
report_error(e)
end
success
end
def unsub_sustaining_member
return unless user.tag_moderator?
gibbon.lists(ApplicationConfig["MAILCHIMP_TAG_MODERATORS_ID"]).members(target_md5_email).update(
body: {
status: "unsubscribed"
},
)
end
def unsub_community_mod
return unless user.has_role?(:trusted)
gibbon.lists(ApplicationConfig["MAILCHIMP_COMMUNITY_MODERATORS_ID"]).members(target_md5_email).update(
body: {
status: "unsubscribed"
},
)
end
def unsub_tag_mod
return unless a_sustaining_member?
gibbon.lists(ApplicationConfig["MAILCHIMP_SUSTAINING_MEMBERS_ID"]).members(target_md5_email).update(
body: {
status: "unsubscribed"
},
)
end
def unsubscribe_all_newsletters
success = false
begin
gibbon.lists(ApplicationConfig["MAILCHIMP_NEWSLETTER_ID"]).members(target_md5_email).update(
body: {
status: "unsubscribed"
},
)
unsub_tag_mod
unsub_sustaining_member
unsub_community_mod
success = true
rescue Gibbon::MailChimpError => e
report_error(e)
end
success
end
private
def a_sustaining_member?
# Reasoning for including => saved_changes["monthly_dues"]
# Is that mailchimp should be updated if a user decides to
# unsubscribes
user.monthly_dues.positive? || saved_changes["monthly_dues"]
end
def md5_email(email)
Digest::MD5.hexdigest(email.downcase)
end
def report_error(exception)
logger = Logger.new(STDOUT)
logger.error(exception)
end
def target_md5_email
email = saved_changes["unconfirmed_email"] ? saved_changes["email"][0] : user.email
md5_email(email)
end
end