class WebsocketRails::Event

Contains all of the relevant information for incoming and outgoing events. All events except for channel events will have a connection object associated.

Events require an event name and hash of options:

:data => The data object will be passed to any callback functions bound on the client side.

You can also pass a Hash of options to specify:

:connection => Connection that will be receiving or that sent this event.

:namespace => The namespace this event is under. Will default to :global If the namespace is nested under multiple levels pass them as an array. For instance, if the namespace route looks like the following:

namespace :products do
  namespace :hats do
    # events
  end
end

Then you would pass the namespace argument as [:products,:hats]

:channel => The name of the channel that this event is destined for.

Attributes

channel[R]
connection[R]
data[RW]
id[R]
name[R]
namespace[R]
propagate[RW]
result[RW]
server_token[RW]
success[RW]
token[R]
user_id[R]

Public Class Methods

log_header() click to toggle source
# File lib/websocket_rails/event.rb, line 66
def self.log_header
  "Event"
end
new(event_name, options={}) click to toggle source
# File lib/websocket_rails/event.rb, line 105
def initialize(event_name, options={})
  case event_name
  when String
    namespace   = event_name.split('.')
    @name       = namespace.pop.to_sym
  when Symbol
    @name       = event_name
    namespace   = [:global]
  end
  @id           = options[:id]
  @data         = options[:data].is_a?(Hash) ? options[:data].with_indifferent_access : options[:data]
  @channel      = options[:channel].to_sym rescue options[:channel].to_s.to_sym if options[:channel]
  @token        = options[:token] if options[:token]
  @connection   = options[:connection]
  @server_token = options[:server_token]
  @user_id      = options[:user_id]
  @propagate    = options[:propagate].nil? ? true : options[:propagate]
  @namespace    = validate_namespace( options[:namespace] || namespace )
end
new_from_json(encoded_data, connection) click to toggle source
# File lib/websocket_rails/event.rb, line 70
def self.new_from_json(encoded_data, connection)
  case encoded_data
  when String
    event_name, data = JSON.parse encoded_data

    unless event_name.is_a?(String) && data.is_a?(Hash)
      raise UnknownDataType
    end

    data = data.merge(:connection => connection).with_indifferent_access
    Event.new event_name, data
    # when Array
    # TODO: Handle file
    #File.open("/tmp/test#{rand(100)}.jpg", "wb") do |file|
    #  encoded_data.each do |byte|
    #    file << byte.chr
    #  end
    #end
  else
    raise UnknownDataType
  end
rescue JSON::ParserError, UnknownDataType => ex
  warn "Invalid Event Received: #{ex}"
  debug "Event Data: #{encoded_data}"
  log_exception(ex)
  Event.new_on_invalid_event_received(connection, nil)
end

Public Instance Methods

as_json() click to toggle source
# File lib/websocket_rails/event.rb, line 125
def as_json
  [
    encoded_name,
    {
      :id => id,
      :channel => channel,
      :user_id => user_id,
      :data => data,
      :success => success,
      :result => result,
      :token => token,
      :server_token => server_token
    }
  ]
end
encoded_name() click to toggle source
# File lib/websocket_rails/event.rb, line 169
def encoded_name
  if namespace.size > 1
    child_namespace = namespace.dup[1..-1]
    child_namespace << name
    combined_name = child_namespace.join('.')
  else
    combined_name = name
  end
  combined_name
end
is_channel?() click to toggle source
# File lib/websocket_rails/event.rb, line 145
def is_channel?
  !@channel.nil?
end
is_internal?() click to toggle source
# File lib/websocket_rails/event.rb, line 157
def is_internal?
  namespace.include?(:websocket_rails)
end
is_invalid?() click to toggle source
# File lib/websocket_rails/event.rb, line 153
def is_invalid?
  name == :invalid_event
end
is_user?() click to toggle source
# File lib/websocket_rails/event.rb, line 149
def is_user?
  !@user_id.nil? && !is_channel?
end
serialize() click to toggle source
# File lib/websocket_rails/event.rb, line 141
def serialize
  as_json.to_json
end
should_propagate?() click to toggle source
# File lib/websocket_rails/event.rb, line 161
def should_propagate?
  @propagate
end
trigger() click to toggle source
# File lib/websocket_rails/event.rb, line 165
def trigger
  connection.trigger self if connection
end

Private Instance Methods

validate_namespace(namespace) click to toggle source
# File lib/websocket_rails/event.rb, line 182
def validate_namespace(namespace)
  namespace = [namespace] unless namespace.is_a?(Array)
  namespace.unshift :global unless namespace.first == :global
  namespace.map(&:to_sym) rescue [:global]
end