faye-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "http://rubygems.org"
2
+ gem 'faye'
3
+ gem 'eventmachine'
4
+ gem 'sidekiq'
5
+ gem 'active_support'
6
+
7
+ group :development do
8
+ gem "shoulda", ">= 0"
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.1.1"
11
+ gem "jeweler", "~> 1.8.3"
12
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,68 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ active_support (3.0.0)
5
+ activesupport (= 3.0.0)
6
+ activesupport (3.0.0)
7
+ addressable (2.3.2)
8
+ celluloid (0.11.1)
9
+ timers (>= 1.0.0)
10
+ connection_pool (0.9.2)
11
+ cookiejar (0.3.0)
12
+ em-http-request (0.3.0)
13
+ addressable (>= 2.0.0)
14
+ escape_utils
15
+ eventmachine (>= 0.12.9)
16
+ escape_utils (0.2.4)
17
+ eventmachine (0.12.10)
18
+ faye (0.8.3)
19
+ cookiejar (>= 0.3.0)
20
+ em-http-request (>= 0.3.0)
21
+ eventmachine (>= 0.12.0)
22
+ faye-websocket (>= 0.4.0)
23
+ rack (>= 1.0.0)
24
+ yajl-ruby (>= 1.0.0)
25
+ faye-websocket (0.4.6)
26
+ eventmachine (>= 0.12.0)
27
+ git (1.2.5)
28
+ jeweler (1.8.4)
29
+ bundler (~> 1.0)
30
+ git (>= 1.2.5)
31
+ rake
32
+ rdoc
33
+ json (1.7.5)
34
+ multi_json (1.3.6)
35
+ rack (1.4.1)
36
+ rake (0.9.2.2)
37
+ rdoc (3.12)
38
+ json (~> 1.4)
39
+ redis (3.0.1)
40
+ redis-namespace (1.2.1)
41
+ redis (~> 3.0.0)
42
+ shoulda (3.1.1)
43
+ shoulda-context (~> 1.0)
44
+ shoulda-matchers (~> 1.2)
45
+ shoulda-context (1.0.0)
46
+ shoulda-matchers (1.3.0)
47
+ activesupport (>= 3.0.0)
48
+ sidekiq (2.2.1)
49
+ celluloid (~> 0.11.1)
50
+ connection_pool (~> 0.9.2)
51
+ multi_json (~> 1)
52
+ redis (~> 3)
53
+ redis-namespace
54
+ timers (1.0.1)
55
+ yajl-ruby (1.1.0)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ active_support
62
+ bundler (~> 1.1.1)
63
+ eventmachine
64
+ faye
65
+ jeweler (~> 1.8.3)
66
+ rdoc (~> 3.12)
67
+ shoulda
68
+ sidekiq
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Brian Goff
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,43 @@
1
+ = faye-client
2
+
3
+ This is a wrapper for Faye::Client
4
+ It handles managing the client and spinning it off to another thread, so you can easily run this in any ruby app without blocking the main thread.
5
+
6
+ To use extend your class with FayeClient.
7
+ You will need to set self.messaging_server_url, and self.messaging_channels where the url is the url of your Faye Server, channels are the channels you want to subscribe to, e.g. ['/example']
8
+
9
+ Channels are expected to have a Handler for processing the message.
10
+ We are expecting a SidekiqWorker class here by default.
11
+ It will search for a class with MyClass::ChannelNameHandler where MyClass is the class that you extended for FayeClient, and ChannelName is the name of the channel, capitalied, without the begingin "/" and '::' for any other slash.
12
+
13
+ So a channel called '/example' should have a handler at MyClass::ExampleHandler
14
+ This class is by default expected to be a Sidekiq::Worker (Check out the Sidekiq project @ https://github.com/mperham/sidekiq ).
15
+ This class should have a method called "perform", which you would be passing the received message to.
16
+ Look at the Sidekiq docs for more details.
17
+
18
+ If MyClass::ExampleHandler is not found it will look for defaults from your class:
19
+ self.default_channel_handler_class_name
20
+ self.default_channel_handler_method
21
+
22
+ You can also further set this on a per channel bases where instead of passing in ['/message'] as your list of channels, you would do something like:
23
+
24
+ [{name: '/message', handler_class_name: 'MyExampleClassName', handler_class_method: 'my_method'}]
25
+
26
+ Keep in mind your channel handlers need to return quickly so that FayeClient can keep handling incoming messages. This is why by default this is getting done asyncronously with Sidekiq.
27
+
28
+
29
+ == Contributing to faye-client
30
+
31
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
32
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
33
+ * Fork the project.
34
+ * Start a feature/bugfix branch.
35
+ * Commit and push until you are happy with your contribution.
36
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
37
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2012 Brian Goff. See LICENSE.txt for
42
+ further details.
43
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "faye-client"
18
+ gem.homepage = "http://github.com/cpuguy83/faye-client"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Quickly and easily connect your app to a Faye server}
21
+ gem.description = %Q{Quickly and easily connect your app to a Faye server}
22
+ gem.email = "cpuguy83@gmail.com"
23
+ gem.authors = ["Brian Goff"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ #require 'rcov/rcovtask'
36
+ #Rcov::RcovTask.new do |test|
37
+ # test.libs << 'test'
38
+ # test.pattern = 'test/**/test_*.rb'
39
+ # test.verbose = true
40
+ # test.rcov_opts << '--exclude "gems/*"'
41
+ #end
42
+
43
+ #task :default => :test
44
+
45
+ require 'rdoc/task'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "faye-client #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'eventmachine'
3
+ require 'Faye'
4
+ require 'sidekiq'
5
+ require 'active_support'
6
+ require './faye_client/faye_client'
7
+ require './faye_client/channel_handlers/default_channel_handler'
@@ -0,0 +1,10 @@
1
+ module FayeClient
2
+ class DefaultChannelHandler
3
+ include Sidekiq::Worker
4
+ sidekiq_options :queue => :messaging
5
+
6
+ def perform(message)
7
+ message
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,122 @@
1
+ module FayeClient
2
+ attr_accessor :messaging_client, :messaging_client_thread, :messaging_server_url, :messaging_channels
3
+ attr_accessor :default_channel_handler_class_name, :default_channel_handler_method
4
+
5
+
6
+ # Start client
7
+ # Will need to restart client anytime you want to add more channels
8
+ def start
9
+ raise "AlreadyRunning" if running?
10
+ self.messaging_client_thread = Thread.new do
11
+ # Must be run inside EventMachine
12
+ EventMachine.run {
13
+ # Create the actual Faye client
14
+ self.messaging_client = Faye::Client.new(messaging_server_url)
15
+ self.messaging_channels.each do |channel|
16
+ # Channel Handlers provide customization for how to handle a message
17
+ channel_handler = self.get_channel_handler(channel)
18
+ raise 'NoChannelNameProvided' if !channel_handler[:name]
19
+ self.messaging_client.subscribe(channel_handler[:name]) do |message|
20
+ channel_handler[:handler_class_name].send(channel_handler[:handler_method_name], message)
21
+ end
22
+ end
23
+ }
24
+ end
25
+ end
26
+
27
+ def stop
28
+ raise "NotRunning" if !running?
29
+ EM.stop
30
+ end
31
+
32
+ def restart
33
+ stop
34
+ start
35
+ end
36
+
37
+
38
+ # Is the client running?
39
+ def running?
40
+ EM.reactor_running?
41
+ end
42
+
43
+ def get_channel_handler(channel)
44
+ if channel.is_a? String
45
+ parsed_channel_name = channel.gsub(/^\//, '').gsub('/','::')
46
+ handler = get_channel_handler_for_string(parsed_channel_name)
47
+ handler[:name] = channel
48
+ elsif channel.is_a? Hash
49
+ # Can provide a Hash to get full customization of handler names/methods
50
+ handler = get_channel_handler_for_hash
51
+ handler[:name] = channel[:name]
52
+ else
53
+ raise TypeError, 'Channel Must be a String or a Hash'
54
+ end
55
+
56
+ handler
57
+ end
58
+
59
+ def get_channel_handler_for_string(channel)
60
+ handler = {}
61
+ # Set handler class
62
+ handler[:handler_class_name] = get_channel_handler_class_name_for_string(channel)
63
+ # Set handler method
64
+ handler[:handler_method_name] = get_default_channel_handler_method_name
65
+
66
+ handler
67
+ end
68
+
69
+ # Build channel handler pointers when hash is provided for channel
70
+ def get_channel_handler_for_hash(channel)
71
+ handler = {}
72
+ if channel[:handler_class_name]
73
+ # if class name is provided, then you use it
74
+ handler[:handler_class_name] = channel[:handler_class_name]
75
+ else
76
+ # Get default class ifnone is provided
77
+ handler[:handler_class_name] = get_channel_handler_class_name_for_string(channel[:name])
78
+ end
79
+
80
+ if channel[:handler_method_name]
81
+ # Get method to use if one is provided
82
+ handler[:handler_method_name] = channel[:handler_method_name]
83
+ else
84
+ # Use default method if none is provided
85
+ handler[:handler_method_name] = get_default_channel_handler_method_name
86
+ end
87
+ return handler
88
+ end
89
+
90
+ def get_channel_handler_class_name_for_string(channel)
91
+ # Try to use the channel name to determine the class to use
92
+ class_name = "#{self.class}::#{channel.capitalize}Handler"
93
+ rescue_counter = 0
94
+ begin
95
+ class_name = ActiveSupport::Inflector.constantize class_name if class_name.is_a? String
96
+ rescue NameError
97
+ # If class_name can't be constantized, try to use a default
98
+ if self.default_channel_handler_class_name and rescue_counter == 0
99
+ # Try to use defined default from class
100
+ class_name = self.default_channel_handler_class_name
101
+ else
102
+ # Use gem default if defined default doesn't work
103
+ class_name = "FayeClient::DefaultChannelHandler"
104
+ end
105
+ rescue_counter += 1
106
+ retry if rescue_counter <= 1
107
+ raise 'CannotLoadConstant' if rescue_counter > 1
108
+ end
109
+ return class_name
110
+ end
111
+
112
+ def get_default_channel_handler_method_name
113
+ if self.default_channel_handler_method
114
+ # Use defined default if available
115
+ return self.default_channel_handler_method
116
+ else
117
+ # By default we are using Sidekiq Workers to handle incoming messages.
118
+ # 'perform_async' comes from Sidekiq
119
+ return 'perform_async'
120
+ end
121
+ end
122
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'faye-client'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestFayeClient < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,190 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faye-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Goff
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faye
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: eventmachine
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sidekiq
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: active_support
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: shoulda
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rdoc
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '3.12'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '3.12'
110
+ - !ruby/object:Gem::Dependency
111
+ name: bundler
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.1.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.1.1
126
+ - !ruby/object:Gem::Dependency
127
+ name: jeweler
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ~>
132
+ - !ruby/object:Gem::Version
133
+ version: 1.8.3
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ version: 1.8.3
142
+ description: Quickly and easily connect your app to a Faye server
143
+ email: cpuguy83@gmail.com
144
+ executables: []
145
+ extensions: []
146
+ extra_rdoc_files:
147
+ - LICENSE.txt
148
+ - README.rdoc
149
+ files:
150
+ - .document
151
+ - Gemfile
152
+ - Gemfile.lock
153
+ - LICENSE.txt
154
+ - README.rdoc
155
+ - Rakefile
156
+ - VERSION
157
+ - lib/faye-client.rb
158
+ - lib/faye_client/channel_handlers/default_channel_handler.rb
159
+ - lib/faye_client/faye_client.rb
160
+ - test/helper.rb
161
+ - test/test_faye-client.rb
162
+ homepage: http://github.com/cpuguy83/faye-client
163
+ licenses:
164
+ - MIT
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ segments:
176
+ - 0
177
+ hash: -1052917701805139625
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ! '>='
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ requirements: []
185
+ rubyforge_project:
186
+ rubygems_version: 1.8.24
187
+ signing_key:
188
+ specification_version: 3
189
+ summary: Quickly and easily connect your app to a Faye server
190
+ test_files: []