Sha256: f645eebd7bcd3466a4479754c3bb8afe521e841a4bce2140e7df48a69ee932df

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module CanvasSync::Concerns
  module LiveEventSync
    extend ActiveSupport::Concern

    included do
      define_model_callbacks :process_live_event
    end

    class_methods do
      def cs_internal_process_live_event(event)
        meta = event[:metadata]
        payload = event[:payload] || event[:body]

        canvas_id = payload[:id] || payload[:"#{name.underscore}_id"]
        inst = self.find_or_initialize_by(canvas_id: canvas_id)
        model, _, subtype = meta[:event_name].rpartition('_')

        result = inst.run_callbacks(:process_live_event) do
          inst.process_live_event(subtype.to_sym, payload, meta)
        end

        inst.save! if result != false && inst.changed?
      end
    end

    def process_live_event(event_type, payload, metadata)
      api_response = request_from_api
      assign_from_api_params(api_response)
    end
  end

  CanvasSync::LiveEvents.listen do |event|
    meta = event[:metadata]
    payload = event[:body]

    event_type = meta[:event_name]

    model, _, subtype = event_type.rpartition('_')
    mcls = model.classify.constantize rescue nil

    if mcls.present? && mcls < LiveEventSync
      mcls.cs_internal_process_live_event(event)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
canvas_sync-0.21.0 lib/canvas_sync/concerns/live_event_sync.rb