Permalink
Browse files

Clean up and improve documentation.

  • Loading branch information...
1 parent 7e666f5 commit 82341d22e359f85270c760fa24d925ca7e58b9d1 @tristandunn committed Jan 18, 2013
@@ -2,10 +2,10 @@ module PusherFake
module Channel
class << self
# Name matcher for private channels.
- PRIVATE_CHANNEL_MATCHER = /^private-/.freeze
+ PRIVATE_CHANNEL_MATCHER = /\Aprivate-/.freeze
# Name matcher for presence channels.
- PRESENCE_CHANNEL_MATCHER = /^presence-/.freeze
+ PRESENCE_CHANNEL_MATCHER = /\Apresence-/.freeze
# @return [Hash] Cache of existing channels.
attr_accessor :channels
@@ -48,9 +48,10 @@ def reset
# @param [String] name The name of the channel.
# @return [Class] The class to use for the channel.
def class_for(name)
- if name =~ PRIVATE_CHANNEL_MATCHER
+ case name
+ when PRIVATE_CHANNEL_MATCHER
Private
- elsif name =~ PRESENCE_CHANNEL_MATCHER
+ when PRESENCE_CHANNEL_MATCHER
Presence
else
Public
@@ -15,6 +15,8 @@ def initialize(name)
# Remove the +connection+ from the channel and notify the channel.
#
+ # Also trigger the member_removed webhook.
+ #
# @param [Connection] connection The connection to remove.
def remove(connection)
super
@@ -39,9 +41,11 @@ def subscription_data
private
- # Store the member data for the connection and notify connections a
+ # Store the member data for the connection and notify the channel a
# member was added.
#
+ # Also trigger the member_added webhook.
+ #
# @param [Connection] connection The connection a subscription succeeded for.
# @param [Hash] options The options for the channel.
def subscription_succeeded(connection, options = {})
@@ -45,6 +45,8 @@ def includes?(connection)
# Remove the +connection+ from the channel.
#
+ # If it is the last connection, trigger the channel_vacated webhook.
+ #
# @param [Connection] connection The connection to remove.
def remove(connection)
connections.delete(connection)
@@ -64,9 +66,11 @@ def subscription_data
private
- # Notify the channel of the successful subscription and add the
+ # Notify the +connection+ of the successful subscription and add the
# connection to the channel.
#
+ # If it is the first connection, trigger the channel_occupied webhook.
+ #
# @param [Connection] connection The connection a subscription succeeded for.
# @param [Hash] options The options for the channel.
def subscription_succeeded(connection, options = {})
@@ -1,5 +1,8 @@
module PusherFake
class Connection
+ # Name matcher for client events.
+ CLIENT_EVENT_MATCHER = /\Aclient-(.+)\Z/.freeze
+
# @return [EventMachine::WebSocket::Connection] The socket object for this connection.
attr_reader :socket
@@ -18,9 +21,8 @@ def initialize(socket)
def emit(event, data = {}, channel = nil)
message = { event: event, data: data }
message[:channel] = channel if channel
- message = MultiJson.dump(message)
- socket.send(message)
+ socket.send(MultiJson.dump(message))
end
# Notify the Pusher client that a connection has been established.
@@ -43,7 +45,7 @@ def process(data)
channel.add(self, data)
when "pusher:unsubscribe"
channel.remove(self)
- when /^client-(.+)$/
+ when CLIENT_EVENT_MATCHER
return unless channel.is_a?(Channel::Private)
return unless channel.includes?(self)

0 comments on commit 82341d2

Please sign in to comment.