Class: JsChat::User

Inherits:
Object
  • Object
show all
Includes:
FloodProtection
Defined in:
lib/jschat/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FloodProtection

#detect_flooding, #flooding?, #remove_old_activity_logs, #seen!

Constructor Details

#initialize(connection) ⇒ User



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jschat/server.rb', line 51

def initialize(connection)
  @name = nil
  @connection = connection
  @rooms = []
  @last_activity = Time.now.utc
  @last_poll = Time.now.utc
  @identified = false
  @ip = ''
  @expires = nil
  @session_length = nil
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



48
49
50
# File 'lib/jschat/server.rb', line 48

def connection
  @connection
end

#identifiedObject

Returns the value of attribute identified.



48
49
50
# File 'lib/jschat/server.rb', line 48

def identified
  @identified
end

#ipObject

Returns the value of attribute ip.



48
49
50
# File 'lib/jschat/server.rb', line 48

def ip
  @ip
end

#last_activityObject

Returns the value of attribute last_activity.



48
49
50
# File 'lib/jschat/server.rb', line 48

def last_activity
  @last_activity
end

#last_pollObject

Returns the value of attribute last_poll.



48
49
50
# File 'lib/jschat/server.rb', line 48

def last_poll
  @last_poll
end

#nameObject

Returns the value of attribute name.



48
49
50
# File 'lib/jschat/server.rb', line 48

def name
  @name
end

#roomsObject

Returns the value of attribute rooms.



48
49
50
# File 'lib/jschat/server.rb', line 48

def rooms
  @rooms
end

#session_lengthObject

Returns the value of attribute session_length.



48
49
50
# File 'lib/jschat/server.rb', line 48

def session_length
  @session_length
end

Class Method Details

.valid_name?(name) ⇒ Boolean



88
89
90
# File 'lib/jschat/server.rb', line 88

def self.valid_name?(name)
  not name.match /[^[:alnum:]._\-\[\]^C]/ and name.size > 0
end

Instance Method Details

#change(params) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jschat/server.rb', line 97

def change(params)
  # Valid options for change
  ['name'].each do |field|
    if params[field]
      old_value = send(field)
      send "#{field}=", params[field]
      @rooms.each do |room|
        response = { 'change' => 'user',
                     'room' => room.name,
                     'user' => { field => { old_value => params[field] } } }
        room.change_notice self, response
        return [field, params[field]]
      end
    end
  end
end

#private_message(message) ⇒ Object



92
93
94
95
# File 'lib/jschat/server.rb', line 92

def private_message(message)
  response = { 'display' => 'message', 'message' => message }
  @connection.send_response response
end

#session_expired?Boolean



63
64
65
66
# File 'lib/jschat/server.rb', line 63

def session_expired?
  return true if @expires.nil?
  Time.now.utc >= @expires
end

#to_jsonObject



73
74
75
# File 'lib/jschat/server.rb', line 73

def to_json
  { 'name' => @name, 'last_activity' => @last_activity }.to_json
end

#update_session_expirationObject



68
69
70
71
# File 'lib/jschat/server.rb', line 68

def update_session_expiration
  return if @session_length.nil?
  @expires = Time.now.utc + @session_length
end