acts_as_stream 0.0.2.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 John Metta
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.md ADDED
@@ -0,0 +1,90 @@
1
+ # ActsAsStream
2
+ A highly configurable activity stream system built on top of Redis
3
+
4
+ ## Warnings
5
+
6
+ * I've just written this today (3/11/12)
7
+ * The test suite is complete, but I haven't used it in production.
8
+ * I'm building it into a production app now
9
+ * It's probably going to be updated frequently
10
+ * You're welcome to use it, and I'd love feedback
11
+ * Caveat Downloader
12
+
13
+ ## Installation
14
+
15
+ Install
16
+
17
+ ```ruby
18
+ gem install acts_as_stream
19
+ ```
20
+
21
+ or in your ```Gemfile```
22
+
23
+ ```ruby
24
+ gem 'acts_as_stream'
25
+ ```
26
+
27
+ Make sure your redis server is running! Redis configuration is outside the scope of this README, but
28
+ check out the [Redis documentation](http://redis.io/documentation).
29
+
30
+ ## Basic Usage
31
+ ```ruby
32
+ ActsAsStream.configure do |config|
33
+ redis = Redis.new(:db => 15)
34
+ config.redis = redis
35
+ config.namespace = :redis_stream_test
36
+ config.activity_scope = :activity
37
+ config.activity_key = :activity
38
+ config.activity_attr = :id
39
+ config.activity_incr = :activity_counter
40
+ config.page_size = 25
41
+ end
42
+
43
+ class User < ActiveRecord::Base
44
+ acts_as_stream
45
+ acts_as_amico # <- not necessary, any follower system will do
46
+
47
+ # Define an all_followers method which will return a list of Redis keys, one per follower
48
+ def all_followers
49
+ get_all(:followers).map{|id| User.find(id.to_i)}.map{|u| u.following_key}
50
+ end
51
+
52
+ end
53
+
54
+ actor = User.create
55
+ follower = User.create
56
+
57
+ follower.follow! actor # <- acts_as_amico syntax, follow whatever your follow system is
58
+
59
+ user.register_activity! 'some smart, perhaps parsable, string package– say, perhaps, JSON'
60
+ (1..5).each{|i| user.register_activity! "activity #{i}"}
61
+
62
+ follower.activity_count
63
+ => 6
64
+
65
+ follower.get_activity.first
66
+ => 'some smart, perhaps parsable, string package– say, perhaps, JSON'
67
+ ```
68
+
69
+ ## Advanced Usage
70
+
71
+ ## Configuration
72
+
73
+ ## Future Plans
74
+
75
+ * Clean up the ActiveResource integration and figure out why :name is so dangerous.
76
+ * Put activity creation/update into a background worker queue
77
+
78
+ ## Contributing to acts_as_stream
79
+
80
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
81
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
82
+ * Fork the project
83
+ * Start a feature/bugfix branch
84
+ * Commit and push until you are happy with your contribution
85
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
86
+ * 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.
87
+
88
+ ## Copyright
89
+
90
+ Copyright (c) John Metta. See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ActsAsStream'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.md')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,14 @@
1
+ require 'redis'
2
+ require 'acts_as_stream'
3
+ require "acts_as_stream/configuration"
4
+ require "acts_as_stream/connector"
5
+ require "acts_as_stream/version"
6
+ require 'acts_as_stream/streamable_object'
7
+
8
+ module ActsAsStream
9
+ extend Configuration
10
+ extend Connector
11
+ end
12
+
13
+ ActiveRecord::Base.send :include, ActsAsStream::StreamableObject
14
+ ActiveResource::Base.send :include, ActsAsStream::StreamableObject
@@ -0,0 +1,49 @@
1
+ module ActsAsStream
2
+ # Configuration settings for ActsAsStream.
3
+ module Configuration
4
+ # Redis instance.
5
+ attr_accessor :redis
6
+
7
+ attr_writer :namespace,
8
+ :activity_key,
9
+ :activity_scope,
10
+ :activity_attr,
11
+ :page_size,
12
+ :activity_incr
13
+
14
+ def configure
15
+ yield self
16
+ if activity_incr.nil?
17
+ warn "You should really define :activity_incr in ActsAsStream Configuration. Using '#{ActsAsStream.namespace}:activity_counter', but that's pretty unsafe!"
18
+ @activity_incr = "#{ActsAsStream.namespace}:activity_counter"
19
+ ActsAsStream.redis.set @activity_incr, 0
20
+ end
21
+ ActsAsStream.redis.setnx(@activity_incr, 0)
22
+ end
23
+
24
+ def namespace
25
+ @namespace ||= :activity_stream
26
+ end
27
+
28
+ def activity_key
29
+ @activity_key ||= :activity
30
+ end
31
+
32
+ def activity_scope
33
+ @activity_scope ||= :activity
34
+ end
35
+
36
+ def activity_attr
37
+ @activity_attr ||= :id
38
+ end
39
+
40
+ def page_size
41
+ @page_size ||= 25
42
+ end
43
+
44
+ def activity_incr
45
+ @activity_incr
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,98 @@
1
+ module ActsAsStream
2
+ module Connector
3
+
4
+ def register_new_activity! package = nil
5
+ return if package.nil?
6
+ id = increment!
7
+ this_time = time
8
+ ActsAsStream.redis.multi do
9
+ #Add the key and package to the system
10
+ ActsAsStream.redis.set "#{base_key}:#{id}", package
11
+ #Add a timed key, scored by id, and an id-based key
12
+ ActsAsStream.redis.zadd "#{base_key}:time:#{this_time}", id, id
13
+ ActsAsStream.redis.set "#{base_key}:id:#{id}", this_time
14
+ end
15
+ raise "### Unable to set Time keys!" if ActsAsStream.redis.get("#{base_key}:id:#{id}") != this_time.to_s
16
+ #return the current activity ID
17
+ id
18
+ end
19
+
20
+ def deregister_activity! ident
21
+ return if ident.nil?
22
+ score = ActsAsStream.redis.get "#{base_key}:id:#{ident}"
23
+ ActsAsStream.redis.multi do
24
+ ActsAsStream.redis.del "#{base_key}:#{ident}"
25
+ ActsAsStream.redis.zrem "#{base_key}:time:#{score}", ident
26
+ ActsAsStream.redis.del "#{base_key}:id:#{ident}"
27
+ end
28
+ deregister_followers! ident
29
+ end
30
+
31
+ def register_followers! options = {}
32
+ options.assert_valid_keys(:following_keys, :activity_id)
33
+ return if options[:following_keys].nil? or options[:activity_id].nil?
34
+ raise ":following_keys must be an array of keys" if not options[:following_keys].is_a? Array
35
+ time = Time.now.to_i
36
+
37
+ ActsAsStream.redis.multi do
38
+ options[:following_keys].each do |key|
39
+ ActsAsStream.redis.zadd key, time, options[:activity_id]
40
+ ActsAsStream.redis.lpush "#{base_key}:followers:#{options[:activity_id]}", key
41
+ end
42
+ end
43
+ end
44
+
45
+ def deregister_followers! activity_id = nil
46
+ return if activity_id.nil?
47
+ followers_key = "#{base_key}:followers:#{activity_id}"
48
+ len = ActsAsStream.redis.llen followers_key
49
+ followers = ActsAsStream.redis.lrange followers_key, 0, len
50
+ return if followers.nil?
51
+ ActsAsStream.redis.multi do
52
+ followers.each do |f|
53
+ ActsAsStream.redis.zrem f, activity_id
54
+ end
55
+ ActsAsStream.redis.del "#{base_key}:followers:#{activity_id}"
56
+ end
57
+ end
58
+
59
+ def get_activity_for follower_key, options = {}
60
+ options = {:page => 1, :page_size => ActsAsStream.page_size}.merge options
61
+ options[:page] = 1 if options[:page] < 1
62
+
63
+ if options[:page] > total_pages(follower_key, options[:page_size])
64
+ options[:page] = total_pages(follower_key, options[:page_size])
65
+ end
66
+
67
+ starting_offset = ((options[:page] - 1) * options[:page_size])
68
+ starting_offset = 0 if starting_offset < 0
69
+ ending_offset = (starting_offset + options[:page_size]) - 1
70
+
71
+ ActsAsStream.redis.zrevrange(follower_key, starting_offset, ending_offset, :with_scores => false).map{|i| get_activity i}
72
+ end
73
+
74
+ def get_activity id
75
+ ActsAsStream.redis.get "#{base_key}:#{id}"
76
+ end
77
+
78
+ def total_pages key, page_size = ActsAsStream.page_size
79
+ (ActsAsStream.redis.zcard(key) / page_size.to_f).ceil
80
+ end
81
+
82
+ def count
83
+ ActsAsStream.redis.get(ActsAsStream.activity_incr).to_i
84
+ end
85
+
86
+ def base_key
87
+ "#{ActsAsStream.namespace}:#{ActsAsStream.activity_scope}"
88
+ end
89
+ private
90
+
91
+ def increment!
92
+ ActsAsStream.redis.incr(ActsAsStream.activity_incr)
93
+ end
94
+ def time
95
+ Time.now.to_i
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,66 @@
1
+ module ActsAsStream
2
+ module StreamableObject
3
+
4
+ def self.included base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def acts_as_stream options = {}
11
+ cattr_accessor :activity_scope, :activity_attr, :activity_key_base, :followers_attr
12
+ self.activity_scope = options[:activity_scope] || ActsAsStream.activity_scope
13
+ self.activity_attr = options[:activity_attr] || ActsAsStream.activity_attr
14
+ self.activity_key_base = "#{ActsAsStream.namespace}:#{self.activity_scope}"
15
+ self.followers_attr = options[:followers_attr] || :all_followers
16
+ send :include, ActsAsStream::StreamableObject::InstanceMethods
17
+ end
18
+ end
19
+
20
+ module InstanceMethods
21
+
22
+ def activity_id
23
+ self.send(self.class.activity_attr)
24
+ end
25
+ def activity_key
26
+ "#{self.class.activity_key_base}:#{activity_id}"
27
+ end
28
+ def following_key
29
+ "#{ActsAsStream.namespace}:#{self.class.name.tableize.singularize}:#{activity_id}:#{activity_scope}"
30
+ end
31
+
32
+ def register_activity! package
33
+ act_id = ActsAsStream.register_new_activity! package
34
+ self.register_followers! act_id
35
+ end
36
+
37
+ def delete_activity act_id
38
+ ActsAsStream.deregister_activity! act_id
39
+ end
40
+
41
+ def register_followers! act_id
42
+ ActsAsStream.register_followers! :following_keys => get_follower_keys, :activity_id => act_id
43
+ end
44
+
45
+ def get_follower_keys
46
+ send(followers_attr)
47
+ end
48
+
49
+ def get_activity options = {}
50
+ if options == :all
51
+ ActsAsStream.get_activity_for following_key,
52
+ :page_size => activity_count,
53
+ :page => 1
54
+ else
55
+ options = {:page_size => ActsAsStream.page_size, :page => 1}.merge options
56
+ ActsAsStream.get_activity_for following_key, :page_size => options[:page_size], :page => options[:page]
57
+ end
58
+ end
59
+
60
+ def activity_count
61
+ ActsAsStream.redis.zcard following_key
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module ActsAsStream
2
+ VERSION = "0.0.2.alpha.1"
3
+ end
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_stream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.alpha.1
5
+ prerelease: 6
6
+ platform: ruby
7
+ authors:
8
+ - John Metta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70248699332900 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70248699332900
25
+ - !ruby/object:Gem::Dependency
26
+ name: redis
27
+ requirement: &70248699332100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70248699332100
36
+ - !ruby/object:Gem::Dependency
37
+ name: sqlite3
38
+ requirement: &70248699331420 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70248699331420
47
+ - !ruby/object:Gem::Dependency
48
+ name: acts_as_amico
49
+ requirement: &70248699330520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70248699330520
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec-rails
60
+ requirement: &70248699329720 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70248699329720
69
+ - !ruby/object:Gem::Dependency
70
+ name: spork
71
+ requirement: &70248699328140 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.9.0.rc
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70248699328140
80
+ - !ruby/object:Gem::Dependency
81
+ name: guard-rspec
82
+ requirement: &70248699343560 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70248699343560
91
+ - !ruby/object:Gem::Dependency
92
+ name: guard-spork
93
+ requirement: &70248699342760 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70248699342760
102
+ - !ruby/object:Gem::Dependency
103
+ name: factory_girl_rails
104
+ requirement: &70248699342180 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70248699342180
113
+ - !ruby/object:Gem::Dependency
114
+ name: simplecov
115
+ requirement: &70248699340840 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70248699340840
124
+ - !ruby/object:Gem::Dependency
125
+ name: fakeweb
126
+ requirement: &70248699339860 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *70248699339860
135
+ - !ruby/object:Gem::Dependency
136
+ name: uuidtools
137
+ requirement: &70248699338500 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: *70248699338500
146
+ description: Rails injectable Redis-backed activity stream system. This is an alpha
147
+ release of code that I just wrote and put into production on 3/11/12. Send feedback
148
+ and post bugs to the Github page, but use at your own risk!
149
+ email:
150
+ - mail@johnmetta.com
151
+ executables: []
152
+ extensions: []
153
+ extra_rdoc_files: []
154
+ files:
155
+ - lib/acts_as_stream/configuration.rb
156
+ - lib/acts_as_stream/connector.rb
157
+ - lib/acts_as_stream/streamable_object.rb
158
+ - lib/acts_as_stream/version.rb
159
+ - lib/acts_as_stream.rb
160
+ - MIT-LICENSE
161
+ - Rakefile
162
+ - README.md
163
+ homepage: http://github.com/mettadore/acts_as_stream.git
164
+ licenses: []
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
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ none: false
177
+ requirements:
178
+ - - ! '>'
179
+ - !ruby/object:Gem::Version
180
+ version: 1.3.1
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 1.8.17
184
+ signing_key:
185
+ specification_version: 3
186
+ summary: Rails injectable Redis-backed activity stream system
187
+ test_files: []