streama 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +32 -16
- data/VERSION +1 -1
- data/lib/streama/activity.rb +15 -15
- data/lib/streama/actor.rb +3 -3
- data/lib/streama/definition.rb +2 -2
- data/lib/streama/definition_dsl.rb +3 -3
- data/lib/streama/errors.rb +4 -1
- data/spec/lib/activity_spec.rb +14 -20
- data/spec/lib/actor_spec.rb +5 -5
- data/spec/lib/definition_dsl_spec.rb +6 -6
- data/spec/lib/definition_spec.rb +6 -6
- data/spec/support/models/activity.rb +2 -2
- data/spec/support/models/user.rb +3 -0
- data/streama.gemspec +2 -2
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
Streama is a simple Ruby activity stream gem for use with the Mongoid ODM framework.
|
4
4
|
|
5
|
+
**UPDATE: Streama now conforms to the activitystrea.ms specification. Version 0.3.0 breaks compatibility with previous versions.**
|
6
|
+
|
5
7
|
== Install
|
6
8
|
|
7
9
|
$ gem install streama
|
@@ -12,16 +14,28 @@ Streama is a simple Ruby activity stream gem for use with the Mongoid ODM framew
|
|
12
14
|
|
13
15
|
Create an Activity model and define the activities and the fields you would like to cache within the activity.
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
+
An activity consists of an actor, a verb, an object, and a target.
|
18
|
+
|
19
|
+
class Activity
|
20
|
+
include Streama::Activity
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
activity :new_enquiry do
|
23
|
+
actor :user, :cache => [:full_name]
|
24
|
+
object :enquiry, :cache => [:subject, :comment]
|
25
|
+
target :listing, :cache => [:title]
|
26
|
+
end
|
23
27
|
|
24
|
-
end
|
28
|
+
end
|
29
|
+
|
30
|
+
The activity verb is implied from the activity name, in the above example the verb is :new_enquiry
|
31
|
+
|
32
|
+
The object may be the entity performing the activity, or the entity on which the activity was performed.
|
33
|
+
e.g John(actor) shared a video(object)
|
34
|
+
|
35
|
+
The target is the object that the verb is enacted on.
|
36
|
+
e.g. Geraldine(actor) posted a photo(object) to her album(target)
|
37
|
+
|
38
|
+
This is based on the Activity Streams 1.0 specification (http://activitystrea.ms)
|
25
39
|
|
26
40
|
=== Setup Actors
|
27
41
|
|
@@ -31,8 +45,10 @@ Include the Actor module in a class and override the default followers method.
|
|
31
45
|
include Mongoid::Document
|
32
46
|
include Streama::Actor
|
33
47
|
|
48
|
+
field :full_name, :type => String
|
49
|
+
|
34
50
|
def followers
|
35
|
-
|
51
|
+
User.excludes(:id => self.id).all
|
36
52
|
end
|
37
53
|
end
|
38
54
|
|
@@ -46,14 +62,14 @@ Create the indexes for the Activities collection. You can do so by calling the c
|
|
46
62
|
|
47
63
|
In your controller or background worker:
|
48
64
|
|
49
|
-
current_user.publish_activity(:
|
65
|
+
current_user.publish_activity(:new_enquiry, :object => @enquiry, :target => @listing)
|
50
66
|
|
51
|
-
This will publish the activity to the objects returned by the
|
67
|
+
This will publish the activity to the mongoid objects returned by the #followers method in the Actor.
|
52
68
|
|
53
69
|
To send your activity to different receievers, pass in an additional :receivers parameter.
|
54
70
|
|
55
|
-
current_user.publish_activity(:
|
56
|
-
current_user.publish_activity(:
|
71
|
+
current_user.publish_activity(:new_enquiry, :object => @enquiry, :target => @listing, :receivers => :friends) # calls friends method
|
72
|
+
current_user.publish_activity(:new_enquiry, :object => @enquiry, :target => @listing, :receivers => current_user.find(:all, :conditions => {:group_id => mygroup}))
|
57
73
|
|
58
74
|
== Retrieving Activity
|
59
75
|
|
@@ -63,9 +79,9 @@ To retrieve all activity for an actor
|
|
63
79
|
|
64
80
|
To retrieve and filter to a particular activity type
|
65
81
|
|
66
|
-
current_user.activity_stream(:type => :
|
82
|
+
current_user.activity_stream(:type => :activity_verb)
|
67
83
|
|
68
|
-
If you need to return the instance of an :actor, :
|
84
|
+
If you need to return the instance of an :actor, :object or :target from an activity call the Activity#load_instance method
|
69
85
|
|
70
86
|
activity.load_instance(:actor)
|
71
87
|
|
@@ -75,7 +91,7 @@ You can also refresh the cached activity data by calling the Activity#refresh_da
|
|
75
91
|
|
76
92
|
== Compatibility
|
77
93
|
|
78
|
-
Streama is developed against Ruby 1.9.2 and Mongoid 2
|
94
|
+
Streama is developed against Ruby 1.9.2 and Mongoid 2
|
79
95
|
|
80
96
|
== TODO
|
81
97
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/lib/streama/activity.rb
CHANGED
@@ -7,19 +7,19 @@ module Streama
|
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::Timestamps
|
9
9
|
|
10
|
-
field :
|
10
|
+
field :verb, :type => Symbol
|
11
11
|
field :actor, :type => Hash
|
12
|
+
field :object, :type => Hash
|
12
13
|
field :target, :type => Hash
|
13
|
-
field :referrer, :type => Hash
|
14
14
|
field :receivers, :type => Array
|
15
15
|
|
16
16
|
index :name
|
17
17
|
index [['actor._id', Mongo::ASCENDING], ['actor._type', Mongo::ASCENDING]]
|
18
|
+
index [['object._id', Mongo::ASCENDING], ['object._type', Mongo::ASCENDING]]
|
18
19
|
index [['target._id', Mongo::ASCENDING], ['target._type', Mongo::ASCENDING]]
|
19
|
-
index [['referrer._id', Mongo::ASCENDING], ['referrer._type', Mongo::ASCENDING]]
|
20
20
|
index [['receivers.id', Mongo::ASCENDING], ['receivers.type', Mongo::ASCENDING]]
|
21
21
|
|
22
|
-
validates_presence_of :actor, :
|
22
|
+
validates_presence_of :actor, :verb
|
23
23
|
before_save :assign_data
|
24
24
|
|
25
25
|
end
|
@@ -33,8 +33,8 @@ module Streama
|
|
33
33
|
# @example Define a new activity
|
34
34
|
# activity(:enquiry) do
|
35
35
|
# actor :user, :cache => [:full_name]
|
36
|
-
#
|
37
|
-
#
|
36
|
+
# object :enquiry, :cache => [:subject]
|
37
|
+
# target :listing, :cache => [:title]
|
38
38
|
# end
|
39
39
|
#
|
40
40
|
# @return [Definition] Returns the registered definition
|
@@ -46,18 +46,18 @@ module Streama
|
|
46
46
|
|
47
47
|
# Publishes an activity using an activity name and data
|
48
48
|
#
|
49
|
-
# @param [ String ]
|
49
|
+
# @param [ String ] verb The verb of the activity
|
50
50
|
# @param [ Hash ] data The data to initialize the activity with.
|
51
51
|
#
|
52
52
|
# @return [Streama::Activity] An Activity instance with data
|
53
|
-
def publish(
|
53
|
+
def publish(verb, data)
|
54
54
|
receivers = data.delete(:receivers)
|
55
|
-
new({:
|
55
|
+
new({:verb => verb}.merge(data)).publish(:receivers => receivers)
|
56
56
|
end
|
57
57
|
|
58
58
|
def stream_for(actor, options={})
|
59
59
|
query = {:receivers => {'$elemMatch' => {:id => actor.id, :type => actor.class.to_s}}}
|
60
|
-
query.merge!({:
|
60
|
+
query.merge!({:verb => options[:type]}) if options[:type]
|
61
61
|
self.where(query).without(:receivers).desc(:created_at)
|
62
62
|
end
|
63
63
|
|
@@ -77,11 +77,11 @@ module Streama
|
|
77
77
|
self
|
78
78
|
end
|
79
79
|
|
80
|
-
# Returns an instance of an actor,
|
80
|
+
# Returns an instance of an actor, object or target
|
81
81
|
#
|
82
|
-
# @param [ Symbol ] type The data type (actor,
|
82
|
+
# @param [ Symbol ] type The data type (actor, object, target) to return an instance for.
|
83
83
|
#
|
84
|
-
# @return [
|
84
|
+
# @return [Mongoid::Document] document A mongoid document instance
|
85
85
|
def load_instance(type)
|
86
86
|
(data = self.send(type)).is_a?(Hash) ? data['type'].to_s.camelcase.constantize.find(data['id']) : data
|
87
87
|
end
|
@@ -95,7 +95,7 @@ module Streama
|
|
95
95
|
|
96
96
|
def assign_data
|
97
97
|
|
98
|
-
[:actor, :
|
98
|
+
[:actor, :object, :target].each do |type|
|
99
99
|
next unless object = load_instance(type)
|
100
100
|
|
101
101
|
class_sym = object.class.name.underscore.to_sym
|
@@ -115,7 +115,7 @@ module Streama
|
|
115
115
|
end
|
116
116
|
|
117
117
|
def definition
|
118
|
-
@definition ||= Streama::Definition.find(
|
118
|
+
@definition ||= Streama::Definition.find(verb)
|
119
119
|
end
|
120
120
|
|
121
121
|
end
|
data/lib/streama/actor.rb
CHANGED
@@ -21,8 +21,8 @@ module Streama
|
|
21
21
|
#
|
22
22
|
# @param [ Hash ] options The options to publish with.
|
23
23
|
#
|
24
|
-
# @example publish an activity with a
|
25
|
-
# current_user.publish_activity(:enquiry, :
|
24
|
+
# @example publish an activity with a object and target
|
25
|
+
# current_user.publish_activity(:enquiry, :object => @enquiry, :target => @listing)
|
26
26
|
#
|
27
27
|
def publish_activity(name, options={})
|
28
28
|
options[:receivers] = self.send(options[:receivers]) if options[:receivers].is_a?(Symbol)
|
@@ -34,7 +34,7 @@ module Streama
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def followers
|
37
|
-
|
37
|
+
raise Streama::NoFollowersDefined
|
38
38
|
end
|
39
39
|
|
40
40
|
def activity_class
|
data/lib/streama/definition.rb
CHANGED
@@ -2,14 +2,14 @@ module Streama
|
|
2
2
|
|
3
3
|
class Definition
|
4
4
|
|
5
|
-
attr_reader :name, :actor, :
|
5
|
+
attr_reader :name, :actor, :object, :target, :receivers
|
6
6
|
|
7
7
|
# @param dsl [Streama::DefinitionDSL] A DSL object
|
8
8
|
def initialize(definition)
|
9
9
|
@name = definition[:name]
|
10
10
|
@actor = definition[:actor] || {}
|
11
|
+
@object = definition[:object] || {}
|
11
12
|
@target = definition[:target] || {}
|
12
|
-
@referrer = definition[:referrer] || {}
|
13
13
|
end
|
14
14
|
|
15
15
|
#
|
@@ -8,8 +8,8 @@ module Streama
|
|
8
8
|
@attributes = {
|
9
9
|
:name => name.to_sym,
|
10
10
|
:actor => {},
|
11
|
-
:
|
12
|
-
:
|
11
|
+
:object => {},
|
12
|
+
:target => {}
|
13
13
|
}
|
14
14
|
end
|
15
15
|
|
@@ -22,7 +22,7 @@ module Streama
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
25
|
-
data_methods :actor, :
|
25
|
+
data_methods :actor, :object, :target
|
26
26
|
|
27
27
|
end
|
28
28
|
|
data/lib/streama/errors.rb
CHANGED
@@ -7,7 +7,7 @@ module Streama
|
|
7
7
|
end
|
8
8
|
|
9
9
|
# This error is raised when an object isn't defined
|
10
|
-
# as an actor,
|
10
|
+
# as an actor, object or target
|
11
11
|
#
|
12
12
|
# Example:
|
13
13
|
#
|
@@ -38,4 +38,7 @@ module Streama
|
|
38
38
|
class ActivityNotSaved < StreamaError
|
39
39
|
end
|
40
40
|
|
41
|
+
class NoFollowersDefined < StreamaError
|
42
|
+
end
|
43
|
+
|
41
44
|
end
|
data/spec/lib/activity_spec.rb
CHANGED
@@ -10,9 +10,9 @@ describe "Activity" do
|
|
10
10
|
it "registers and return a valid definition" do
|
11
11
|
@definition = Activity.activity(:new_enquiry) do
|
12
12
|
actor :user, :cache => [:full_name]
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
object :enquiry, :cache => [:comment]
|
14
|
+
object :listing, :cache => [:title, :full_address]
|
15
|
+
target :listing, :cache => [:title]
|
16
16
|
end
|
17
17
|
|
18
18
|
@definition.is_a?(Streama::Definition).should be true
|
@@ -20,23 +20,19 @@ describe "Activity" do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
describe '#publish' do
|
23
|
-
|
24
|
-
before :each do
|
25
|
-
@actor = user
|
26
|
-
@activity = Activity.publish(:new_enquiry, {:actor => @actor, :target => enquiry, :referrer => listing})
|
27
|
-
end
|
28
23
|
|
29
24
|
it "overrides the recievers if option passed" do
|
30
25
|
send_to = []
|
31
26
|
2.times { |n| send_to << User.create(:full_name => "Custom Receiver #{n}") }
|
32
27
|
5.times { |n| User.create(:full_name => "Receiver #{n}") }
|
33
|
-
|
34
|
-
|
35
|
-
# @activity.receivers.size.should == 2
|
28
|
+
@activity = Activity.publish(:new_enquiry, {:actor => user, :object => enquiry, :target => listing, :receivers => send_to})
|
29
|
+
@activity.receivers.size.should == 2
|
36
30
|
end
|
37
31
|
|
38
32
|
context "when republishing"
|
39
33
|
before :each do
|
34
|
+
@actor = user
|
35
|
+
@activity = Activity.publish(:new_enquiry, {:actor => @actor, :object => enquiry, :target => listing})
|
40
36
|
@activity.publish
|
41
37
|
end
|
42
38
|
|
@@ -45,14 +41,12 @@ describe "Activity" do
|
|
45
41
|
@actor.save
|
46
42
|
@activity.publish
|
47
43
|
@activity.actor['full_name'].should eq "testing"
|
48
|
-
|
49
44
|
end
|
50
45
|
end
|
51
46
|
|
52
|
-
|
53
47
|
describe '.publish' do
|
54
48
|
it "creates a new activity" do
|
55
|
-
activity = Activity.publish(:new_enquiry, {:actor => user, :
|
49
|
+
activity = Activity.publish(:new_enquiry, {:actor => user, :object => enquiry, :target => listing})
|
56
50
|
activity.should be_an_instance_of Activity
|
57
51
|
end
|
58
52
|
end
|
@@ -61,7 +55,7 @@ describe "Activity" do
|
|
61
55
|
|
62
56
|
before :each do
|
63
57
|
@user = user
|
64
|
-
@activity = Activity.publish(:new_enquiry, {:actor => @user, :
|
58
|
+
@activity = Activity.publish(:new_enquiry, {:actor => @user, :object => enquiry, :target => listing})
|
65
59
|
end
|
66
60
|
|
67
61
|
it "reloads instances and updates activities stored data" do
|
@@ -79,7 +73,7 @@ describe "Activity" do
|
|
79
73
|
describe '#load_instance' do
|
80
74
|
|
81
75
|
before :each do
|
82
|
-
@activity = Activity.publish(:new_enquiry, {:actor => user, :
|
76
|
+
@activity = Activity.publish(:new_enquiry, {:actor => user, :object => enquiry, :target => listing})
|
83
77
|
@activity = Activity.last
|
84
78
|
end
|
85
79
|
|
@@ -87,12 +81,12 @@ describe "Activity" do
|
|
87
81
|
@activity.load_instance(:actor).should be_instance_of User
|
88
82
|
end
|
89
83
|
|
90
|
-
it "loads
|
91
|
-
@activity.load_instance(:
|
84
|
+
it "loads an object instance" do
|
85
|
+
@activity.load_instance(:object).should be_instance_of Enquiry
|
92
86
|
end
|
93
87
|
|
94
|
-
it "loads a
|
95
|
-
@activity.load_instance(:
|
88
|
+
it "loads a target instance" do
|
89
|
+
@activity.load_instance(:target).should be_instance_of Listing
|
96
90
|
end
|
97
91
|
|
98
92
|
end
|
data/spec/lib/actor_spec.rb
CHANGED
@@ -9,8 +9,8 @@ describe "Actor" do
|
|
9
9
|
before :all do
|
10
10
|
Activity.activity :new_comment do
|
11
11
|
actor :user, :cache => [:full_name]
|
12
|
+
object :listing, :cache => [:title]
|
12
13
|
target :listing, :cache => [:title]
|
13
|
-
referrer :listing, :cache => [:title]
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -21,12 +21,12 @@ describe "Actor" do
|
|
21
21
|
end
|
22
22
|
|
23
23
|
it "pushes activity to receivers" do
|
24
|
-
activity = user.publish_activity(:new_enquiry, :
|
24
|
+
activity = user.publish_activity(:new_enquiry, :object => enquiry, :target => listing)
|
25
25
|
activity.receivers.size == 6
|
26
26
|
end
|
27
27
|
|
28
28
|
it "pushes to a defined stream" do
|
29
|
-
activity = user.publish_activity(:new_enquiry, :
|
29
|
+
activity = user.publish_activity(:new_enquiry, :object => enquiry, :target => listing, :receivers => :friends)
|
30
30
|
activity.receivers.size == 6
|
31
31
|
end
|
32
32
|
|
@@ -36,8 +36,8 @@ describe "Actor" do
|
|
36
36
|
|
37
37
|
before :each do
|
38
38
|
5.times { |n| User.create(:full_name => "Receiver #{n}") }
|
39
|
-
user.publish_activity(:new_enquiry, :
|
40
|
-
user.publish_activity(:new_comment, :
|
39
|
+
user.publish_activity(:new_enquiry, :object => enquiry, :target => listing)
|
40
|
+
user.publish_activity(:new_comment, :object => listing)
|
41
41
|
end
|
42
42
|
|
43
43
|
it "retrieves the stream for an actor" do
|
@@ -21,16 +21,16 @@ describe "Definition" do
|
|
21
21
|
dsl.attributes[:actor].should eq :user => { :cache=>[:id, :full_name] }, :company => { :cache=>[:id, :name] }
|
22
22
|
end
|
23
23
|
|
24
|
-
it "adds
|
24
|
+
it "adds an object to the definition" do
|
25
25
|
dsl = definition_dsl
|
26
|
-
dsl.
|
27
|
-
dsl.attributes[:
|
26
|
+
dsl.object(:listing, :cache => [:id, :title])
|
27
|
+
dsl.attributes[:object].should eq :listing => { :cache=>[:id, :title] }
|
28
28
|
end
|
29
29
|
|
30
|
-
it "adds a
|
30
|
+
it "adds a target to the definition" do
|
31
31
|
dsl = definition_dsl
|
32
|
-
dsl.
|
33
|
-
dsl.attributes[:
|
32
|
+
dsl.target(:company, :cache => [:id, :name])
|
33
|
+
dsl.attributes[:target].should eq :company => { :cache=>[:id, :name] }
|
34
34
|
end
|
35
35
|
|
36
36
|
end
|
data/spec/lib/definition_spec.rb
CHANGED
@@ -5,8 +5,8 @@ describe "Definition" do
|
|
5
5
|
let(:definition_dsl) do
|
6
6
|
dsl = Streama::DefinitionDSL.new(:new_enquiry)
|
7
7
|
dsl.actor(:user, :cache => [:id, :full_name])
|
8
|
-
dsl.
|
9
|
-
dsl.
|
8
|
+
dsl.object(:enquiry, :cache => [:id, :full_name])
|
9
|
+
dsl.target(:listing, :cache => [:id, :name, :full_address])
|
10
10
|
dsl
|
11
11
|
end
|
12
12
|
|
@@ -19,12 +19,12 @@ describe "Definition" do
|
|
19
19
|
it "assigns @actor" do
|
20
20
|
@definition.actor.has_key?(:user).should be true
|
21
21
|
end
|
22
|
-
it "assigns @
|
23
|
-
@definition.
|
22
|
+
it "assigns @object" do
|
23
|
+
@definition.object.has_key?(:enquiry).should be true
|
24
24
|
end
|
25
25
|
|
26
|
-
it "assigns @
|
27
|
-
@definition.
|
26
|
+
it "assigns @target" do
|
27
|
+
@definition.target.has_key?(:listing).should be true
|
28
28
|
end
|
29
29
|
|
30
30
|
end
|
data/spec/support/models/user.rb
CHANGED
data/streama.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{streama}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Christos Pappas"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-06-23}
|
13
13
|
s.description = %q{Streama is a simple activity stream gem for use with the Mongoid ODM framework.}
|
14
14
|
s.email = %q{christos.pappas@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: streama
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.3.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Christos Pappas
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-06-23 00:00:00 +10:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -127,7 +127,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
127
127
|
requirements:
|
128
128
|
- - ">="
|
129
129
|
- !ruby/object:Gem::Version
|
130
|
-
hash: -
|
130
|
+
hash: -1352868759098387394
|
131
131
|
segments:
|
132
132
|
- 0
|
133
133
|
version: "0"
|