This gem is a plugin for Webhookr that enables your application to accept webhooks from Mailchimp.
Add this line to your application's Gemfile:
gem 'webhookr-mailchimp'
Or install it yourself:
$ gem install webhookr-mailchimp
Once you have the gem installed run the generator to add the code to your initializer. An initializer will be created if you do not have one.
rails g webhookr:mailchimp:init *initializer_name* -s
Run the generator to create an example file to handle MailChimp webhooks.
rails g webhookr:mailchimp:example_hooks
Or create a MailChimp handler class for any event that you want to handle. For example to handle unsubscribes you would create a class as follows:
class MailChimpHooks
def on_unsubscribe(incoming)
# Your custom logic goes here.
User.unsubscribe_newletter(incoming.payload.data.email)
end
end
For a complete list of events, and the payload format, see below.
Edit config/initializers/initializer_name and change the commented line to point to your custom Mailchimp event handling class. If your class was called MailChimpHooks the configuration line would look like this:
Webhookr::Mailchimp::Adapter.config.callback = MailChimpHooks
To see the list of MailChimp URLs for your application can use when you configure MailChimp webhooks, run the provided webhookr rake task:
rake webhookr:services
Example output:
mailchimp:
GET /webhookr/events/mailchimp/19xl64emxvn
POST /webhookr/events/mailchimp/19xl64emxvn
All webhook events are supported. For further information on these events, see the MailChimp documentation.
MailChimp Event | Event Handler |
---|---|
subscribe | on_subscribe(incoming) |
unsubscribe | on_unsubscribe(incoming) |
profile | on_profile(incoming) |
upemail | on_upemail(incoming) |
cleaned | on_cleaned(incoming) |
campaign | on_campaign(incoming) |
The payload is the full payload data from as per the MailChimp documentation, converted to an OpenStruct for ease of access. Examples for the method call unsubscribe:
incoming.payload.fired_at
incoming.payload.data.action
incoming.payload.data.reason
incoming.payload.data.id
incoming.payload.data.list_id
incoming.payload.data.email
incoming.payload.data.email_type
incoming.payload.data.merges.EMAIL
incoming.payload.data.merges.FNAME
incoming.payload.data.merges.LNAME
incoming.payload.data.merges.INTERESTS
incoming.payload.data.ip_opt
incoming.payload.data.campaign_id
incoming.payload.data.reason
webhookr-mailchimp works with Rails 3.1, 3.2 and 4.0, and has been tested on the following Ruby implementations:
- 1.9.3
- 2.0.0
- jruby-19mode
This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, once this gem reaches a 1.0 release, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:
spec.add_dependency 'webhookr-mailchimp', '~> 1.0'
While this gem is currently a 0.x release, suggestion is to require the exact version that works for your code:
spec.add_dependency 'webhookr-mailchimp', '0.0.1'
webhookr-mailchimp is released under the MIT license.