ruby-on-railsrailsapps

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


Running OSX Mavericks, ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0], rvm 1.25.23 (master), and rails-4.1.0 (allegedly)

I'm working through the railsapps.org book on learning rails I have finished implementing the mailchimp email list code, and when I press submit on my form, I get the following error:

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

I was working through an invalid URI error, and it mysteriously disappeared over the weekend (I haven't touched the Mac since last Friday). Now I have this new error.

My API Key and List ID are valid and properly set. If I look back in the log, I see another error that the mailchimp.lists.subscribe method is missing.

Here's the code as implemented from the book:

class Visitor < ActiveRecord::Base
  has_no_table
  column :email, :string
  validates_presence_of :email
  validates_format_of :email, :with => /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i

  def subscribe
  mailchimp = Gibbon::API.new
  result = mailchimp.lists.subscribe({
    :id => Rails.application.secrets.mailchimp_list_id,
    :email => {:email => self.email},
    :double_optin => false,
    :update_existing => true,
    :send_welcome => true
  })
  Rails.logger.info("Subscribed #{self.email} to MailChimp") if result
end

end

I hate being a noob when I can't debug for myself... Replies are appreciated.

Regards,

Jeff


Solution

  • Gibbon automatically looks for the environment variable MAILCHIMP_API_KEY and Gibbon will use it when you create an instance. If you haven't set a Unix env variable MAILCHIMP_API_KEY, you will need to set it explicitly. To hardcode it:

     mailchimp = Gibbon::API.new("your_api_key")
    

    Or obtain it from the config/secrets.yml file:

     mailchimp = Gibbon::API.new(Rails.application.secrets.mailchimp_api_key)