2

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.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
Bitwise
  • 8,021
  • 22
  • 70
  • 161

4 Answers4

4

OP, I am assuming that in your tests you had a MAILCHIMP_API_KEY env var set, and yet Gibbon was still giving you the error as if you hadn't got one at all?

The issue is that Gibbon expects any API key string to be formatted as per Mailchimp's API keys. So if in your test you're using string api_key_123, you can set that without issue using Gibbon::API.api_key = "api_key_123", but at the point where you try to use it, Gibbon will act like it doesn't exist. Whereas if you use a Mailchimp-formatted key ,like 8472f063cf868ab9a49ccbb118c1d5f1-us15, Gibbon will acknowledge its existence.

Patrick
  • 165
  • 2
  • 13
1

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"]

Pim van der Heijden
  • 6,956
  • 4
  • 16
  • 21
0

I think you should require the gem dotenv like this in your model

class SubscribeUserToMailingListJob < ActiveJob::Base
  queue_as :default

  require 'dotenv'
  Dotenv.load


  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
0

A follow up on this question. The main reason was missing env variable in test environment and even if one mocks the env variable in before block, it won't work as Gibbon sets the API key in the initializer. As a fix for this without using a real API key for test env, we can set a test API directly in before block of the test like

def before
  Gibbon::Request.api_key = 'test-us3'
end

Please note the "-" in the key. That's the trick which passed the validation in the gem as implemented here

Peter T.
  • 8,757
  • 3
  • 34
  • 32