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

Returns a new instance of User.



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

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.



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

def connection
  @connection
end

#identifiedObject

Returns the value of attribute identified.



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

def identified
  @identified
end

#ipObject

Returns the value of attribute ip.



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

def ip
  @ip
end

#last_activityObject

Returns the value of attribute last_activity.



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

def last_activity
  @last_activity
end

#last_pollObject

Returns the value of attribute last_poll.



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

def last_poll
  @last_poll
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#roomsObject

Returns the value of attribute rooms.



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

def rooms
  @rooms
end

#session_lengthObject

Returns the value of attribute session_length.



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

def session_length
  @session_length
end

Class Method Details

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#change(params) ⇒ Object



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

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



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

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

#session_expired?Boolean

Returns:

  • (Boolean)


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

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

#to_json(*a) ⇒ Object



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

def to_json(*a)
  { 'name' => @name, 'last_activity' => @last_activity }.to_json(*a)
end

#update_session_expirationObject



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

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