Skip to content

Instantly share code, notes, and snippets.

@mchail
Last active October 13, 2015 02:37
Show Gist options
  • Save mchail/4125771 to your computer and use it in GitHub Desktop.
Save mchail/4125771 to your computer and use it in GitHub Desktop.
Zapier Webhooks in Rails with Resque
gem "resque", :require => 'resque/server'
gem 'resque-history'
{
name: "Steve McHail",
favorite_beer: "Dogfish Head 90 Minute"
}
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
beer_update: UjvN
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
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