pubby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2011, Simon Russell
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted
5
+ provided that the following conditions are met:
6
+
7
+ Redistributions of source code must retain the above copyright notice, this list of conditions and
8
+ the following disclaimer.
9
+
10
+ Redistributions in binary form must reproduce the above copyright notice, this list of conditions
11
+ and the following disclaimer in the documentation and/or other materials provided with the
12
+ distribution.
13
+
14
+ Neither the name of Simon Russell nor the names of its contributors may be used to endorse or
15
+ promote products derived from this software without specific prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
24
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1 @@
1
+ Wrappers for various channel publishing APIs; trying to keep the API simple and consistent.
@@ -0,0 +1,22 @@
1
+ module Pubby
2
+
3
+ autoload :Stub, File.join(File.dirname(__FILE__), 'pubby/stub')
4
+ autoload :Pubnub, File.join(File.dirname(__FILE__), 'pubby/pubnub')
5
+ autoload :SimpleAsync, File.join(File.dirname(__FILE__), 'pubby/simple_async')
6
+
7
+ def self.from_config(config)
8
+ type = config.fetch('type') { raise "type is required" }
9
+
10
+ klass = case type
11
+ when 'stub'
12
+ Pubby::Stub
13
+ when 'pubnub'
14
+ Pubby::Pubnub
15
+ else
16
+ raise "unknown type #{type.inspect}"
17
+ end
18
+
19
+ klass.from_config(config)
20
+ end
21
+
22
+ end
@@ -0,0 +1,26 @@
1
+ require 'pubnub-ruby'
2
+
3
+ class Pubby::Pubnub
4
+
5
+ attr_reader :pubnub
6
+
7
+ def initialize(pubnub)
8
+ @pubnub = pubnub
9
+ end
10
+
11
+ def publish(channel_name, message)
12
+ @pubnub.publish('channel' => channel_name, 'message' => message)
13
+ end
14
+
15
+ def self.from_config(config)
16
+ new(
17
+ Pubnub.new(
18
+ config.fetch('publish_key') { raise "publish_key is required" },
19
+ config.fetch('subscribe_key', ''),
20
+ config.fetch('secret_key', ''),
21
+ config.fetch('ssl_on', false)
22
+ )
23
+ )
24
+ end
25
+
26
+ end
@@ -0,0 +1,52 @@
1
+ class Pubby::SimpleAsync
2
+
3
+ attr_reader :pubby
4
+ attr_reader :thread_count
5
+ attr_reader :queue
6
+ attr_reader :threads
7
+
8
+ # you probably want the pubby used here to be threadsafe if there's more than one thread...
9
+ def initialize(pubby, thread_count = 1)
10
+ @pubby = pubby
11
+ @thread_count = thread_count
12
+ @queue = Queue.new
13
+ @threads = []
14
+
15
+ start_threads!
16
+ end
17
+
18
+ def publish(channel, message)
19
+ @queue << [channel, message]
20
+ end
21
+
22
+ def shutdown!
23
+ until @queue.empty?
24
+ sleep 0.05
25
+ end
26
+
27
+ kill!
28
+ end
29
+
30
+ def kill!
31
+ @threads.each { |t| t.kill; t.join }
32
+ end
33
+
34
+ private
35
+
36
+ def start_threads!
37
+ @thread_count.times do
38
+ @threads << Thread.new do
39
+ Thread.abort_on_exception = true
40
+ process_messages!
41
+ end
42
+ end
43
+ end
44
+
45
+ def process_messages!
46
+ until @shutdown
47
+ args = @queue.pop
48
+ @pubby.publish(*args)
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,33 @@
1
+ class Pubby::Stub
2
+
3
+ def initialize(initial_messages = {})
4
+ @messages = Hash.new { |h, k| h[k] = [] }
5
+
6
+ initial_messages.each do |channel, messages|
7
+ messages.each do |message|
8
+ publish(channel, message)
9
+ end
10
+ end
11
+ end
12
+
13
+ def publish(channel_name, message)
14
+ @messages[channel_name] << message
15
+ end
16
+
17
+ def messages
18
+ @messages
19
+ end
20
+
21
+ def channels
22
+ @messages.keys
23
+ end
24
+
25
+ def empty?
26
+ @messages.empty? || @messages.all? { |k, v| v.empty? }
27
+ end
28
+
29
+ def self.from_config(config)
30
+ new
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pubby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Simon Russell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-07-14 00:00:00.000000000 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: ! ' Wrappers for various channel publishing APIs;
16
+ trying to keep the API simple and consistent.
17
+
18
+ '
19
+ email: spam+pubby@bellyphant.com
20
+ executables: []
21
+ extensions: []
22
+ extra_rdoc_files: []
23
+ files:
24
+ - lib/pubby.rb
25
+ - lib/pubby/pubnub.rb
26
+ - lib/pubby/stub.rb
27
+ - lib/pubby/simple_async.rb
28
+ - LICENSE
29
+ - README.md
30
+ has_rdoc: true
31
+ homepage: http://github.com/simonrussell/pubby-gem
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.9.2
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.6.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Wrappers for various channel publishing APIs
55
+ test_files: []