redis_message_capsule 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redis_message_capsule.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Arbind
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # RedisMessageCapsule
2
+
3
+ Send and receive real-time messages between applications (via redis).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'redis_message_capsule'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install redis_message_capsule
18
+
19
+ ## Usage
20
+
21
+ Open terminal window one (to send messages):
22
+
23
+ $ irb
24
+ require 'redis_message_capsule'
25
+ channel_cat = RedisMessageCapsule.channel('cat')
26
+ channel_cat.send('meow')
27
+ channel_cat.send('meow')
28
+
29
+ Open terminal window two (to listen for messages):
30
+
31
+ $ irb
32
+ require 'redis_message_capsule'
33
+ RedisMessageCapsule.listen('cat') do |msg|
34
+ puts msg
35
+ end
36
+ # => meow
37
+
38
+ Go back to terminal window one:
39
+
40
+ channel_cat.send 9
41
+ channel_cat.send say: 'roar', time: Time.now
42
+ channel_cat.send :purr
43
+
44
+ Watch for messages in terminal window two:
45
+
46
+ # => 9
47
+ # => {"say"=>"roar", "time"=>"2012-11-19 23:16:08 -0800"}
48
+ # => purr
49
+
50
+ (Make sure you have redis running)
51
+
52
+ ## Comming Soon
53
+
54
+ A node.js version you can use to send messages back and forth between node and rails apps.
55
+
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,3 @@
1
+ module RedisMessageCapsule
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,146 @@
1
+ require "redis_message_capsule/version"
2
+ require 'redis'
3
+ require 'json'
4
+ require 'uri'
5
+
6
+ module RedisMessageCapsule
7
+ class Configuration
8
+ attr_accessor :environment, :db_number, :redis_url
9
+
10
+ def initialize
11
+ self.environment = ENV["RACK_ENV"] || "development"
12
+ self.db_number = 7 if self.environment.eql? 'production'
13
+ self.db_number = 8 if self.environment.eql? 'development'
14
+ self.db_number = 9 if self.environment.eql? 'test'
15
+ self.db_number ||= 9
16
+ self.redis_url = ENV["REDIS_URL"] || ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
17
+ end
18
+ end
19
+
20
+ class << self
21
+ attr_accessor :configuration, :redis_clients, :capsule_channels, :listener_threads, :handlers
22
+ end
23
+
24
+ def self.redis_clients
25
+ @redis_clients ||= {}
26
+ end
27
+
28
+ def self.capsule_channels
29
+ @capsule_channels ||= {}
30
+ end
31
+
32
+ def self.listener_threads
33
+ @listener_threads ||= {}
34
+ end
35
+
36
+ def self.configuration
37
+ @configuration ||= Configuration.new
38
+ end
39
+ def self.config() configuration end
40
+
41
+ def self.configure
42
+ yield(configuration) if block_given?
43
+ end
44
+
45
+ class Channel
46
+ attr_accessor :name, :redis_client
47
+ def initialize(name, redis_client)
48
+ self.name = name
49
+ self.redis_client = redis_client
50
+ end
51
+
52
+ def send (message)
53
+ payload = { 'data' => message }
54
+ redis_client.rpush name, payload.to_json
55
+ end
56
+
57
+ end
58
+
59
+ def self.make_client_key(url, db_num)
60
+ "#{url}.#{db_num}"
61
+ end
62
+
63
+ def self.make_channel_key(name, url, db_num)
64
+ "#{name}.#{url}.#{db_num}"
65
+ end
66
+
67
+ def self.make_listener_key(channels, url, db_num)
68
+ [ *channels, url, db_num].join('.')
69
+ end
70
+
71
+ def self.channel(name, redis_url=nil, db_number=-1)
72
+ url = redis_url || config.redis_url
73
+ db_num = db_number
74
+ db_num = config.db_number if db_num < 0
75
+
76
+ channel_key = make_channel_key(name, url, db_num)
77
+ return capsule_channels[channel_key] unless capsule_channels[channel_key].nil?
78
+
79
+ client_key = make_client_key(url, db_num)
80
+ redis_client = redis_clients[client_key]
81
+
82
+ if redis_client.nil?
83
+ uri = URI.parse(url)
84
+ redis_client = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) rescue nil
85
+ if redis_client.nil?
86
+ puts "!!!\n!!! Can not connect to redis server at #{uri}\n!!!"
87
+ return nil
88
+ end
89
+ redis_client.select db_num
90
+ redis_clients[client_key] = redis_client
91
+ end
92
+ channel = Channel.new(name, redis_client)
93
+ capsule_channels[channel_key] = channel
94
+ channel
95
+ end
96
+
97
+
98
+ def self.listen(channels_array, redis_url=nil, db_number=-1)
99
+ url = redis_url || config.redis_url
100
+ db_num = db_number
101
+ db_num = config.db_number if db_num < 0
102
+ channels = *channels_array
103
+ key = make_listener_key(channels, url, db_number)
104
+ return true unless listener_threads[key].nil?
105
+
106
+ listener_threads[key] = Thread.new do
107
+ Thread.current[:name] = :RedisMessageCapsule
108
+ Thread.current[:description] = "Listening for messages from #{url.to_s} on chanel: #{[*channels].join(',')} "
109
+
110
+ redis_client = nil # establish redis connection:
111
+ until !redis_client.nil? and redis_client.ping
112
+ uri = URI.parse(url)
113
+ redis_client = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password) rescue nil
114
+ if redis_client.nil?
115
+ puts "!!!\n!!! Can not connect to redis server at #{uri}\n!!!"
116
+ sleep 10
117
+ else
118
+ redis_client.select db_num
119
+ puts "connected!"
120
+ Thread.current[:redis_client] = redis_client
121
+ end
122
+ end
123
+
124
+ puts "Listening for messages on #{[*channels].join(', ')}"
125
+ loop do
126
+ channel_element = redis_client.blpop *channels, 0
127
+ channel = channel_element.first
128
+ element = channel_element.last
129
+ payload = ( JSON.parse(element) rescue {} )
130
+ message = payload['data']
131
+ # fire event on channel
132
+ puts "#{channel}: #{message}"
133
+ yield(message) if block_given?
134
+ end
135
+ end
136
+ end
137
+
138
+ end
139
+
140
+
141
+ def start_redis_listeners(channels_array, redis_url=nil)
142
+ channels = *channels_array
143
+
144
+ url = redis_url || ENV["REDIS_URL"] || ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
145
+ key = url.to_s + channels.to_s
146
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'redis_message_capsule/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "redis_message_capsule"
8
+ gem.version = RedisMessageCapsule::VERSION
9
+ gem.authors = ["Arbind"]
10
+ gem.email = ["arbind@carbonfive.com"]
11
+ gem.description = "Send and receive real-time messages between applications (via redis)."
12
+ gem.summary = ""
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ gem.add_runtime_dependency 'redis'
22
+ gem.add_runtime_dependency 'json'
23
+
24
+ gem.add_development_dependency 'rake'
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'simplecov'
27
+
28
+ end
@@ -0,0 +1,13 @@
1
+ # setup for test environment
2
+ ENV['RACK_ENV'] = 'test'
3
+
4
+ # set up test coverage
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ minimum_coverage 100
8
+ end
9
+
10
+ require 'RedisMessageCapsule'
11
+
12
+ # Dont let tests overwrite any production or development data
13
+ abort('Redis not configured for test environment !!!') unless REDIS_DB.eql? REDIS_DB_ENVIRONMENTS[:test]
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis_message_capsule
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Arbind
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
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: json
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: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
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: simplecov
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
+ description: Send and receive real-time messages between applications (via redis).
95
+ email:
96
+ - arbind@carbonfive.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.md
105
+ - Rakefile
106
+ - lib/redis_message_capsule.rb
107
+ - lib/redis_message_capsule/version.rb
108
+ - redis_message_capsule.gemspec
109
+ - spec/spec_helper.rb
110
+ homepage: ''
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.24
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: ''
134
+ test_files:
135
+ - spec/spec_helper.rb