2

In my app's user settings, a user can subscribe or unsubscribe to a MailChimp list by checking a box. I am having some trouble retrieving the users MailChimp member_id.

I created a .env file with:

MAILCHIMP_API_KEY = "my_api_key"
MAILCHIMP_LIST_ID = "my_list_id"

I created an initializer called gibbon.rb with:

Gibbon::Request.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::Request.timeout = 15

I use this method in the user.rb

def mailchimp_news
  @mailchimp_list_id = ENV["MAILCHIMP_LIST_ID"]
  @gibbon = Gibbon::Request.new
  if self.news_email == true
    @gibbon.lists(@mailchimp_list_id).members.update(body: {
    email_address: self.email,
    status: "subscribed",
    merge_fields: {FNAME: self.name,
                   LNAME: ""}
    })
  elsif self.news_email == false
    @gibbon.lists(@mailchimp_list_id).members(member_id).update(body: { status: "unsubscribed" })
end

end

And call it with an after check

after_save :mailchimp_news

I receive this error: undefined local variable or method `member_id'. How do I get the member_id?

Phil Mok
  • 3,860
  • 6
  • 24
  • 36
  • You have an error in your Ruby code, and an error is being raised before a call to the Mailchimp API is being made. You're referring to a variable or method named `member_id` and that's not defined. The first step is to fix this bug. – balexand Aug 30 '15 at 23:36

1 Answers1

10

From the How To Manage Subscribers help page, in API v3 the subscriber ID is the MD5 hash of the lower case version of the user's email address.

So you'll want to use Digest::MD5.hexdigest(self.email.downcase).

TooMuchPete
  • 4,583
  • 2
  • 17
  • 21