Skip to content

Commit

Permalink
Create StreamActivity packager for creating JSON packages of activity
Browse files Browse the repository at this point in the history
  • Loading branch information
John Metta committed Mar 12, 2012
1 parent 353264a commit 7579fe8
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
acts_as_stream (0.0.1)
acts_as_stream (0.0.2.alpha.1)
rails (~> 3.1)
redis

Expand Down
9 changes: 9 additions & 0 deletions README.md
@@ -1,6 +1,15 @@
# ActsAsStream
A highly configurable activity stream system built on top of Redis

## Warnings

* I've just written this today (3/11/12)
* The test suite is complete, but I haven't used it in production.
* I'm building it into a production app now
* It's probably going to be updated frequently
* You're welcome to use it, and I'd love feedback
* Caveat Downloader

## Installation

Install
Expand Down
2 changes: 1 addition & 1 deletion acts_as_stream.gemspec
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.email = ["mail@johnmetta.com"]
s.homepage = "http://github.com/mettadore/acts_as_stream.git"
s.summary = "Rails injectable Redis-backed activity stream system"
s.description = "Rails injectable Redis-backed activity stream system"
s.description = "Rails injectable Redis-backed activity stream system. This is an alpha release of code that I just wrote and put into production on 3/11/12. Send feedback and post bugs to the Github page, but use at your own risk!"

s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]

Expand Down
2 changes: 2 additions & 0 deletions lib/acts_as_stream.rb
Expand Up @@ -4,10 +4,12 @@
require "acts_as_stream/connector"
require "acts_as_stream/version"
require 'acts_as_stream/streamable_object'
require 'acts_as_stream/stream_activity'

module ActsAsStream
extend Configuration
extend Connector
extend StreamActivity
end

ActiveRecord::Base.send :include, ActsAsStream::StreamableObject
Expand Down
30 changes: 30 additions & 0 deletions lib/acts_as_stream/stream_activity.rb
@@ -0,0 +1,30 @@
module ActsAsStream
module StreamActivity

def package options = {}
options.assert_valid_keys(:who, :action, :time, :object, :ignore_stream_hash_on)
raise "You need at least a :who and an :action! to create an activity package" if options[:who].nil? or options[:action].nil?
options = {:time => Time.now.to_i, :ignore_stream_hash_on => []}.merge options
# Try to ensure :time is in seconds
options[:time] = options[:time].to_i if options[:time].is_a?(Time)

# If Objects provide the :to_stream_hash method, use it.
if options[:ignore_stream_hash_on].present?
#convenience, make sure it's an array so we can use include? instead of "include? or equals"
options[:ignore_stream_hash_on] = [options[:ignore_stream_hash_on]] unless options[:ignore_stream_hash_on].is_a?(Array)
end

[:who, :object].each do |opt|
#unless we are ignoring the stream hash for this object, use StreamableObject.stream_hash
unless options[:ignore_stream_hash_on].include?(opt)
options[opt] = options[opt].to_stream_hash
end
end

options.delete(:ignore_stream_hash_on)

# then, if everything is fine, bundle it up into a JSON string
options.to_json
end
end
end
3 changes: 3 additions & 0 deletions lib/acts_as_stream/streamable_object.rb
Expand Up @@ -19,6 +19,9 @@ def acts_as_stream options = {}

module InstanceMethods

def to_stream_hash
{self.class.name.tableize.singularize => {'id' => self.id, activity_attr.to_s => self.send(activity_attr)}}
end
def activity_id
self.send(self.class.activity_attr)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_stream/version.rb
@@ -1,3 +1,3 @@
module ActsAsStream
VERSION = "0.0.2"
VERSION = "0.0.2.alpha.1"
end
54 changes: 54 additions & 0 deletions spec/stream_activity_spec.rb
@@ -0,0 +1,54 @@
require 'spec_helper'

describe ActsAsStream::StreamActivity do

before :each do
@user = Factory :user
@widget = Factory :widget
@admin = Factory :admin
@time = Time.now.to_i
@valid_options = {:who => @user,
:action => "Tested StreamActivity!",
:time => @time,
:object => @widget}
@valid_json = "{\"time\":#{@time},\"who\":{\"user\":{\"id\":#{@user.id}}},\"action\":\"Tested StreamActivity!\",\"object\":{\"widget\":{\"id\":#{@widget.id}}}}"
end

describe "time" do
it "should create a valid package with all valid options" do
ActsAsStream.package(@valid_options).should == @valid_json
end

it "should create a valid package without a time" do
options = @valid_options.dup
options.delete(:time)
#Making an assumption this won't block on time!
time = Time.now.to_i

json = "{\"time\":#{time},\"who\":{\"user\":{\"id\":#{@user.id}}},\"action\":\"Tested StreamActivity!\",\"object\":{\"widget\":{\"id\":#{@widget.id}}}}"
ActsAsStream.package(options).should == json
end

it "should create a valid package with a Time object" do
options = @valid_options.dup
options.delete(:time)
#Making an assumption this won't block on time!
time = Time.now
json = "{\"time\":#{time.to_i},\"who\":{\"user\":{\"id\":#{@user.id}}},\"action\":\"Tested StreamActivity!\",\"object\":{\"widget\":{\"id\":#{@widget.id}}}}"
ActsAsStream.package(options).should == json
end
end

describe "stream_hash" do
it "should encode the actor as complete json" do
options = @valid_options.dup
options[:ignore_stream_hash_on] = :who
ActsAsStream.package(options).should_not == @valid_json
end
end
private

def test_package(options={})
ActsAsStream.package options
end
end
4 changes: 4 additions & 0 deletions spec/streamable_object_spec.rb
Expand Up @@ -59,6 +59,10 @@
usera.get_activity(:all).should =~ packages
end

it "should provide a subset of object attributes for JSON hashing" do

end

private
def test_package number = nil
if number
Expand Down

0 comments on commit 7579fe8

Please sign in to comment.