Class: Pusher::Channel
- Inherits:
-
Object
- Object
- Pusher::Channel
- Defined in:
- lib/pusher/channel.rb
Overview
Trigger events on Channels
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 an authentication endpoint response.
-
#authentication_string(socket_id, custom_string = nil) ⇒ String
(also: #socket_auth)
Compute authentication string required to subscribe to this channel.
-
#initialize(base_url, name, client = Pusher) ⇒ Channel
constructor
A new instance of Channel.
-
#stats ⇒ Hash
Request channel stats.
-
#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, &block) ⇒ EM::DefaultDeferrable
Trigger event asynchronously using EventMachine::HttpRequest.
Constructor Details
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 an authentication endpoint response
124 125 126 127 128 129 130 |
# File 'lib/pusher/channel.rb', line 124 def authenticate(socket_id, custom_data = nil) custom_data = MultiJson.encode(custom_data) if custom_data auth = socket_auth(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 Also known as: socket_auth
Compute authentication string required to subscribe to this channel.
See pusher.com/docs/auth_signatures for more details.
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pusher/channel.rb', line 87 def authentication_string(socket_id, custom_string = nil) raise "Invalid socket_id" if socket_id.nil? || socket_id.empty? raise 'Custom argument must be a string' unless custom_string.nil? || custom_string.kind_of?(String) string_to_sign = [socket_id, name, custom_string].compact.map{|e|e.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 |
#stats ⇒ Hash
Request channel stats
72 73 74 75 |
# File 'lib/pusher/channel.rb', line 72 def stats request = Pusher::Request.new(:get, @uri + 'stats', {}, nil, nil, @client) return request.send_sync end |
#trigger(event_name, data, socket_id = nil) ⇒ Object
Note:
CAUTION! No exceptions will be raised on failure
Trigger event, catching and logging any errors.
59 60 61 62 63 64 |
# File 'lib/pusher/channel.rb', line 59 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
49 50 51 52 |
# File 'lib/pusher/channel.rb', line 49 def trigger!(event_name, data, socket_id = nil) request = construct_event_request(event_name, data, socket_id) request.send_sync end |
#trigger_async(event_name, data, socket_id = nil, &block) ⇒ EM::DefaultDeferrable
Trigger event asynchronously using EventMachine::HttpRequest
27 28 29 30 |
# File 'lib/pusher/channel.rb', line 27 def trigger_async(event_name, data, socket_id = nil, &block) request = construct_event_request(event_name, data, socket_id) request.send_async end |