Class: Pusher::Channel
- Inherits:
-
Object
- Object
- Pusher::Channel
- Defined in:
- lib/pusher/channel.rb
Overview
Trigger events on Channels
Instance Attribute Summary (collapse)
-
- (Object) name
readonly
Returns the value of attribute name.
Instance Method Summary (collapse)
-
- (Hash) authenticate(socket_id, custom_data = nil)
Generate the expected response for an authentication endpoint.
-
- (String) authentication_string(socket_id, custom_string = nil)
Compute authentication string required as part of the authentication endpoint response.
-
- (Hash) info(attributes = [])
Request info for a channel.
-
- (Channel) initialize(base_url, name, client = Pusher)
constructor
A new instance of Channel.
-
- (Object) trigger(event_name, data, socket_id = nil)
Trigger event, catching and logging any errors.
-
- (Object) trigger!(event_name, data, socket_id = nil)
Trigger event.
-
- (EM::DefaultDeferrable) trigger_async(event_name, data, socket_id = nil)
Trigger event asynchronously using EventMachine::HttpRequest.
Constructor Details
- (Channel) initialize(base_url, name, client = Pusher)
A new instance of Channel
9 10 11 12 13 14 |
# File 'lib/pusher/channel.rb', line 9 def initialize(base_url, name, client = Pusher) @uri = base_url.dup @uri.path = @uri.path + "/channels/#{name}/" @name = name @client = client end |
Instance Attribute Details
- (Object) name (readonly)
Returns the value of attribute name
7 8 9 |
# File 'lib/pusher/channel.rb', line 7 def name @name end |
Instance Method Details
- (Hash) authenticate(socket_id, custom_data = nil)
Generate the expected response for an authentication endpoint. See pusher.com/docs/authenticating_users for details.
139 140 141 142 143 144 145 |
# File 'lib/pusher/channel.rb', line 139 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 |
- (String) authentication_string(socket_id, custom_string = nil)
Compute authentication string required as part of the authentication endpoint response. Generally the authenticate method should be used in preference to this one
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pusher/channel.rb', line 98 def authentication_string(socket_id, custom_string = nil) if socket_id.nil? || socket_id.empty? raise Error, "Invalid socket_id #{socket_id}" end unless custom_string.nil? || custom_string.kind_of?(String) raise Error, 'Custom argument must be a string' end string_to_sign = [socket_id, name, custom_string]. compact.map(&:to_s).join(':') Pusher.logger.debug "Signing #{string_to_sign}" token = @client.authentication_token digest = OpenSSL::Digest::SHA256.new signature = OpenSSL::HMAC.hexdigest(digest, token.secret, string_to_sign) return "#{token.key}:#{signature}" end |
- (Hash) info(attributes = [])
Request info for a channel
84 85 86 |
# File 'lib/pusher/channel.rb', line 84 def info(attributes = []) @client.get("/channels/#{name}", :info => attributes.join(',')) end |
- (Object) trigger(event_name, data, socket_id = nil)
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
70 71 72 73 74 75 |
# File 'lib/pusher/channel.rb', line 70 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 |
- (Object) trigger!(event_name, data, socket_id = nil)
Trigger event
- Deprecated
-
This method will be removed in a future gem version. Please
switch to Pusher.trigger or Pusher::Client#trigger instead
56 57 58 59 60 |
# File 'lib/pusher/channel.rb', line 56 def trigger!(event_name, data, socket_id = nil) params = {} params[:socket_id] = socket_id if socket_id @client.trigger(name, event_name, data, params) end |
- (EM::DefaultDeferrable) trigger_async(event_name, data, socket_id = nil)
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
30 31 32 33 34 |
# File 'lib/pusher/channel.rb', line 30 def trigger_async(event_name, data, socket_id = nil) params = {} params[:socket_id] = socket_id if socket_id @client.trigger_async(name, event_name, data, params) end |