Class: JsChat::Protocol
- Inherits:
-
Object
- Object
- JsChat::Protocol
- Defined in:
- lib/jschat/client.rb
Defined Under Namespace
Classes: Response
Instance Method Summary collapse
- #change_user(json) ⇒ Object
- #error(json) ⇒ Object
- #get_command(json) ⇒ Object
- #get_time(json) ⇒ Object
- #identified(json) ⇒ Object
- #identified? ⇒ Boolean
-
#initialize(connection) ⇒ Protocol
constructor
A new instance of Protocol.
- #join(json) ⇒ Object
- #join_notice(json) ⇒ Object
- #legal?(command) ⇒ Boolean
- #legal_change?(command) ⇒ Boolean
- #legal_change_commands ⇒ Object
- #legal_commands ⇒ Object
- #message(json) ⇒ Object
- #messages(messages) ⇒ Object
- #names(json) ⇒ Object
- #part(json) ⇒ Object
- #part_notice(json) ⇒ Object
- #process_message(json) ⇒ Object
- #protocol_method_name(command, method_name) ⇒ Object
- #quit(json) ⇒ Object (also: #quit_notice)
Constructor Details
#initialize(connection) ⇒ Protocol
Returns a new instance of Protocol.
59 60 61 |
# File 'lib/jschat/client.rb', line 59 def initialize(connection) @connection = connection end |
Instance Method Details
#change_user(json) ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/jschat/client.rb', line 81 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
184 185 186 |
# File 'lib/jschat/client.rb', line 184 def error(json) "* [ERROR] #{json['message']}" end |
#get_command(json) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/jschat/client.rb', line 105 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
113 114 115 116 117 118 119 |
# File 'lib/jschat/client.rb', line 113 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
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/jschat/client.rb', line 173 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
79 |
# File 'lib/jschat/client.rb', line 79 def identified? ; @identified ; end |
#join(json) ⇒ Object
144 145 146 147 |
# File 'lib/jschat/client.rb', line 144 def join(json) @connection.send_names json['room'] "* User #{json['user']} joined #{json['room']}" end |
#join_notice(json) ⇒ Object
149 150 151 152 |
# File 'lib/jschat/client.rb', line 149 def join_notice(json) @connection.names << json['user'] "* User #{json['user']} joined #{json['room']}" end |
#legal?(command) ⇒ Boolean
71 72 73 |
# File 'lib/jschat/client.rb', line 71 def legal?(command) legal_commands.include? command end |
#legal_change?(command) ⇒ Boolean
75 76 77 |
# File 'lib/jschat/client.rb', line 75 def legal_change?(command) legal_change_commands.include? command end |
#legal_change_commands ⇒ Object
67 68 69 |
# File 'lib/jschat/client.rb', line 67 def legal_change_commands %w(user) end |
#legal_commands ⇒ Object
63 64 65 |
# File 'lib/jschat/client.rb', line 63 def legal_commands %w(messages message joined quit error join names part identified part_notice quit_notice join_notice) end |
#message(json) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/jschat/client.rb', line 89 def (json) if json['room'] "[#{json['room']}] <#{json['user']}> #{json['message']}" else "PRIVATE <#{json['user']}> #{json['message']}" end end |
#messages(messages) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/jschat/client.rb', line 97 def () results = [] .each do |json| results << (json) end results end |
#names(json) ⇒ Object
168 169 170 171 |
# File 'lib/jschat/client.rb', line 168 def names(json) @connection.names = json.collect { |u| u['name'] } "* In this room: #{@connection.names.join(', ')}" end |
#part(json) ⇒ Object
154 155 156 |
# File 'lib/jschat/client.rb', line 154 def part(json) "* You left #{json['room']}" end |
#part_notice(json) ⇒ Object
158 159 160 161 |
# File 'lib/jschat/client.rb', line 158 def part_notice(json) @connection.names.delete json['user'] "* #{json['user']} left #{json['room']}" end |
#process_message(json) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jschat/client.rb', line 129 def (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
121 122 123 124 125 126 127 |
# File 'lib/jschat/client.rb', line 121 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
163 164 165 166 |
# File 'lib/jschat/client.rb', line 163 def quit(json) @connection.names.delete json['user'] "* User #{json['user']} left #{json['room']}" end |