Class: RSence::Message

Inherits:
Object
  • Object
show all
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.

Examples:

Logs a message in the client javascript console.

msg.console "#{Time.new.to_s} -- Testing.."

Executes a custom Javascript command in the client.

msg.reply "alert('Hello!');"

Invalidates the session.

msg.expire_session

Creating a new HValue. Usage like this in any extended Plugin or GUIPlugin class. It’s recommended to use the values.yaml file to create any initial values.

def create_example_value( msg )
  ses = get_ses(msg)
  default_data = "Some text"
  if ses.has_key?( :example_value )
    # Resets the default data, causes it to update in the client too on the next sync.
    ses[:example_value].set( msg, default_data )
  else
    # Creates a new value with default data
    ses[:example_value] = HValue.new( msg, default_data )
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cloned_sourcefalse, Hash

Contains the source ses on the first request after the active session was cloned from another session.

Returns:

  • (false, Hash)


58
59
60
# File 'lib/rsence/msg.rb', line 58

def cloned_source
  @cloned_source
end

#cloned_targetsfalse, Array<Hash>

Contains the target sessions packed in an Array on the first request after another session was cloned from the active session.

Returns:

  • (false, Array<Hash>)


62
63
64
# File 'lib/rsence/msg.rb', line 62

def cloned_targets
  @cloned_targets
end

#new_sessionBoolean

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.

Returns:

  • (Boolean)

    This flag is true on the first request of a newly created session.



49
50
51
# File 'lib/rsence/msg.rb', line 49

def new_session
  @new_session
end

#pluginsPluginManager

Reference to the main PluginManager

Returns:



98
99
100
# File 'lib/rsence/msg.rb', line 98

def plugins
  @plugins
end

#requestRequest

The HTTP Request object of the active request.

Returns:



69
70
71
# File 'lib/rsence/msg.rb', line 69

def request
  @request
end

#responseResponse

The HTTP Response object of the active request.

Returns:



73
74
75
# File 'lib/rsence/msg.rb', line 73

def response
  @response
end

#restored_sessionBoolean

Flag for a restored session’s first request. Check it in your code to decide what to do, when a restored session is encountered.

Returns:

  • (Boolean)

    This flag is true on the first request of a newly restored session.



54
55
56
# File 'lib/rsence/msg.rb', line 54

def restored_session
  @restored_session
end

#sessionsSessionManager

Reference to SessionManager

Returns:



94
95
96
# File 'lib/rsence/msg.rb', line 94

def sessions
  @sessions
end

#transporterTransporter

Reference to Transporter

Returns:



86
87
88
# File 'lib/rsence/msg.rb', line 86

def transporter
  @transporter
end

#valuemanagerValueManager

Reference to ValueManager

Returns:

  • (ValueManager)


90
91
92
# File 'lib/rsence/msg.rb', line 90

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.

Parameters:

  • plugin_name (Symbol)

    The plugin to call

  • plugin_method (Symbol)

    The method of the plugin to call

  • *args

    Any arguments to pass to the ‘plugin_method`



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.

Parameters:

  • data (#to_json)

    Any data that can be presented in the Javascript console.



312
313
314
# File 'lib/rsence/msg.rb', line 312

def console(data)
  reply( "console.log(#{data.to_json});" )
end

#expire_sessionnil

Invalidates the active session.

Returns:

  • (nil)


161
162
163
# File 'lib/rsence/msg.rb', line 161

def expire_session
  @sessions.expire_session( @session[:ses_id] ) if @session
end

#inspectObject



374
375
376
# File 'lib/rsence/msg.rb', line 374

def inspect
  "#<Message ses_id:#{ses_id.inspect}, ses_key: #{ses_key.inspect} ...>"
end

#release_rsrc(uri) ⇒ Object Also known as: unserve_rsrc

Deprecated.

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.

Parameters:

  • uri (String)

    The URL to delete; the return value of #serve_rsrc

See Also:

  • RSence::Message.{{#serve_rsrc}


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

Parameters:

  • data (String<js>, #to_json)

    Javascript source or object that responds to #to_json

  • dont_squeeze (Boolean) (defaults to: false)

    When true, doesn’t ‘squeeze` the contents (jsmin + jscompress)



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

Deprecated.

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.

Parameters:

  • file_data (String)

    The binary data to serve

  • content_type (String) (defaults to: 'text/plain')

    The MIME type to serve the data as

  • filename (String) (defaults to: 'untitled.txt')

    The name of the downloadable file.

Returns:

  • (String)

    The URL where the downloadable file can be accessed from using a GET request.



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

Deprecated.

Serves an image object and returns its disposable URL. Calls the default ‘ticket` plugin. Use ticket.serve_img directly instead.

Parameters:

  • img_obj (#to_blob)

    RMagick image object.

  • img_format (String) (defaults to: 'PNG')

    The format img_obj#to_blob is encoded as.

Returns:

  • (String)

    The URL where the image can be accessed from using a GET request.



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

Deprecated.

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.

Parameters:

  • rsrc_data (String)

    The binary data of the resource to serve.

  • content_type (String) (defaults to: 'text/plain')

    The MIME type to serve the data as

Returns:

  • (String)

    The URL where the resource can be accessed from using a GET request.

See Also:

  • RSence::Message.{{#release_rsrc}


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_idNumber Also known as: session_id

Returns the session id

Returns:

  • (Number)


215
216
217
# File 'lib/rsence/msg.rb', line 215

def ses_id
  @session[:ses_id]
end

#user_idNumber, String

Getter for the user id

Returns:

  • (Number, String)

    The current user id. Returns 0 by default.



178
179
180
# File 'lib/rsence/msg.rb', line 178

def user_id
  @session[:user_id]
end

#user_id=(user_id) ⇒ nil

Setter for the user id

Parameters:

  • user_id (Number, String)

    The user id to set. Use in login situations to store the user id.

Returns:

  • (nil)


202
203
204
# File 'lib/rsence/msg.rb', line 202

def user_id=(user_id)
  @session[:user_id] = user_id
end

#user_infoHash

Getter for the user info hash

Returns:

  • (Hash)

    The current user info hash. Returns {} by default.



184
185
186
187
# File 'lib/rsence/msg.rb', line 184

def 
  @session[:user_info] = {} unless @session.has_key?(:user_info)
  @session[:user_info]
end

#user_info=(user_info) ⇒ nil

Setter for the user info

Parameters:

  • user_info (Hash)

    The user info hash to set. Use in login situations to store the user information.

Returns:

  • (nil)


209
210
211
# File 'lib/rsence/msg.rb', line 209

def user_info=()
  @session[:user_info] = 
end