Created
May 12, 2015 13:55
-
-
Save Griminy/83821a732cc829f0e1da to your computer and use it in GitHub Desktop.
some some some some
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 'sidekiq' | |
require 'json' | |
require 'net/http' | |
require 'net/https' | |
require 'uri' | |
module MagazCore | |
class WebhooksWorker | |
include Sidekiq::Worker | |
def perform(format, address, mail) | |
uri = URI.parse("#{address}") | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = true | |
req = Net::HTTP::Post.new(uri.path, initheader = {"Content-Type" =>"application/#{format}"}) | |
req.body = "[ #{mail} ]" | |
res = https.request(req) | |
puts "Response #{res.code} #{res.message}: #{res.body}" | |
end | |
end | |
end | |
///////////////////////////// | |
require 'json' | |
ActiveSupport::Notifications.subscribe 'create product event' do |name, data| | |
shop = MagazCore::Shop.find(data[:event][:shop_id]) | |
webhook = shop.webhooks.find_by(topic: "Product creation") | |
run_worker(data: data, webhook: webhook) | |
end | |
def run_worker(data: {}, webhook:) | |
unless webhook == nil | |
if webhook.format == "XML" | |
mail = data.to_xml | |
else | |
mail = data.to_json | |
end | |
format = webhook.format.downcase | |
address = webhook.address | |
MagazCore::WebhooksWorker.perform_async(format, address, mail) | |
end | |
end | |
/////////////////////////////////////// | |
def call(subject:, message:, verb:) | |
@event = subject.events.new | |
event_name = [verb, class_name(subject: subject).downcase, 'event'].join(" ") | |
MagazCore::Event.connection.transaction do | |
begin | |
_create_event!(subject: subject, event: @event, | |
message: message, verb: verb, event: @event) | |
ActiveSupport::Notifications.publish(event_name, event: @event) || fail(ArgumentError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment