Sha256: b05a7c76cecf65704a862978005af8fa8a92652b3c9c1126d726eaa3dfed983a

Contents?: true

Size: 1.82 KB

Versions: 24

Compression:

Stored size: 1.82 KB

Contents

require 'rubygems'
require 'json'

module Factor
  module Runtime
    class Message
      attr_accessor :body, :workflow, :workflow_instance_id, :activity_instance_id, :last_activity_instance_id, :position
      def initialize
        @activity_instance_id=SecureRandom.hex
        @workflow = workflow
        @body = Hash.new
        @position = Array.new
      end
    
      def respond(params, event)
        m = Message.new
        m.body = @body
        m.workflow_instance_id=@workflow_instance_id
        m.last_activity_instance_id = @activity_instance_id
        m.workflow = @workflow
        m.position = @position
        m.position << "on"
        m.position << event
        m.add_values(params)
        m
      end
    
    
      def add_values(values)
        current=@body
        position.each do |key|
          #puts "[add value] #{key} (#{key.class.name})"
          current[key]={} if !current.include?(key)
          current=current[key]
        end
        values.each do |key,value|
          current[key]=value
        end
      
      end
    
      def route
        "#{workflow}.#{position.join('.')}"
      end
    
      def payload
        {"body"=>@body, "workflow_instance_id"=>@workflow_instance_id, "activity_instance_id"=>@activity_instance_id, "last_activity_instance_id"=>@last_activity_instance_id}.to_json
      end
    
      def from_queue routing_key, payload
        routing_array=routing_key.split('.')
        @workflow=routing_array.first #first
        @position=routing_array.drop(1) # everything after first
      
        message=JSON.parse(payload)
        @body=message["body"]
        @workflow_instance_id=message["workflow_instance_id"]
        @activity_instance_id=message["activity_instance_id"]
        @last_activity_instance_id=message["last_activity_instance_id"]
      end
    
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
factor-0.1.07 lib/runtime/message.rb
factor-0.1.06 lib/runtime/message.rb
factor-0.1.05 lib/runtime/message.rb
factor-0.1.02 lib/runtime/message.rb
factor-0.1.01 lib/runtime/message.rb
factor-0.1.00 lib/runtime/message.rb
factor-0.0.99 lib/runtime/message.rb
factor-0.0.98 lib/runtime/message.rb
factor-0.0.97 lib/runtime/message.rb
factor-0.0.96 lib/runtime/message.rb
factor-0.0.95 lib/runtime/message.rb
factor-0.0.94 lib/runtime/message.rb
factor-0.0.93 lib/runtime/message.rb
factor-0.0.92 lib/runtime/message.rb
factor-0.0.91 lib/runtime/message.rb
factor-0.0.90 lib/runtime/message.rb
factor-0.0.89 lib/runtime/message.rb
factor-0.0.88 lib/runtime/message.rb
factor-0.0.87 lib/runtime/message.rb
factor-0.0.86 lib/runtime/message.rb