ZapierRestHooks
Rails engine that provides functionality for Zapier REST hooks pattern.
Installation
Add this line to your application's Gemfile:
gem 'zapier_rest_hooks'
After you've added ZapierRestHooks to your gemfile, you can install it with:
rails generate zapier_rest_hooks:install
The generator will mount ZapierRestHooks in your config/routes.rb at the path /hooks
.
Now run rake db:migrate
Usage
By adding ZapierRestHooks engine to your Rails app, you will have access to ZapierRestHooks::Hook model. This model maps the Hook object described on Zapier REST Hooks pattern.
Here's an example on how you can integrate hooks into your existing models.
Note:
This is simple example, in real life you should wrap ZapierRestHooks::Hook.trigger
inside a Job.
A hook can be scoped to an owner. By default it's nil but you can specify it if you need it.
class Candidate < ActiveRecord::Base
# Relations
belongs_to :organization
# Callbacks
after_create :trigger_hooks_with_owner, if: :organization
after_create :trigger_hooks_without_owner, unless: :organization
private
def trigger_hooks_with_owner
return unless ZapierRestHooks::Hook.hooks_exist?('new_candidate', organization)
# Scoped event.
ZapierRestHooks::Hook.trigger('new_candidate', self.to_json, organization)
end
def trigger_hooks_without_owner
return unless ZapierRestHooks::Hook.hooks_exist?('new_candidate')
# Global event.
ZapierRestHooks::Hook.trigger('new_candidate', self.to_json)
end
end
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/esbanarango/zapier-REST-hooks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Author
This was written by Esteban Arango Medina, based on Ryan Alyn Porter initial solution.
License
The gem is available as open source under the terms of the MIT License.