Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create StreamActivity packager for creating JSON packages of activity
- Loading branch information
John Metta
committed
Mar 12, 2012
1 parent
353264a
commit 7579fe8
Showing
9 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ActsAsStream | ||
VERSION = "0.0.2" | ||
VERSION = "0.0.2.alpha.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters