Module: FayeClient

Defined in:
lib/faye-client/faye_client.rb,
lib/faye-client/channel_handlers/default_channel_handler.rb

Defined Under Namespace

Classes: DefaultChannelHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_channel_handler_class_nameObject

Returns the value of attribute default_channel_handler_class_name.



26
27
28
# File 'lib/faye-client/faye_client.rb', line 26

def default_channel_handler_class_name
  @default_channel_handler_class_name
end

#default_channel_handler_methodObject

Returns the value of attribute default_channel_handler_method.



26
27
28
# File 'lib/faye-client/faye_client.rb', line 26

def default_channel_handler_method
  @default_channel_handler_method
end

#messaging_channelsObject

Returns the value of attribute messaging_channels.



25
26
27
# File 'lib/faye-client/faye_client.rb', line 25

def messaging_channels
  @messaging_channels
end

#messaging_clientObject

Returns the value of attribute messaging_client.



25
26
27
# File 'lib/faye-client/faye_client.rb', line 25

def messaging_client
  @messaging_client
end

#messaging_client_threadObject

Returns the value of attribute messaging_client_thread.



25
26
27
# File 'lib/faye-client/faye_client.rb', line 25

def messaging_client_thread
  @messaging_client_thread
end

#messaging_server_urlObject

Returns the value of attribute messaging_server_url.



25
26
27
# File 'lib/faye-client/faye_client.rb', line 25

def messaging_server_url
  @messaging_server_url
end

Instance Method Details

#get_channel_handler(channel) ⇒ Object

Set the handler class/method to be used for a given channel



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/faye-client/faye_client.rb', line 81

def get_channel_handler(channel)
  if channel.is_a? String
    parsed_channel_name = channel.gsub(/^\//, '').gsub('/','::')
    handler = get_channel_handler_for_string(parsed_channel_name)
    handler[:name] = channel
  elsif channel.is_a? Hash
    # Can provide a Hash to get full customization of handler names/methods
    handler = get_channel_handler_for_hash(channel)
    handler[:name] = channel[:name]
  else
    raise TypeError, 'Channel Must be a String or a Hash'
  end

  handler
end

#get_channel_handler_class_name_for_string(channel) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/faye-client/faye_client.rb', line 129

def get_channel_handler_class_name_for_string(channel)
  # Try to use the channel name to determine the class to use
  class_name = "#{self.class}::#{channel.capitalize}Handler"
  rescue_counter = 0
  begin
  class_name = ActiveSupport::Inflector.constantize class_name if class_name.is_a? String
  rescue NameError
    # If class_name can't be constantized, try to use a default
    if self.default_channel_handler_class_name and rescue_counter == 0
      # Try to use defined default from class
      class_name = self.default_channel_handler_class_name
    else
      # Use gem default if defined default doesn't work
      class_name = "FayeClient::DefaultChannelHandler"
    end
    rescue_counter += 1
    retry if rescue_counter <= 1
    raise 'CannotLoadConstant' if rescue_counter > 1
  end
  return class_name
end

#get_channel_handler_for_hash(channel) ⇒ Object

Build channel handler pointers when hash is provided for channel



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/faye-client/faye_client.rb', line 109

def get_channel_handler_for_hash(channel)
  handler = {}
  if channel[:handler_class_name] 
    # if class name is provided, then you use it
    handler[:handler_class_name] = channel[:handler_class_name]
  else
    # Get default class ifnone is provided
    handler[:handler_class_name] = get_channel_handler_class_name_for_string(channel[:name])
  end

  if channel[:handler_method_name]
    # Get method to use if one is provided
    handler[:handler_method_name] = channel[:handler_method_name]
  else
    # Use default method if none is provided
    handler[:handler_method_name] = get_default_channel_handler_method_name
  end
  return handler
end

#get_channel_handler_for_string(channel) ⇒ Object

If just a string is provided for a channel



98
99
100
101
102
103
104
105
106
# File 'lib/faye-client/faye_client.rb', line 98

def get_channel_handler_for_string(channel)
  handler = {}
  # Set handler class
  handler[:handler_class_name] = get_channel_handler_class_name_for_string(channel)
  # Set handler method
  handler[:handler_method_name] = get_default_channel_handler_method_name
  
  handler
end

#get_default_channel_handler_method_nameObject



151
152
153
154
155
156
157
158
159
160
# File 'lib/faye-client/faye_client.rb', line 151

def get_default_channel_handler_method_name
  if self.default_channel_handler_method
    # Use defined default if available
    return self.default_channel_handler_method
  else
    # By default we are using Sidekiq Workers to handle incoming messages.
    # 'perform_async' comes from Sidekiq
    return 'perform_async'
  end
end

#publish(options) ⇒ Object

Publish a :message to a :channel



51
52
53
54
55
# File 'lib/faye-client/faye_client.rb', line 51

def publish(options)
  raise 'NoChannelProvided' unless options[:channel]
  raise 'NoMessageProvided' unless options[:message]
  messaging_client.publish(options[:channel], options[:message])
end

#restartObject

Restart the running client



65
66
67
68
# File 'lib/faye-client/faye_client.rb', line 65

def restart
  stop
  start
end

#running?Boolean

Is the client running?



72
73
74
75
76
77
78
# File 'lib/faye-client/faye_client.rb', line 72

def running?
  if self.messaging_client and self.messaging_client.state == :CONNECTED
    true
  else
    false
  end
end

#startObject

Start client Will need to restart client anytime you want to add more channels



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/faye-client/faye_client.rb', line 31

def start
  raise "AlreadyRunning" if running?
  self.messaging_client_thread = Thread.new do
    # Must be run inside EventMachine
    EventMachine.run {
      # Create the actual Faye client
      self.messaging_client = Faye::Client.new(messaging_server_url)
      self.messaging_channels.each do |channel|
        # Channel Handlers provide customization for how to handle a message
        channel_handler = self.get_channel_handler(channel)
        raise 'NoChannelNameProvided' if !channel_handler[:name]
        self.messaging_client.subscribe(channel_handler[:name]) do |message|
          channel_handler[:handler_class_name].send(channel_handler[:handler_method_name], message)
        end
      end
    }
  end
end

#stopObject

Stop the running client



58
59
60
61
62
# File 'lib/faye-client/faye_client.rb', line 58

def stop
  raise "NotRunning" if !running?
  self.messaging_client.disconnect
  self.messaging_client_thread.kill
end