Class: JsChat::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/jschat/client.rb

Defined Under Namespace

Classes: Response

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Protocol

Returns a new instance of Protocol.



60
61
62
# File 'lib/jschat/client.rb', line 60

def initialize(connection)
  @connection = connection
end

Instance Method Details

#change_user(json) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/jschat/client.rb', line 82

def change_user(json)
  original_name = json['name'].keys().first
  new_name = json['name'].values.first
  @connection.names.delete original_name
  @connection.names << new_name
  "* User #{original_name} is now known as #{new_name}"
end

#error(json) ⇒ Object



185
186
187
# File 'lib/jschat/client.rb', line 185

def error(json)
  "* [ERROR] #{json['message']}"
end

#get_command(json) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/jschat/client.rb', line 106

def get_command(json)
  if json.has_key? 'display' and legal? json['display']
    'display'
  elsif json.has_key? 'change' and legal_change? json['change']
    'change'
  end
end

#get_time(json) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/jschat/client.rb', line 114

def get_time(json)
  if json.kind_of? Hash and json.has_key? 'time'
    Time.parse(json['time']).getlocal
  else
    Time.now.localtime
  end
end

#identified(json) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/jschat/client.rb', line 174

def identified(json)
  if ClientConfig[:auto_join] and !@auto_joined
    @connection.send_join ClientConfig[:auto_join]
    @auto_joined = true
  end

  @identified = true

  "* You are now known as #{json['name']}"
end

#identified?Boolean

Returns:

  • (Boolean)


80
# File 'lib/jschat/client.rb', line 80

def identified? ; @identified ; end

#join(json) ⇒ Object



145
146
147
148
# File 'lib/jschat/client.rb', line 145

def join(json)
  @connection.send_names json['room']
  "* User #{json['user']} joined #{json['room']}"
end

#join_notice(json) ⇒ Object



150
151
152
153
# File 'lib/jschat/client.rb', line 150

def join_notice(json)
  @connection.names << json['user']
  "* User #{json['user']} joined #{json['room']}"
end

#legal?(command) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/jschat/client.rb', line 72

def legal?(command)
  legal_commands.include? command
end

Returns:

  • (Boolean)


76
77
78
# File 'lib/jschat/client.rb', line 76

def legal_change?(command)
  legal_change_commands.include? command
end


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

def legal_change_commands
  %w(user)
end


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

def legal_commands
  %w(messages message joined quit error join names part identified part_notice quit_notice join_notice)
end

#message(json) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/jschat/client.rb', line 90

def message(json)
  if json['room']
    "[#{json['room']}] <#{json['user']}> #{json['message']}"
  else
    "PRIVATE <#{json['user']}> #{json['message']}"
  end
end

#messages(messages) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/jschat/client.rb', line 98

def messages(messages)
  results = []
  messages.each do |json|
    results << process_message(json)
  end
  results
end

#names(json) ⇒ Object



169
170
171
172
# File 'lib/jschat/client.rb', line 169

def names(json)
  @connection.names = json.collect { |u| u['name'] }
  "* In this room: #{@connection.names.join(', ')}"
end

#part(json) ⇒ Object



155
156
157
# File 'lib/jschat/client.rb', line 155

def part(json)
  "* You left #{json['room']}"
end

#part_notice(json) ⇒ Object



159
160
161
162
# File 'lib/jschat/client.rb', line 159

def part_notice(json)
  @connection.names.delete json['user']
  "* #{json['user']} left #{json['room']}"
end

#process_message(json) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jschat/client.rb', line 130

def process_message(json)
  command = get_command json

  if command
    time = get_time json
    response = send(protocol_method_name(command, json[command]), json[json[command]])
    case response
    when Array
      response
    when String
      Response.new(response, time)
    end
  end
end

#protocol_method_name(command, method_name) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/jschat/client.rb', line 122

def protocol_method_name(command, method_name)
  if command == 'display'
    method_name
  elsif command == 'change'
    "change_#{method_name}"
  end
end

#quit(json) ⇒ Object Also known as: quit_notice



164
165
166
167
# File 'lib/jschat/client.rb', line 164

def quit(json)
  @connection.names.delete json['user']
  "* User #{json['user']} left #{json['room']}"
end