RailsApps / learn-rails

An example Rails 5.1 app to accompany the "Learn Ruby on Rails" book.
https://learn-rails.com/install-rails-mac/index.html
392 stars 266 forks source link

Update required for Gibbon 2.0 #65

Closed DanielKehoe closed 9 years ago

DanielKehoe commented 9 years ago

Gibbon 2.0.0 was released July 29, 2015.

When you run bundle install or bundle update the newest Gibbon 2.0 gem will be installed and the application will error with

NameError: uninitialized constant Gibbon::API

Either change the Gemfile and specify

gem 'gibbon',  '~>1.2.0'

or update the code that uses Gibbon.

See https://github.com/amro/gibbon/issues/131

asalom commented 9 years ago

Solved it with the following code:

mailchimp = Gibbon::Request.new(api_key: Rails.application.secrets.mailchimp_api_key)
mailchimp_id = Rails.application.secrets.mailchimp_list_id
result = mailchimp.lists(mailchimp_id).members.create(
    body: {
        :email_address => self.email,
        :status => 'subscribed'
    })
DanielKehoe commented 9 years ago

@asalom that's correct. I'm working on an update to the book.

asalom commented 9 years ago

Thank you, I'm really enjoying your book. It gave me the kickstart I needed to jump into the rails world. If it helps you in any way I improved my solution a little bit by handling the exceptions Gibbon raises when there are any problems with the request such as subscribing an email address more than once.

DanielKehoe commented 9 years ago

@asalom yes it'd be nice to see your exception handling code.

Just a note, that doesn't apply to the learn-rails application, but is relevant if the subscribe method runs as an Active Job: An exception doesn't disrupt the response to the user. You could handle the exception by sending an email to the administrator so you know there was a failure. Or you could install an exception notifier gem such as Rollbar. Otherwise, the exception just gets logged and you don't know unless you search the log files.

asalom commented 9 years ago

When I try to register an email that was previously registered with the new version of Gibbon I got a Gibbon::MailChimpError exception. I catch this on the subscribe method of the Visitor class and return one of the strings from the exception to the VisitorsController for showing as a flash message. The code is on GitHub: https://github.com/asalom/learn-rails/commit/40adf3779f2add384765e88bc03d9a4da3ca9902

DanielKehoe commented 9 years ago

Resolved with commit f4ff55b.