Sha256: 5aae9da41a7749b25785dca76fdcab1aa9e9aa12f557569675338ddb4012fb32

Contents?: true

Size: 1.3 KB

Versions: 13

Compression:

Stored size: 1.3 KB

Contents

module Operations
  # encapsulates our "messages store" server side
  # the params and dispatcher will be inherited
  class ServerBase < Hyperloop::ServerOp
    param :acting_user, nils: true
    param :user_name
    dispatch_to { Hyperloop::Application }

    def messages
      Rails.cache.fetch('messages') { [] }
    end

    def add_message
      params.message = {
        message: params.message,
        time: Time.now,
        from: params.user_name
      }
      Rails.cache.write('messages', messages << params.message)
    end
  end

  # get all the messages
  class GetMessages < ServerBase
    outbound :messages

    step { params.messages = messages }

    def self.deserialize_dispatch(messages)
      # convert the time string back to time
      messages[:messages].each do |message|
        message[:time] = Time.parse(message[:time])
      end
      messages
    end
  end

  # send a message to everybody
  class Send < ServerBase
    param :message

    step :add_message

    def self.deserialize_dispatch(message)
      # convert time strings back to time
      message[:message][:time] = Time.parse(message[:message][:time])
      message
    end
  end

  # client side only: registers user_name and then gets the messages
  class Join < Hyperloop::Operation
    param :user_name
    step GetMessages
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hyper-operation-0.5.12 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.11 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.10 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.9 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.8 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.7 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.6 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.5 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.4 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.3 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.2 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.1 examples/chat-app/app/hyperloop/operations/operations.rb
hyper-operation-0.5.0 examples/chat-app/app/hyperloop/operations/operations.rb