Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when utilizing Gibbon gem via delayed_job?

Without delayed_job, this code works fine; if I include delayed_job, I get an error of uninitialized constant EmailNewsletter::Gibbon with every attempt the worker makes.

Rails 3.0.7, Gibbon 0.1.5 (a gem for working with MailChimp), delayed_job 2.1.4.

Controller

def subscribe
  email = params[:email]
  EmailNewsletter.subscribe(email)
  render(:update) do |page|
    page << "...view update code..."
  end
end

lib/email_newsletter.rb

module EmailNewsletter
  def self.subscribe(email)
    g = Gibbon::API.new('api_key_here', :id => 'list_id_here')
    g.listSubscribe(:email_address => email)
  end
end

With the above code, if I post an email address to /subscribe, everything works fine. I want to incorporate delayed_job so that my app doesn't feel slow if MailChimp takes a beat too long to respond.

Controller

def subscribe
  email = params[:email]
  EmailNewsletter.delay.subscribe(email)
  render(:update) do |page|
    page << "...view update code..."
  end
end

It looks like the job goes into the delayed_job table just fine. The handler data is:

--- !ruby/struct:Delayed::PerformableMethod 
object: !ruby/module EmailNewsletter
method_name: :subscribe
args: 
- [email protected]

A second later the worker picks it up and runs, and I get an error: uninitialized constant EmailNewsletter::Gibbon.

application.rb includes config.autoload_paths += %W(#{Rails.root}/lib).

What am I doing wrong?

Edit

For clarification, the line that the error is being thrown on is

g = Gibbon::API.new('api_key_here', :id => 'list_id_here')

I have also tried

g = ::Gibbon::API.new('api_key_here', :id => 'list_id_here')

like image 948
jaacob Avatar asked May 09 '11 22:05

jaacob


1 Answers

it just can't find the Gibbon libs

require 'gibbon' #in the module to help it find it

-7 months later, but hey

Or better yet, there is no Gibbon::API class, Gibbon itself is the class, not a module.

api = Gibbon.new("abc123-us2")
like image 181
winfred Avatar answered Oct 12 '22 13:10

winfred