Search code examples
ruby-on-railsmailchimpmailchimp-api-v3.0gibbon

GIBBON::must set an api_key prior to making a call - rails


I used a tutorial to set up my app with mailchimp so that it sends a new subscriber to a mailchimp list. Everything is working just fine if you go through the flow. But when I ran Rspec Half of my test suite is red with this error Gibbon::GibbonError:You must set an api_key prior to making a call. I tried setting my api key where it says but it didn't work? I'll post some code for clarity.

Gibbon.rb:

Gibbon::API.api_key = ENV["MAILCHIMP_API_KEY"]
Gibbon::API.timeout = 15
Gibbon::API.throws_exceptions = true

JOB

     class SubscribeUserToMailingListJob < ActiveJob::Base
  queue_as :default

  def perform(subscriber)
    gb = Gibbon::API.new(Figaro.env.mailchimp_api_key)
    gb.lists.subscribe({:id => ENV["MAILCHIMP_LIST_ID"], :email => {:email => subscriber.email}, :merge_vars => {:FNAME => subscriber.first_name, :LNAME => subscriber.last_name}, :double_optin => false})
  end
end

MODEL:

 def subscribe_user_to_mailing_list
   SubscribeUserToMailingListJob.perform_later(self)
 end

ERROR

 Create a subscriber Subscriber can sign in
  Failure/Error: gb.lists.subscribe({:id => ENV["MAILCHIMP_LIST_ID"],   :email => {:email => subscriber.email}, :merge_vars => {:FNAME =>   subscriber.first_name, :LNAME => subscriber.last_name}, :double_optin =>   false})

Gibbon::GibbonError:
You must set an api_key prior to making a call

I'm lost on this one. Any help would be great! thank you.


Solution

  • What about generating a MailChimp API-key for a start? If you have a MailChimp account you can easily get one in the extra´s menu.

    Replace "MAILCHIMP_API_KEY" with your key in the line: Gibbon::API.api_key = ENV["MAILCHIMP_API_KEY"]