Sha256: 57acaf062929c7b755abb3f90a362eac47e7809f4724c5a2ab15e67390556c02

Contents?: true

Size: 1.47 KB

Versions: 17

Compression:

Stored size: 1.47 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)
      if self.has_attribute?(:workflow_state) && payload['workflow_state'].present? && payload['workflow_state'] == 'deleted'
        # API will respond with 404, so just update the workflow_state
        update(workflow_state: payload['workflow_state'])
      else
        api_response = request_from_api
        assign_from_api_params(api_response)
      end
    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

17 entries across 17 versions & 1 rubygems

Version Path
canvas_sync-0.22.6 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.5 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.4 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.3 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.2 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.1 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta8 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta7 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta6 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta5 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta4 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta3 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta2 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.22.0.beta1 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.21.1 lib/canvas_sync/concerns/live_event_sync.rb
canvas_sync-0.21.1.beta1 lib/canvas_sync/concerns/live_event_sync.rb