Sha256: 58762a2f010493d80d766cded1388bd7db07f164f1db02107cd5743cc6ccd321

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Logux
  module Process
    class Action
      attr_reader :stream, :chunk
      attr_accessor :stop_process

      def initialize(stream:, chunk:)
        @stream = stream
        @chunk = chunk
      end

      def call
        process_authorization!
        process_action!
      end

      def action_from_chunk
        @action_from_chunk ||= chunk[:action]
      end

      def meta_from_chunk
        @meta_from_chunk ||= chunk[:meta]
      end

      def stop_process?
        @stop_process ||= false
      end

      def stop_process!
        @stop_process = true
      end

      private

      def process_action!
        return if stop_process?

        action_caller = Logux::ActionCaller.new(
          action: action_from_chunk,
          meta: meta_from_chunk
        )

        stream.write(action_caller.call!.format)
      end

      def process_authorization!
        policy_caller = Logux::PolicyCaller.new(action: action_from_chunk,
                                                meta: meta_from_chunk)
        policy_check = policy_caller.call!
        status = policy_check ? :approved : :forbidden
        stream.write([status, meta_from_chunk.id])
        return stream.write(',') if policy_check

        stop_process!
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
logux-rack-0.1.0 lib/logux/process/action.rb
logux_rails-0.1.0 lib/logux/process/action.rb