Sha256: aab6f439f0e93a4c46a7cad2cefc79771c3ecc3e323058c4b314d5510ad5a330

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

module Puggernaut
  class Server
    class Channel < EM::Channel
      
      include Logger
      
      attr_reader :channels
      
      def initialize(channels)
        @channels = channels
        super()
      end
      
      class <<self
        
        attr_accessor :channels
        
        def create(channels)
          channel = self.new(channels)
          @channels ||= []
          @channels << channel
          channel
        end
        
        def all_messages_after(channel, identifier)
          if @messages && @messages[channel]
            found = false
            (
              @messages[channel].select { |(id, message)|
                found = true if id == identifier
                found
              }[1..-1] || []

            ).collect { |message|
              "#{channel}|#{message.join '|'}"
            }
          else
            []
          end
        end
        
        def say(messages)
          @messages ||= {}
          messages = messages.inject({}) do |hash, (channel_name, messages)|
            messages = messages.collect do |message|
              [ rand.to_s[2..-1], message ]
            end
            @messages[channel_name] ||= []
            @messages[channel_name] += messages
            if @messages[channel_name].length > 100
              @messages[channel_name] = @messages[channel_name][-100..-1]
            end
            hash[channel_name] = messages
            hash
          end
          @channels.each do |channel|
            push = channel.channels.collect do |channel_name|
              if messages[channel_name]
                messages[channel_name].collect { |message|
                  "#{channel_name}|#{message.join('|')}"
                }.join("\n")
              end
            end
            push = push.compact
            unless push.empty?
              channel.push push.join("\n")
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
puggernaut-0.1.5 lib/puggernaut/server/channel.rb
puggernaut-0.1.4 lib/puggernaut/server/channel.rb
puggernaut-0.1.3 lib/puggernaut/server/channel.rb