Class: WebsocketRails::Event

Inherits:
Object
  • Object
show all
Extended by:
Logging, StaticEvents
Includes:
Logging
Defined in:
lib/websocket_rails/event.rb

Overview

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.

Direct Known Subclasses

SpecHelperEvent

Defined Under Namespace

Classes: UnknownDataType

Constant Summary

Constants included from Logging

Logging::ANSI, Logging::LOGGABLE_DATA

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StaticEvents

new_on_close, new_on_error, new_on_invalid_event_received, new_on_open, new_on_ping

Methods included from Logging

color_for_level, colorize, configure, log, log_data?, log_event, log_event?, log_event_end, log_event_start, log_exception, log_header, log_level, wrap

Constructor Details

#initialize(event_name, options = {}) ⇒ Event

Returns a new instance of Event



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def channel
  @channel
end

#connectionObject (readonly)

Returns the value of attribute connection



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def connection
  @connection
end

#dataObject

Returns the value of attribute data



103
104
105
# File 'lib/websocket_rails/event.rb', line 103

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def namespace
  @namespace
end

#propagateObject

Returns the value of attribute propagate



103
104
105
# File 'lib/websocket_rails/event.rb', line 103

def propagate
  @propagate
end

#resultObject

Returns the value of attribute result



103
104
105
# File 'lib/websocket_rails/event.rb', line 103

def result
  @result
end

#server_tokenObject

Returns the value of attribute server_token



103
104
105
# File 'lib/websocket_rails/event.rb', line 103

def server_token
  @server_token
end

#successObject

Returns the value of attribute success



103
104
105
# File 'lib/websocket_rails/event.rb', line 103

def success
  @success
end

#tokenObject (readonly)

Returns the value of attribute token



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def token
  @token
end

#user_idObject (readonly)

Returns the value of attribute user_id



101
102
103
# File 'lib/websocket_rails/event.rb', line 101

def user_id
  @user_id
end

Class Method Details

.log_headerObject



66
67
68
# File 'lib/websocket_rails/event.rb', line 66

def self.log_header
  "Event"
end

.new_from_json(encoded_data, connection) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

Instance Method Details

#as_jsonObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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_nameObject



169
170
171
172
173
174
175
176
177
178
# 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?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/websocket_rails/event.rb', line 145

def is_channel?
  !@channel.nil?
end

#is_internal?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/websocket_rails/event.rb', line 157

def is_internal?
  namespace.include?(:websocket_rails)
end

#is_invalid?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/websocket_rails/event.rb', line 153

def is_invalid?
  name == :invalid_event
end

#is_user?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/websocket_rails/event.rb', line 149

def is_user?
  !@user_id.nil? && !is_channel?
end

#serializeObject



141
142
143
# File 'lib/websocket_rails/event.rb', line 141

def serialize
  as_json.to_json
end

#should_propagate?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/websocket_rails/event.rb', line 161

def should_propagate?
  @propagate
end

#triggerObject



165
166
167
# File 'lib/websocket_rails/event.rb', line 165

def trigger
  connection.trigger self if connection
end