-
-
Save alxekb/039920919033446848764507751c08c0 to your computer and use it in GitHub Desktop.
same code
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 'logger' | |
class FetchContentAdDomainsWorker | |
include Sidekiq::Worker | |
extend IntegrationsWrapper | |
URL = 'https://rest.content.ad/reports/publisher?output=json'.freeze | |
sidekiq_options retry: 3, queue: :integrations, backtrace: true | |
wrap_over :network_setting, :content_ad | |
def perform(network_settings_id) | |
network_settings = NetworkSetting.find(network_settings_id) | |
organization = network_settings.organization | |
response = ContentAdRequestExecutor.new(URL, network_settings).execute | |
prepared_response = JSON.parse(response.body, symbolize_names: true) | |
prepared_response[:Domains].each do |domain| | |
network_settings.content_ad_domains.find_or_create_by(name: domain[:Name], domain_id: domain[:ID], organization_id: organization.id) | |
end | |
network_settings.update!(error_message: '') | |
rescue JSON::ParserError => exception | |
logger.error exception.message | |
logger.error exception.backtrace.join("\n") | |
network_settings.update!(error_message: 'API key or password is wrong. Please check the information.') | |
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
module IntegrationsWrapper | |
def wrap_over(type, ns_type) | |
case type | |
when :ad_tag then wrap_ad_tag(ns_type) | |
when :network_setting then wrap_network_setting(ns_type) | |
when :website then wrap_website(ns_type) | |
when :analytic_user_account then wrap_analytic_user_account(ns_type) | |
end | |
end | |
private | |
def wrap_network_setting(ns_type) | |
wrap_module = Module.new do | |
define_method(:perform) do |network_setting_id| | |
Rails.logger.debug 'Create a record about worker started!' | |
network_setting = NetworkSetting.find(network_setting_id) | |
organization = network_setting.organization | |
meta = { | |
network_setting_id: network_setting.id | |
} | |
integration_datum = IntegrationsDatum.create!( | |
organization_id: organization.id, | |
network_type: IntegrationsDatum.network_types[ns_type], | |
meta: meta | |
) | |
begin | |
super network_setting_id | |
rescue => error | |
Rails.logger.error 'Create a record about worker fell!' | |
integration_datum.update!(error_message: error.message, fell: true) | |
raise error # to errbit | |
end | |
end | |
end | |
self.prepend wrap_module | |
end | |
def wrap_website(ns_type) | |
wrap_module = Module.new do | |
define_method(:perform) do |website_id| | |
Rails.logger.debug 'Create a record about worker started!' | |
website = Website.find(website_id) | |
organization = website.organization | |
network_setting = website.send("#{ns_type}_network_setting") | |
meta = { | |
website_id: website_id, | |
network_setting_id: network_setting.id | |
} | |
integration_datum = IntegrationsDatum.create!( | |
organization_id: organization.id, | |
network_type: IntegrationsDatum.network_types[ns_type], | |
meta: meta | |
) | |
begin | |
super website_id | |
rescue => error | |
Rails.logger.error 'Create a record about worker fell!' | |
integration_datum.update!(error_message: error.message, fell: true) | |
raise error # to errbit | |
end | |
end | |
end | |
self.prepend wrap_module | |
end | |
def wrap_ad_tag(ns_type) | |
wrap_module = Module.new do | |
define_method(:perform) do |ad_tag_id| | |
Rails.logger.debug 'Create a record about worker started!' | |
ad_tag = AdTag.find(ad_tag_id) | |
website = ad_tag.website | |
organization = website.organization | |
network_setting = website.send("#{ad_tag.network_type}_network_setting") | |
meta = { | |
ad_tag_id: ad_tag&.id, | |
website_id: website&.id, | |
network_setting: network_setting&.id | |
} | |
integration_datum = IntegrationsDatum.create!( | |
organization_id: organization.id, | |
network_type: IntegrationsDatum.network_types[ns_type], | |
meta: meta | |
) | |
begin | |
super ad_tag_id | |
rescue => error | |
Rails.logger.error 'Create a record about worker fell!' | |
integration_datum.update!(error_message: error.message, fell: true) | |
raise error # to errbit | |
end | |
end | |
end | |
self.prepend wrap_module | |
end | |
def wrap_analytic_user_account(ns_type) | |
wrap_module = Module.new do | |
define_method(:perform) do |analytic_user_account_id| | |
Rails.logger.debug 'Create a record about worker started!' | |
analytic_user_account = AnalyticUserAccount.find(analytic_user_account_id) | |
organization = analytic_user_account.organization | |
meta = { | |
analytic_user_account_id: analytic_user_account.id | |
} | |
integration_datum = IntegrationsDatum.create!( | |
organization_id: organization.id, | |
network_type: IntegrationsDatum.network_types[ns_type], | |
meta: meta | |
) | |
begin | |
super analytic_user_account_id | |
rescue => error | |
Rails.logger.error 'Create a record about worker fell!' | |
integration_datum.update!(error_message: error.message, fell: true) | |
raise error # to errbit | |
end | |
end | |
end | |
self.prepend wrap_module | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment