Class: Sockudo::Channel
- Inherits:
-
Object
- Object
- Sockudo::Channel
- Includes:
- Utils
- Defined in:
- lib/sockudo/channel.rb
Overview
Delegates operations for a specific channel from a client
Constant Summary collapse
- INVALID_CHANNEL_REGEX =
/[^A-Za-z0-9_\-=@,.;:]/.freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#append_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message append.
-
#authenticate(socket_id, custom_data = nil) ⇒ Hash
Generate the expected response for an authentication endpoint.
-
#authentication_string(socket_id, custom_string = nil) ⇒ String
Compute authentication string required as part of the authentication endpoint response.
-
#delete_annotation(message_serial, annotation_serial, params = {}) ⇒ Object
Delete an annotation from a versioned message.
-
#delete_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message delete.
-
#get_message(message_serial) ⇒ Object
Fetch the latest visible version of a mutable message.
-
#get_message_versions(message_serial, params = {}) ⇒ Object
Fetch preserved versions of a mutable message.
-
#history(params = {}) ⇒ Hash
Request durable history for this channel.
-
#info(attributes = []) ⇒ Hash
Request info for a channel.
-
#initialize(_, name, client = Sockudo) ⇒ Channel
constructor
A new instance of Channel.
-
#list_annotations(message_serial, params = {}) ⇒ Object
List raw annotation events for a versioned message.
-
#presence_history(params = {}) ⇒ Hash
Request presence history for a presence channel.
-
#presence_snapshot(params = {}) ⇒ Hash
Request a reconstructed presence snapshot for a presence channel.
-
#publish_annotation(message_serial, params = {}) ⇒ Object
Publish an annotation for a versioned message.
- #shared_secret(encryption_master_key) ⇒ Object
-
#trigger(event_name, data, socket_id = nil) ⇒ Object
Trigger event, catching and logging any errors.
-
#trigger!(event_name, data, socket_id = nil) ⇒ Object
Trigger event.
-
#trigger_async(event_name, data, socket_id = nil) ⇒ EM::DefaultDeferrable
Trigger event asynchronously using EventMachine::HttpRequest.
-
#update_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message update.
-
#users(params = {}) ⇒ Hash
Request users for a presence channel Only works on presence channels (see: sockudo.com/docs/client_api_guide/client_presence_channels and sockudo.com/docs/rest_api).
Methods included from Utils
#_authentication_string, #validate_socket_id
Constructor Details
#initialize(_, name, client = Sockudo) ⇒ Channel
Returns a new instance of Channel.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/sockudo/channel.rb', line 13 def initialize(_, name, client = Sockudo) if Sockudo::Channel::INVALID_CHANNEL_REGEX.match(name) raise Sockudo::Error, "Illegal channel name '#{name}'" elsif name.length > 200 raise Sockudo::Error, "Channel name too long (limit 164 characters) '#{name}'" end @name = name @client = client end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/sockudo/channel.rb', line 9 def name @name end |
Instance Method Details
#append_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message append.
156 157 158 |
# File 'lib/sockudo/channel.rb', line 156 def (, params = {}) @client.(name, , params) end |
#authenticate(socket_id, custom_data = nil) ⇒ Hash
Generate the expected response for an authentication endpoint. See sockudo.com/docs/authenticating_users for details.
232 233 234 235 236 237 238 |
# File 'lib/sockudo/channel.rb', line 232 def authenticate(socket_id, custom_data = nil) custom_data = MultiJson.encode(custom_data) if custom_data auth = authentication_string(socket_id, custom_data) r = { auth: auth } r[:channel_data] = custom_data if custom_data r end |
#authentication_string(socket_id, custom_string = nil) ⇒ String
Compute authentication string required as part of the authentication endpoint response. Generally the authenticate method should be used in preference to this one
202 203 204 205 206 |
# File 'lib/sockudo/channel.rb', line 202 def authentication_string(socket_id, custom_string = nil) string_to_sign = [socket_id, name, custom_string].compact.join(':') _authentication_string(socket_id, string_to_sign, @client.authentication_token, custom_string) end |
#delete_annotation(message_serial, annotation_serial, params = {}) ⇒ Object
Delete an annotation from a versioned message.
166 167 168 |
# File 'lib/sockudo/channel.rb', line 166 def delete_annotation(, annotation_serial, params = {}) @client.delete_annotation(name, , annotation_serial, params) end |
#delete_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message delete.
151 152 153 |
# File 'lib/sockudo/channel.rb', line 151 def (, params = {}) @client.(name, , params) end |
#get_message(message_serial) ⇒ Object
Fetch the latest visible version of a mutable message.
136 137 138 |
# File 'lib/sockudo/channel.rb', line 136 def () @client.(name, ) end |
#get_message_versions(message_serial, params = {}) ⇒ Object
Fetch preserved versions of a mutable message.
141 142 143 |
# File 'lib/sockudo/channel.rb', line 141 def (, params = {}) @client.(name, , params) end |
#history(params = {}) ⇒ Hash
Request durable history for this channel.
111 112 113 |
# File 'lib/sockudo/channel.rb', line 111 def history(params = {}) @client.channel_history(name, params) end |
#info(attributes = []) ⇒ Hash
Request info for a channel
101 102 103 |
# File 'lib/sockudo/channel.rb', line 101 def info(attributes = []) @client.channel_info(name, info: attributes.join(',')) end |
#list_annotations(message_serial, params = {}) ⇒ Object
List raw annotation events for a versioned message.
171 172 173 |
# File 'lib/sockudo/channel.rb', line 171 def list_annotations(, params = {}) @client.list_annotations(name, , params) end |
#presence_history(params = {}) ⇒ Hash
Request presence history for a presence channel.
121 122 123 |
# File 'lib/sockudo/channel.rb', line 121 def presence_history(params = {}) @client.channel_presence_history(name, params) end |
#presence_snapshot(params = {}) ⇒ Hash
Request a reconstructed presence snapshot for a presence channel.
131 132 133 |
# File 'lib/sockudo/channel.rb', line 131 def presence_snapshot(params = {}) @client.channel_presence_snapshot(name, params) end |
#publish_annotation(message_serial, params = {}) ⇒ Object
Publish an annotation for a versioned message.
161 162 163 |
# File 'lib/sockudo/channel.rb', line 161 def publish_annotation(, params = {}) @client.publish_annotation(name, , params) end |
#shared_secret(encryption_master_key) ⇒ Object
240 241 242 243 244 245 246 247 |
# File 'lib/sockudo/channel.rb', line 240 def shared_secret(encryption_master_key) return unless encryption_master_key secret_string = @name + encryption_master_key digest = OpenSSL::Digest.new('SHA256') digest << secret_string digest.digest end |
#trigger(event_name, data, socket_id = nil) ⇒ Object
CAUTION! No exceptions will be raised on failure
Trigger event, catching and logging any errors.
- Deprecated
-
This method will be removed in a future gem version. Please
switch to Sockudo.trigger or Sockudo::Client#trigger instead
84 85 86 87 88 89 |
# File 'lib/sockudo/channel.rb', line 84 def trigger(event_name, data, socket_id = nil) trigger!(event_name, data, socket_id) rescue Sockudo::Error => e Sockudo.logger.error("#{e.} (#{e.class})") Sockudo.logger.debug(e.backtrace.join("\n")) end |
#trigger!(event_name, data, socket_id = nil) ⇒ Object
Trigger event
- Deprecated
-
This method will be removed in a future gem version. Please
switch to Sockudo.trigger or Sockudo::Client#trigger instead
67 68 69 70 71 72 73 74 |
# File 'lib/sockudo/channel.rb', line 67 def trigger!(event_name, data, socket_id = nil) params = {} if socket_id validate_socket_id(socket_id) params[:socket_id] = socket_id end @client.trigger(name, event_name, data, params) end |
#trigger_async(event_name, data, socket_id = nil) ⇒ EM::DefaultDeferrable
Trigger event asynchronously using EventMachine::HttpRequest
- Deprecated
-
This method will be removed in a future gem version. Please
switch to Sockudo.trigger_async or Sockudo::Client#trigger_async instead
38 39 40 41 42 43 44 45 |
# File 'lib/sockudo/channel.rb', line 38 def trigger_async(event_name, data, socket_id = nil) params = {} if socket_id validate_socket_id(socket_id) params[:socket_id] = socket_id end @client.trigger_async(name, event_name, data, params) end |
#update_message(message_serial, params = {}) ⇒ Object
Apply a mutable-message update.
146 147 148 |
# File 'lib/sockudo/channel.rb', line 146 def (, params = {}) @client.(name, , params) end |
#users(params = {}) ⇒ Hash
Request users for a presence channel Only works on presence channels (see: sockudo.com/docs/client_api_guide/client_presence_channels and sockudo.com/docs/rest_api)
186 187 188 |
# File 'lib/sockudo/channel.rb', line 186 def users(params = {}) @client.channel_users(name, params)[:users] end |