Class: RSence::Message
- Inherits:
-
Object
- Object
- RSence::Message
- Defined in:
- lib/rsence/msg.rb
Overview
A Message instance is used as a ‘messenger class` while processing client-server requests. It’s initialized by the system and the convention guideline for its instance is msg, pass it on wherever msg might be needed. In most cases, the use of msg is to just pass the same msg onward from a method to another.
Instance Attribute Summary collapse
-
#cloned_source ⇒ false, Hash
Contains the source ses on the first request after the active session was cloned from another session.
-
#cloned_targets ⇒ false, Array<Hash>
Contains the target sessions packed in an Array on the first request after another session was cloned from the active session.
-
#new_session ⇒ Boolean
Flag for a new session’s first request.
-
#plugins ⇒ PluginManager
Reference to the main PluginManager.
-
#request ⇒ Request
The HTTP Request object of the active request.
-
#response ⇒ Response
The HTTP Response object of the active request.
-
#restored_session ⇒ Boolean
Flag for a restored session’s first request.
-
#sessions ⇒ SessionManager
Reference to SessionManager.
-
#transporter ⇒ Transporter
Reference to Transporter.
-
#valuemanager ⇒ ValueManager
Reference to ValueManager.
Instance Method Summary collapse
-
#call(plugin_name, plugin_method, *args) ⇒ Object
(also: #run)
Calls a method of a registered plugin with optional arguments.
-
#coffee(data) ⇒ Object
Same as #reply, but with CoffeeScript instead of JS.
-
#console(data) ⇒ Object
(also: #puts)
Sends data to the client’s javascript console.
-
#expire_session ⇒ nil
Invalidates the active session.
- #inspect ⇒ Object
-
#lang ⇒ Hash
Getter for the user language.
-
#lang=(lang) ⇒ nil
Setter for the user language.
- #release_rsrc(uri) ⇒ Object (also: #unserve_rsrc) deprecated Deprecated.
-
#reply(data, dont_squeeze = false) ⇒ Object
Sends data to the client, usually javascript, but is valid for any data that responds to #to_json.
- #serve_file(file_data, content_type = 'text/plain', filename = 'untitled.txt') ⇒ String deprecated Deprecated.
- #serve_img(img_obj, img_format = 'PNG') ⇒ String deprecated Deprecated.
- #serve_rsrc(rsrc_data, content_type = 'text/plain') ⇒ String deprecated Deprecated.
-
#ses_id ⇒ Number
(also: #session_id)
Returns the session id.
-
#user_id ⇒ Number, String
Getter for the user id.
-
#user_id=(user_id) ⇒ nil
Setter for the user id.
-
#user_info ⇒ Hash
Getter for the user info hash.
-
#user_info=(user_info) ⇒ nil
Setter for the user info.
Instance Attribute Details
#cloned_source ⇒ false, Hash
Contains the source ses on the first request after the active session was cloned from another session.
49 50 51 |
# File 'lib/rsence/msg.rb', line 49 def cloned_source @cloned_source end |
#cloned_targets ⇒ false, Array<Hash>
Contains the target sessions packed in an Array on the first request after another session was cloned from the active session.
53 54 55 |
# File 'lib/rsence/msg.rb', line 53 def cloned_targets @cloned_targets end |
#new_session ⇒ Boolean
Flag for a new session’s first request. Check it in your code to decide what to do, when a new session is encountered. In systems that require authentication, this may be used as a trigger to display a login/register dialog.
40 41 42 |
# File 'lib/rsence/msg.rb', line 40 def new_session @new_session end |
#plugins ⇒ PluginManager
Reference to the main PluginManager
89 90 91 |
# File 'lib/rsence/msg.rb', line 89 def plugins @plugins end |
#request ⇒ Request
The HTTP Request object of the active request.
60 61 62 |
# File 'lib/rsence/msg.rb', line 60 def request @request end |
#response ⇒ Response
The HTTP Response object of the active request.
64 65 66 |
# File 'lib/rsence/msg.rb', line 64 def response @response end |
#restored_session ⇒ Boolean
Flag for a restored session’s first request. Check it in your code to decide what to do, when a restored session is encountered.
45 46 47 |
# File 'lib/rsence/msg.rb', line 45 def restored_session @restored_session end |
#sessions ⇒ SessionManager
Reference to SessionManager
85 86 87 |
# File 'lib/rsence/msg.rb', line 85 def sessions @sessions end |
#transporter ⇒ Transporter
Reference to Transporter
77 78 79 |
# File 'lib/rsence/msg.rb', line 77 def transporter @transporter end |
#valuemanager ⇒ ValueManager
Reference to ValueManager
81 82 83 |
# File 'lib/rsence/msg.rb', line 81 def valuemanager @valuemanager end |
Instance Method Details
#call(plugin_name, plugin_method, *args) ⇒ Object Also known as: run
Calls a method of a registered plugin with optional arguments.
369 370 371 |
# File 'lib/rsence/msg.rb', line 369 def call( plugin_name, plugin_method, *args ) @plugins.call( plugin_name, plugin_method, *args) end |
#coffee(data) ⇒ Object
Same as #reply, but with CoffeeScript instead of JS
292 293 294 295 |
# File 'lib/rsence/msg.rb', line 292 def coffee(data) puts data if @config[:trace] @buffer.push( @plugins[:client_pkg].coffee( data ) ) end |
#console(data) ⇒ Object Also known as: puts
Sends data to the client’s javascript console.
312 313 314 |
# File 'lib/rsence/msg.rb', line 312 def console(data) reply( "console.log(#{data.to_json});" ) end |
#expire_session ⇒ nil
Invalidates the active session.
146 147 148 |
# File 'lib/rsence/msg.rb', line 146 def expire_session @sessions.expire_session( @session[:ses_id] ) if @session end |
#inspect ⇒ Object
374 375 376 |
# File 'lib/rsence/msg.rb', line 374 def inspect "#<Message ses_id:#{ses_id.inspect}, ses_key: #{ses_key.inspect} ...>" end |
#lang ⇒ Hash
Getter for the user language
176 177 178 179 180 |
# File 'lib/rsence/msg.rb', line 176 def lang uinfo = user_info uinfo[:lang] = RSence.config[:lang] unless uinfo.has_key? :lang uinfo[:lang] end |
#lang=(lang) ⇒ nil
Setter for the user language
209 210 211 |
# File 'lib/rsence/msg.rb', line 209 def lang=(lang) @session[:user_info][:lang] = lang end |
#release_rsrc(uri) ⇒ Object Also known as: unserve_rsrc
Removes the URL served, you must call this manually when after a served resource is no longer needed! Calls the default ‘ticket` plugin. Use ticket.serve_rsrc directly instead.
360 361 362 |
# File 'lib/rsence/msg.rb', line 360 def release_rsrc( uri ) call(:ticket,:del_rsrc, uri[3..-1] ) end |
#reply(data, dont_squeeze = false) ⇒ Object
Sends data to the client, usually javascript, but is valid for any data that responds to #to_json
284 285 286 287 288 289 |
# File 'lib/rsence/msg.rb', line 284 def reply(data,dont_squeeze=false) data.strip! data = @plugins[:client_pkg].squeeze( data ) unless dont_squeeze puts data if @config[:trace] @buffer.push( data ) end |
#serve_file(file_data, content_type = 'text/plain', filename = 'untitled.txt') ⇒ String
Binary data to be served once as a downloadable file attachment, returns a disposable URL. Calls the default ‘ticket` plugin. Use ticket.serve_file directly instead.
336 337 338 |
# File 'lib/rsence/msg.rb', line 336 def serve_file( file_data, content_type='text/plain', filename='untitled.txt' ) call( :ticket, :serve_file, self, file_data, content_type, filename ) end |
#serve_img(img_obj, img_format = 'PNG') ⇒ String
Serves an image object and returns its disposable URL. Calls the default ‘ticket` plugin. Use ticket.serve_img directly instead.
324 325 326 |
# File 'lib/rsence/msg.rb', line 324 def serve_img( img_obj, img_format='PNG' ) call( :ticket, :serve_img, self, img_obj, img_format ) end |
#serve_rsrc(rsrc_data, content_type = 'text/plain') ⇒ String
Sends any binary to be served indefinitely, returns a unique, random, static URL. Calls the default ‘ticket` plugin. IMPORTANT: PLEASE call #release_rsrc manually, when the resource is no longer needed to free the memory occupied! HINT: In most cases, it’s a better idea to use serve_img or serve_file to expire the resource automatically. Use ticket.serve_rsrc directly instead.
350 351 352 |
# File 'lib/rsence/msg.rb', line 350 def serve_rsrc( rsrc_data, content_type='text/plain' ) call(:ticket,:serve_rsrc,self, rsrc_data, content_type ) end |
#ses_id ⇒ Number Also known as: session_id
Returns the session id
215 216 217 |
# File 'lib/rsence/msg.rb', line 215 def ses_id @session[:ses_id] end |
#user_id ⇒ Number, String
Getter for the user id
163 164 165 |
# File 'lib/rsence/msg.rb', line 163 def user_id @session[:user_id] end |
#user_id=(user_id) ⇒ nil
Setter for the user id
195 196 197 |
# File 'lib/rsence/msg.rb', line 195 def user_id=(user_id) @session[:user_id] = user_id end |
#user_info ⇒ Hash
Getter for the user info hash
169 170 171 172 |
# File 'lib/rsence/msg.rb', line 169 def user_info @session[:user_info] = {} unless @session.has_key?(:user_info) @session[:user_info] end |
#user_info=(user_info) ⇒ nil
Setter for the user info
202 203 204 |
# File 'lib/rsence/msg.rb', line 202 def user_info=(user_info) @session[:user_info] = user_info end |