require 'active_support/concern' module PlatformSdk module ActiveRecord module DataPipelineable extend ActiveSupport::Concern included do after_create -> { send_to_pipeline('created') } before_destroy -> { send_to_pipeline('destroyed') } after_update -> { send_to_pipeline('modified') } end def pipeline_payload(action) { "noun": self.class.pipeline_noun, "identifiers": pipeline_identifiers, "meta": self.class.pipeline_meta, "data": pipeline_data(action), "envelope_version": '1.0.0', "message_timestamp": Time.current.utc.iso8601 } end def self.pipeline_noun raise NotImplementedError, 'You must implement the pipeline_noun class method' end def self.pipeline_meta raise NotImplementedError, 'You must implement the pipeline_meta class method, an example value is { "source": "strongmind-central" }' end def pipeline_excluded_attributes [] end def pipeline_identifiers { "id": id } end def pipeline_data(action) data_hash = { action: }.merge!(attributes.symbolize_keys) data_hash.merge!(pipeline_additional_attributes) Rails.logger.info("Data Hash after merge of additional attrs: #{data_hash}") data_hash = data_hash.except(*pipeline_excluded_attributes) Rails.logger.info("Data Pipeline Payload: #{data_hash}") data_hash end def pipeline_additional_attributes {} end def one_roster_pipeline_payload(deleted: false) {} end def send_to_pipeline(action) credentials = { pipeline_host: ENV.fetch('DATA_PIPELINE_HOST', 'stage-di-data-pipeline-api.strongmind.com'), pipeline_username: ENV.fetch('DATA_PIPELINE_USERNAME', 'canvas_prod'), pipeline_password: ENV.fetch('DATA_PIPELINE_PASSWORD', '') } client = PlatformSdk::DataPipeline::Client.new(credentials) begin client.post(pipeline_payload(action)) unless Rails.env.development? rescue => e Rails.logger.info("Error posting to data pipeline: #{e.message}") end return unless respond_to?(:one_roster_data_type) && !Rails.env.development? Rails.logger.info('POSTING TO ONE ROSTER...') client.post(one_roster_pipeline_payload(deleted: action == "destroyed")) end end end end