Class: Puggernaut::Server::Channel

Inherits:
EM::Channel
  • Object
show all
Extended by:
Logger
Defined in:
lib/puggernaut/server/channel.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

logger, logger

Constructor Details

#initialize(channels, user_id) ⇒ Channel

Returns a new instance of Channel.



7
8
9
10
11
# File 'lib/puggernaut/server/channel.rb', line 7

def initialize(channels, user_id)
  @channels = channels
  @user_id = user_id
  super()
end

Class Attribute Details

.channelsObject

Returns the value of attribute channels.



17
18
19
# File 'lib/puggernaut/server/channel.rb', line 17

def channels
  @channels
end

Instance Attribute Details

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/puggernaut/server/channel.rb', line 5

def channels
  @channels
end

#user_idObject (readonly)

Returns the value of attribute user_id.



5
6
7
# File 'lib/puggernaut/server/channel.rb', line 5

def user_id
  @user_id
end

Class Method Details

.all_messages_after_id(channel, identifier) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/puggernaut/server/channel.rb', line 26

def all_messages_after_id(channel, identifier)
  if @messages && @messages[channel]
    found = false
    (
      @messages[channel].select { |(id, message, time)|
        found = true if id == identifier
        found
      }[1..-1] || []

    ).collect { |message|
      "#{channel}|#{message.join '|'}"
    }
  else
    []
  end
end

.all_messages_after_time(channel, after_time) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/puggernaut/server/channel.rb', line 43

def all_messages_after_time(channel, after_time)
  if @messages && @messages[channel]
    (
      @messages[channel].select { |(id, message, time)|
        after_time < time
      } || []

    ).collect { |message|
      "#{channel}|#{message.join '|'}"
    }
  else
    []
  end
end

.create(channels, user_id) ⇒ Object



19
20
21
22
23
24
# File 'lib/puggernaut/server/channel.rb', line 19

def create(channels, user_id)
  channel = self.new(channels, user_id)
  @channels ||= []
  @channels << channel
  channel
end

.inhabitants(channel_name) ⇒ Object



58
59
60
61
62
63
# File 'lib/puggernaut/server/channel.rb', line 58

def inhabitants(channel_name)
  user_ids = @channels.collect do |channel|
    channel.user_id if channel.channels.include?(channel_name)
  end
  user_ids.compact
end

.say(messages, exclude_user_id = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/puggernaut/server/channel.rb', line 65

def say(messages, exclude_user_id=nil)
  @messages ||= {}
  messages = messages.inject({}) do |hash, (channel_name, messages)|
    messages = messages.collect do |message|
      [ rand.to_s[2..-1], message, Time.now ]
    end
    @messages[channel_name] ||= []
    @messages[channel_name] += messages
    @messages[channel_name] = @messages[channel_name].select do |message|
      message[2] >= Time.now - 2 * 60 * 60
    end
    hash[channel_name] = messages
    hash
  end
  @channels.each do |channel|
    next if exclude_user_id && channel.user_id == exclude_user_id
    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