1

I've recently added the ability to subscribe people to a mailchimp list from my rails app using the Gibbon gem.

It is working perfectly to subscribe people to list when they register, and when they login it updates their information on the mailchimp list (eg login count and last_login_date).

However there are other users using the app that I would like to add to the mailchimp list but because they already have an account they aren't going to sign up again, therefore won't get subscribed.

I'm trying to add an if statement to the login page that subscribes them to the list if they don't already exist, but can't figure out the syntax.

If, for example, I test it to find a user that I know doesn't exist, like this:

Gibbon::API.lists.member_info({:id => list_id, :emails => [{:email => "d@duncanma.com"}]})

Then it returns this:

{"success_count"=>0, "error_count"=>1, "errors"=>[{"email"=>{"email"=>"d@duncanma.com"}, "error"=>"The id passed does not exist on this list", "code"=>232}], "data"=>[]}

But what I need is just a true or false...

I've tried:

Gibbon::API.lists.member_info({:id => list_id, :emails => [{:email => "d@duncanma.com"}]}).include("success_count"=>0)?

but that obviously doesn't work as it says undefined method include? for Hash...

Any ideas on how I can get it to a true or false boolean?

camillavk
  • 521
  • 5
  • 20
  • I can suggest a bit awkward solution, but I have done it getting all members of list using `Gibbon::API.lists.members({:id => list_id})` and the manually parsing the response to check if a particular user exists there. – usmanali May 27 '15 at 09:31
  • Hmm, I guess I could do that but that feels like a bit of an awkward solution, as you said...the list already includes a few thousand people so that might slow the process down quite a bit, right? – camillavk May 27 '15 at 09:42

2 Answers2

0

OK so eventually after trying a few different things I got around this by doing this:

begin
  @list_member = Gibbon::API.lists.member_info({:id => list_id,
                             :emails => [{:email => current_user.email}]
                             })
  if @list_member["success_count"] = 1
    Gibbon::API.lists.update_member({:id => list_id,
              :email => {:email => current_user.email},
              :merge_vars => {:MMERGE4 => current_user.trips.count,
              :MMERGE13 => Date.today,
              :MMERGE5 => current_user.miles_sailed}
              })
  elsif @list_member["success_count"] = 0
    Gibbon::API.lists.subscribe({:id => list_id,
              :email => {:email => current_user.email},
              :merge_vars => {:FNAME => current_user.name,
              :LNAME => current_user.last_name,
              :MMERGE3 => current_user.login},
              :double_optin => false})
  end
rescue Gibbon::MailChimpError => e
  return redirect_to root_path, :flash => {error: e.message}
end
camillavk
  • 521
  • 5
  • 20
0

If your goal only to discover whether an email is on your list, that last answer works well. If your ultimate goal is a "create or update", then you just want to pass the update_existing parameter into the subscribe call.

See the API Docs for /lists/subscribe for more details.

TooMuchPete
  • 4,583
  • 2
  • 17
  • 21