Last active
October 13, 2015 02:37
-
-
Save mchail/4125771 to your computer and use it in GitHub Desktop.
Zapier Webhooks in Rails with Resque
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem "resque", :require => 'resque/server' | |
gem 'resque-history' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
name: "Steve McHail", | |
favorite_beer: "Dogfish Head 90 Minute" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_update :see_if_favorite_beer_changed | |
def see_if_favorite_beer_changed | |
if favorite_beer.changed? | |
params = { | |
name: name, | |
favorite_beer: favorite_beer | |
} | |
Zapier.zap(:beer_update, params) | |
end | |
rescue | |
logger.error "error telling hipchat about user's drinking problem" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
beer_update: UjvN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'resque-history' | |
class Zapier | |
extend Resque::Plugins::History | |
@queue = :zapier | |
@base_url = "https://zapier.com/hooks/catch/n/" | |
def self.perform(zap_id, params) | |
zap_url = "#{@base_url}#{zap_id}/" | |
RestClient.post(zap_url, params) | |
end | |
def self.zap(zap_name, params) | |
zap_id = ZAP_IDS[zap_name.to_s] | |
if zap_id.nil? | |
Rails.logger.error "No zap id found for zap with name '#{zap_name}'. Check zap_ids.yml" | |
return | |
end | |
Rails.logger.info "zapping #{zap_id} with params: #{params.to_json}" | |
if Rails.env.production? | |
Resque.enqueue(Zapier, zap_id, params) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ZAP_IDS = YAML.load_file(File.join(Rails.root, "config", "zap_ids.yml")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment