livedata 0.2.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/CHANGELOG ADDED
File without changes
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 siddick
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 ADDED
@@ -0,0 +1,43 @@
1
+ == LiveData
2
+ LiveData is a rubygem. This gem is used to implement the applications like chat, live process montior, etc.,
3
+
4
+
5
+ == Installation
6
+ Step 1 :-
7
+ Add the livedata gem in config/environment.rb
8
+ config.gem "livedata"
9
+ Step 2 :-
10
+ Add the following configuration in the config/environments/development.rb, config/environments/production.rb and config/environments/test.rb
11
+ config.threadsafe!
12
+
13
+ == Example
14
+ Class MainController < ApplicationController
15
+
16
+ def get_msg
17
+ user_id = params[:id] || "test"
18
+ chat = LiveData.get_channel('chat') || LiveData.create_channel('chat')
19
+ user = chat.get_user( user_id ) || chat.create_user( user_id )
20
+ render :text => user.read
21
+ end
22
+
23
+ def send_msg
24
+ user_id = params[:id] || "test"
25
+ message = params[:msg] || "Nothing"
26
+
27
+ chat = LiveData.get_channel('chat') || LiveData.create_channel('chat')
28
+ user = chat.get_user( user_id ) || chat.create_user( user_id )
29
+
30
+ user.write( message )
31
+ render :text => 'Finesh'
32
+ end
33
+ end
34
+
35
+ == Run Webrick Server
36
+ ruby script/server -b 0.0.0.0 -p 3000
37
+ == Browser
38
+ Open two tabs in the web browser
39
+ First tab :-
40
+ http://localhost:3000/main/get_msg/tester
41
+ Second Tab :-
42
+ http://localhost:3000/main/send_msg/tester?msg=Hello World
43
+
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require File.join(File.dirname(__FILE__), 'lib', 'live_data', 'version')
6
+
7
+ PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
8
+ PKG_NAME = 'livedata'
9
+ PKG_VERSION = LiveData::VERSION + PKG_BUILD
10
+ PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
11
+
12
+ RELEASE_NAME = "REL #{PKG_VERSION}"
13
+
14
+
15
+ desc 'Default: run unit tests.'
16
+ task :default => :test
17
+
18
+ desc 'Test the live_data plugin.'
19
+ Rake::TestTask.new(:test) do |t|
20
+ t.libs << 'lib'
21
+ t.libs << 'test'
22
+ t.pattern = 'test/**/*_test.rb'
23
+ t.verbose = true
24
+ end
25
+
26
+ desc 'Generate documentation for the live_data plugin.'
27
+ Rake::RDocTask.new(:rdoc) do |rdoc|
28
+ rdoc.rdoc_dir = 'rdoc'
29
+ rdoc.title = 'LiveData'
30
+ rdoc.options << '--line-numbers' << '--inline-source'
31
+ rdoc.rdoc_files.include('README')
32
+ rdoc.rdoc_files.include('lib/**/*.rb')
33
+ end
34
+
35
+ dist_dirs = [ "lib", "test", "examples" ]
36
+
37
+ spec = Gem::Specification.new do |s|
38
+ s.platform = Gem::Platform::RUBY
39
+ s.name = PKG_NAME
40
+ s.version = PKG_VERSION
41
+ s.summary = "Handle live data"
42
+ s.description = %q{Using the livedata plugin, we can implement application like chat, status monitoring, etc.}
43
+
44
+ s.files = [ "Rakefile","MIT-LICENSE", "install.rb", "README", "CHANGELOG" ]
45
+ dist_dirs.each do |dir|
46
+ s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
47
+ end
48
+
49
+ s.require_path = 'lib'
50
+ s.autorequire = 'live_data'
51
+
52
+ s.has_rdoc = true
53
+ s.extra_rdoc_files = %w( README )
54
+ s.rdoc_options.concat ['--main', 'README']
55
+
56
+ s.author = "Mohammed Siddick. E"
57
+ s.email = "siddick007@yahoo.co.in"
58
+ s.homepage = "http://siddick.github.com/"
59
+ #s.rubyforge_project = "live_data"
60
+ end
61
+
62
+ Rake::GemPackageTask.new(spec) do |p|
63
+ p.gem_spec = spec
64
+ p.need_tar = true
65
+ p.need_zip = true
66
+ end
67
+
data/install.rb ADDED
File without changes
@@ -0,0 +1,102 @@
1
+
2
+ module LiveData
3
+
4
+ # Channel is used to maintain users, groups and user-group relation
5
+ class Channel
6
+
7
+ attr :users, :groups, :user_in_groups, :group_have_users, :name
8
+
9
+ # Create a new channel
10
+ def initialize( name = nil )
11
+ @name = name || self
12
+ @users = {}
13
+ @groups = {}
14
+ @user_in_groups = {}
15
+ @group_have_users = {}
16
+ end
17
+
18
+ # Write yaml data
19
+ # ==== Parameters
20
+ # * +yaml_data+ - String which contain data in yaml format
21
+ def write_yaml( yaml_data )
22
+ @users.each{|user_name, user|
23
+ user.write_yaml( yaml_data )
24
+ }
25
+ end
26
+
27
+ # Write json data
28
+ # ==== Parameters
29
+ # * +json_data+ - String which contain data in json format
30
+ def write_json( json_data )
31
+ @users.each{|user_name, user|
32
+ user.write_json( json_data )
33
+ }
34
+ end
35
+
36
+ # Write data in any format
37
+ # ==== Parameters
38
+ # * +data+ - it may be any Object.
39
+ def write( data )
40
+ @users.each{|user_name, user|
41
+ user.write( data )
42
+ }
43
+ end
44
+
45
+ # Create a user, if he is not present in the user list
46
+ # ==== Parameters
47
+ # * +name+ - User name
48
+ def create_user( name )
49
+ unless( @users[name] )
50
+ @users[name] = LiveData::User.new( name, self )
51
+ @user_in_groups[name] = @users[name].groups
52
+ end
53
+ return @users[name]
54
+ end
55
+
56
+ # Create a group, if the group is not present in the group list
57
+ # ==== Parameters
58
+ # * +name+ - Group name
59
+ def create_group( name )
60
+ unless( @groups[name] )
61
+ @groups[name] = LiveData::Group.new( name, self )
62
+ @group_have_users[name] = @groups[name].users
63
+ end
64
+ return @groups[name]
65
+ end
66
+
67
+ # Get user object
68
+ def get_user( name )
69
+ @users[name]
70
+ end
71
+
72
+ # Get group object
73
+ def get_group( name )
74
+ @groups[name]
75
+ end
76
+
77
+ # Destroy a user
78
+ # ==== Parameters
79
+ # * +name+ - User name
80
+ def destroy_user( name )
81
+ @users[name].destroy()
82
+ end
83
+
84
+
85
+ # Destroy a group
86
+ # ==== Parameters
87
+ # * +name+ - Group name
88
+ def destroy_group( name )
89
+ @groups[name].destroy()
90
+ end
91
+
92
+ # Destroy current channel
93
+ def destroy
94
+ @users.each{|name,obj|
95
+ obj.destroy()
96
+ }
97
+ @groups.each{|name,obj|
98
+ obj.destroy()
99
+ }
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,74 @@
1
+ module LiveData
2
+
3
+ # Group is used to maintain collection of users
4
+ # ==== Example
5
+ #
6
+ # chat = LiveData::Channel.new
7
+ # guest1 = chat.create_user('guest1')
8
+ # guest2 = chat.create_user('guest2')
9
+ # user_grp = chat.create_group('user')
10
+ #
11
+ # user_grp.add_user( guest1 )
12
+ # user_grp.add_user( guest2 )
13
+ #
14
+ # user_grp.write( { :title => "Greeting",
15
+ # :message => "Wecome to LiveData" } )
16
+ #
17
+ # group1.read # { :title => "Greeti..... }
18
+ # group2.read # { :title => "Greeti..... }
19
+ class Group
20
+
21
+ attr :users, :name
22
+
23
+ # Create a group
24
+ def initialize( name = nil, channel = nil )
25
+ @name = name || self
26
+ @channel = channel
27
+ @users = []
28
+ end
29
+
30
+ # Write data, which contain yaml format
31
+ def write_yaml( yaml_data )
32
+ @users.each{|user|
33
+ user.write_yaml( yaml_data )
34
+ }
35
+ end
36
+
37
+ # Write data, which contain json format
38
+ def write_json( json_data )
39
+ @users.each{|user|
40
+ user.write_json( json_data )
41
+ }
42
+ end
43
+
44
+ # Write any object
45
+ def write( data )
46
+ @users.each{|user|
47
+ user.write( data )
48
+ }
49
+ end
50
+
51
+ # Add user to the group
52
+ def add_user( user )
53
+ @users.push( user )
54
+ user.groups.push( self )
55
+ end
56
+
57
+ # Delete user from the group
58
+ def remove_user( user )
59
+ @users.delete( user )
60
+ user.groups.delete( self )
61
+ end
62
+
63
+ def destroy
64
+ @users.each{|user|
65
+ user.groups.delete( self )
66
+ }
67
+ if( @channel )
68
+ @channel.groups.delete( @name )
69
+ @channel.group_have_users.delete( @name )
70
+ end
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,94 @@
1
+
2
+ module LiveData
3
+ class User
4
+
5
+ IntegerPackCode = "I"
6
+
7
+ attr :groups, :name
8
+
9
+ # Create a user object
10
+ def initialize( name = nil, channel = nil )
11
+ @name = name || self
12
+ @channel = channel
13
+ @lock = Mutex.new
14
+ @read_pipe, @write_pipe = IO.pipe
15
+ @groups = []
16
+ end
17
+
18
+ # Reset the write pipe and read pipe
19
+ def reset
20
+ begin
21
+ @write_pipe.close
22
+ @read_pipe.close
23
+ rescue => err
24
+ end
25
+ @read_pipe, @write_pipe = IO.pipe
26
+ end
27
+
28
+ # Clean the Contain in the pipe
29
+ def clean
30
+ begin
31
+ while( @read_pipe.read_nonblock( 10000 ) )
32
+ end
33
+ rescue => err
34
+ end
35
+ end
36
+
37
+ # Read json contain
38
+ def read_json
39
+ @read_pipe.gets()
40
+ end
41
+
42
+ # Read yaml contain
43
+ def read_yaml
44
+ len, etc = @read_pipe.read(4).unpack( IntegerPackCode )
45
+ @read_pipe.read( len )
46
+ end
47
+
48
+ # read a Object
49
+ def read
50
+ YAML.load( read_yaml )
51
+ end
52
+
53
+ # Write a string, which contain json format
54
+ # ==== Parameters
55
+ # * +json_data+ - json string
56
+ def write_json( json_data )
57
+ @write_pipe.write( json_data + "\n" )
58
+ end
59
+
60
+ # Write a string, which contain yam format
61
+ # ==== Parameters
62
+ # * +yaml_data+ - yaml string
63
+ def write_yaml( yaml_data )
64
+ return unless yaml_data and yaml_data.class == String
65
+ len = [ yaml_data.length ].pack( IntegerPackCode )
66
+ @write_pipe.write( len )
67
+ @write_pipe.write( yaml_data )
68
+ end
69
+
70
+ # Write a Object
71
+ # ==== Parameters
72
+ # * +data+ - any Object
73
+ def write( data )
74
+ write_yaml( data.to_yaml )
75
+ end
76
+
77
+ # Destroy the user
78
+ def destroy
79
+ @groups.dup.each{|grp|
80
+ grp.remove_user( self )
81
+ }
82
+ if( @channel )
83
+ @channel.users.delete( @name )
84
+ @channel.user_in_groups.delete( @name )
85
+ end
86
+ begin
87
+ @read_pipe.close
88
+ @write_pipe.close
89
+ rescue => err
90
+ end
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module LiveData
3
+ VERSION = '0.2.1'
4
+ end
data/lib/live_data.rb ADDED
@@ -0,0 +1,38 @@
1
+ # LiveData
2
+
3
+ require 'thread'
4
+ require 'yaml'
5
+ require 'json'
6
+
7
+ require 'live_data/version'
8
+ require 'live_data/channel'
9
+ require 'live_data/user'
10
+ require 'live_data/group'
11
+ module LiveData
12
+ Channels = {}
13
+
14
+ def self.create_channel( name )
15
+ unless( Channels[name] )
16
+ Channels[name] = LiveData::Channel.new( name )
17
+ end
18
+ return Channels[name]
19
+ end
20
+
21
+ def self.destroy_channel( name )
22
+ channel = self.get_channel( name )
23
+ channel.destroy()
24
+ end
25
+
26
+ def self.get_channel( name, create = true )
27
+ channel = Channels[name]
28
+ if( !channel and create )
29
+ channel = LiveData.create_channel( name )
30
+ end
31
+ return channel
32
+ end
33
+
34
+ def self.check_channel( name )
35
+ Channels[name]
36
+ end
37
+
38
+ end
data/lib/livedata.rb ADDED
@@ -0,0 +1 @@
1
+ require 'live_data'
@@ -0,0 +1,80 @@
1
+ require 'test_helper'
2
+
3
+ class LiveDataTest < Test::Unit::TestCase
4
+
5
+ def test_channel
6
+ chat = LiveData.create_channel( 'chat' )
7
+
8
+ assert chat.class == LiveData::Channel
9
+ assert chat.name == 'chat'
10
+ assert chat == LiveData.get_channel( 'chat' )
11
+ end
12
+
13
+ def test_user
14
+ chat = LiveData.create_channel( 'chat' )
15
+ guest1 = chat.create_user( 'guest1' )
16
+
17
+ assert guest1.class == LiveData::User
18
+ assert guest1.name == 'guest1'
19
+ assert guest1 == chat.get_user( 'guest1' )
20
+ assert guest1 == chat.create_user( 'guest1' )
21
+
22
+ chat.destroy_user( 'guest1' )
23
+
24
+ assert chat.get_user( 'guest1') == nil
25
+ assert chat.users['guest1'] == nil
26
+ assert chat.user_in_groups['guest1'] == nil
27
+
28
+
29
+ end
30
+
31
+ def test_group
32
+ chat = LiveData.create_channel( 'chat' )
33
+ users_grp = chat.create_group( 'users' )
34
+
35
+ assert users_grp.class == LiveData::Group
36
+ assert users_grp.name == 'users'
37
+ assert users_grp == chat.get_group( 'users' )
38
+ assert users_grp == chat.create_group( 'users' )
39
+
40
+ guest1 = chat.create_user( 'guest1' )
41
+ users_grp.add_user( guest1 )
42
+ assert users_grp.users.include? guest1
43
+ assert guest1.groups.include? users_grp
44
+
45
+ users_grp.remove_user( guest1 )
46
+ assert !users_grp.users.include?(guest1)
47
+ assert !guest1.groups.include?(users_grp)
48
+
49
+ chat.destroy_group( 'users' )
50
+
51
+ assert chat.get_group('users' ) == nil
52
+ assert chat.group_have_users['users'] == nil
53
+ end
54
+
55
+ def test_process_data
56
+ chat = LiveData.create_channel( 'chat' )
57
+ guest1 = chat.create_user( 'guest1' )
58
+ guest2 = chat.create_user( 'guest2' )
59
+ grp = chat.create_group( 'users')
60
+ data = { :title => "Greating", :message => "Welcome you all" }
61
+ data1 = { :title => "Greating1", :message => "Welcome you all" }
62
+ data2 = { :title => "Greating2", :message => "Welcome you all" }
63
+ grp.add_user( guest1 )
64
+ grp.add_user( guest2 )
65
+
66
+ guest1.write( data )
67
+ assert data == guest1.read
68
+
69
+ guest2.write( data1 )
70
+ tmp_data = guest2.read
71
+ assert data1 == tmp_data
72
+ assert data != tmp_data
73
+
74
+
75
+ grp.write( data2 )
76
+ assert data2 == guest1.read
77
+ assert data2 == guest2.read
78
+
79
+ end
80
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'live_data'
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: livedata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Mohammed Siddick. E
8
+ autorequire: live_data
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-13 00:00:00 +05:30
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Using the livedata plugin, we can implement application like chat, status monitoring, etc.
17
+ email: siddick007@yahoo.co.in
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - Rakefile
26
+ - MIT-LICENSE
27
+ - install.rb
28
+ - README
29
+ - CHANGELOG
30
+ - lib/live_data/channel.rb
31
+ - lib/live_data/group.rb
32
+ - lib/live_data/user.rb
33
+ - lib/live_data/version.rb
34
+ - lib/livedata.rb
35
+ - lib/live_data.rb
36
+ - test/live_data_test.rb
37
+ - test/test_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://siddick.github.com/
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Handle live data
67
+ test_files: []
68
+