Class: Pusher::Channel
Overview
Delegates operations for a specific channel from a client
Constant Summary collapse
- INVALID_CHANNEL_REGEX =
/[^A-Za-z0-9_\-=@,.;]/
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#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.
-
#info(attributes = []) ⇒ Hash
Request info for a channel.
-
#initialize(_, name, client = Pusher) ⇒ Channel
constructor
A new instance of Channel.
- #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.
-
#users(params = {}) ⇒ Hash
Request users for a presence channel Only works on presence channels (see: pusher.com/docs/client_api_guide/client_presence_channels and pusher.com/docs/rest_api).
Methods included from Utils
#_authentication_string, #validate_socket_id
Constructor Details
#initialize(_, name, client = Pusher) ⇒ Channel
Returns a new instance of Channel.
10 11 12 13 14 15 16 17 18 |
# File 'lib/pusher/channel.rb', line 10 def initialize(_, name, client = Pusher) if Pusher::Channel::INVALID_CHANNEL_REGEX.match(name) raise Pusher::Error, "Illegal channel name '#{name}'" elsif name.length > 200 raise Pusher::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.
7 8 9 |
# File 'lib/pusher/channel.rb', line 7 def name @name end |
Instance Method Details
#authenticate(socket_id, custom_data = nil) ⇒ Hash
Generate the expected response for an authentication endpoint. See pusher.com/docs/authenticating_users for details.
158 159 160 161 162 163 164 |
# File 'lib/pusher/channel.rb', line 158 def authenticate(socket_id, custom_data = nil) custom_data = MultiJson.dump(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
128 129 130 131 132 |
# File 'lib/pusher/channel.rb', line 128 def authentication_string(socket_id, custom_string = nil) string_to_sign = [socket_id, name, custom_string].compact.map(&:to_s).join(':') _authentication_string(socket_id, string_to_sign, @client.authentication_token, custom_string) end |
#info(attributes = []) ⇒ Hash
Request info for a channel
97 98 99 |
# File 'lib/pusher/channel.rb', line 97 def info(attributes = []) @client.channel_info(name, :info => attributes.join(',')) end |
#shared_secret(encryption_master_key) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/pusher/channel.rb', line 166 def shared_secret(encryption_master_key) return unless encryption_master_key secret_string = @name + encryption_master_key digest = OpenSSL::Digest::SHA256.new 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 Pusher.trigger or Pusher::Client#trigger instead
80 81 82 83 84 85 |
# File 'lib/pusher/channel.rb', line 80 def trigger(event_name, data, socket_id = nil) trigger!(event_name, data, socket_id) rescue Pusher::Error => e Pusher.logger.error("#{e.} (#{e.class})") Pusher.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 Pusher.trigger or Pusher::Client#trigger instead
63 64 65 66 67 68 69 70 |
# File 'lib/pusher/channel.rb', line 63 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 Pusher.trigger_async or Pusher::Client#trigger_async instead
34 35 36 37 38 39 40 41 |
# File 'lib/pusher/channel.rb', line 34 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 |
#users(params = {}) ⇒ Hash
Request users for a presence channel Only works on presence channels (see: pusher.com/docs/client_api_guide/client_presence_channels and pusher.com/docs/rest_api)
112 113 114 |
# File 'lib/pusher/channel.rb', line 112 def users(params = {}) @client.channel_users(name, params)[:users] end |