Class: Nelumba::Activity

Inherits:
Object
  • Object
show all
Includes:
Object
Defined in:
lib/nelumba/activity.rb

Overview

This class represents an Activity object that represents an action taken by a Person.

Constant Summary collapse

STANDARD_TYPES =
[:article, :audio, :bookmark, :comment, :file, :folder,
:group, :list, :note, :person, :image,
:place, :playlist, :product, :review, :service, :status,
:video]

Constants included from Object

Object::USERNAME_REGULAR_EXPRESSION

Instance Attribute Summary collapse

Attributes included from Object

#author, #content, #display_name, #html, #image, #published, #summary, #text, #title, #uid, #updated, #url

Instance Method Summary collapse

Methods included from Object

#init, #reply_to, #to_as1, #to_html, #to_json, #to_text

Constructor Details

#initialize(options = {}, &blk) ⇒ Activity

Create a new entry with the given action and object.

options:

:object      => The object of this activity.
:type        => The type of object for this activity.
:target      => The target of this activity.
:verb        => The action of the activity.

:actor        => An Nelumba::Person responsible for generating this entry.
:source       => An Nelumba::Feed where this Entry originated. This
                 should be used when an Entry is copied into this feed
                 from another.
:published    => The DateTime depicting when the entry was originally
                 published.
:updated      => The DateTime depicting when the entry was modified.
:url          => The canonical url of the entry.
:uid          => The unique id that identifies this entry.
:in_reply_to  => An Nelumba::Entry for which this entry is a response.
                 Or an array of Nelumba::Entry's that this entry is a
                 response to. Use this when this Entry is a reply
                 to an existing Entry.


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nelumba/activity.rb', line 87

def initialize(options = {}, &blk)
  super(options, &blk)

  @object      = options[:object]

  @type        = options[:type]
  if STANDARD_TYPES.map(&:to_s).include? @type
    @type = @type.intern
  end

  @target      = options[:target]
  @verb        = options[:verb]

  @actor        = options[:actor]
  @source       = options[:source]
  @published    = options[:published]
  @updated      = options[:updated]
  @url          = options[:url]
  @uid          = options[:uid]

  unless options[:in_reply_to].nil? or options[:in_reply_to].is_a?(Array)
    options[:in_reply_to] = [options[:in_reply_to]]
  end

  @in_reply_to  = options[:in_reply_to]  || []
  @replies      = options[:replies]      || []

  @mentions     = options[:mentions]     || []
  @likes        = options[:likes]        || []
  @shares       = options[:shares]       || []

  @interactions = options[:interactions] || {}
end

Instance Attribute Details

#actorObject (readonly)

Holds an Nelumba::Person.



45
46
47
# File 'lib/nelumba/activity.rb', line 45

def actor
  @actor
end

#in_reply_toObject (readonly)

Holds an array of related Nelumba::Activity’s that this entry is a response to.



52
53
54
# File 'lib/nelumba/activity.rb', line 52

def in_reply_to
  @in_reply_to
end

#interactionsObject (readonly)

Holds a hash containing the information about interactions where keys are verbs.

For instance, it could have a :share key, with a hash containing the number of times it has been shared.



21
22
23
# File 'lib/nelumba/activity.rb', line 21

def interactions
  @interactions
end

#likesObject (readonly)

Holds an array of Nelumba::Person’s that have favorited this activity.



58
59
60
# File 'lib/nelumba/activity.rb', line 58

def likes
  @likes
end

#mentionsObject (readonly)

Holds an array of Nelumba::Person’s that are mentioned in this activity.



64
65
66
# File 'lib/nelumba/activity.rb', line 64

def mentions
  @mentions
end

#objectObject (readonly)

The object of this activity.



24
25
26
# File 'lib/nelumba/activity.rb', line 24

def object
  @object
end

#repliesObject (readonly)

Holds an array of related Nelumba::Activity’s that are replies to this one.



55
56
57
# File 'lib/nelumba/activity.rb', line 55

def replies
  @replies
end

#sharesObject (readonly)

Holds an array of Nelumba::Person’s that have shared this activity.



61
62
63
# File 'lib/nelumba/activity.rb', line 61

def shares
  @shares
end

#sourceObject (readonly)

Holds the source of this entry as an Nelumba::Feed.



48
49
50
# File 'lib/nelumba/activity.rb', line 48

def source
  @source
end

#targetObject (readonly)

The target of the action.



42
43
44
# File 'lib/nelumba/activity.rb', line 42

def target
  @target
end

#typeObject (readonly)

The type of object for this activity.

The field can be a String for uncommon types. Several are standard:

:article, :audio, :bookmark, :comment, :file, :folder, :group,
:list, :note, :person, :image, :place, :playlist,
:product, :review, :service, :video


32
33
34
# File 'lib/nelumba/activity.rb', line 32

def type
  @type
end

#verbObject (readonly)

The action being invoked in this activity.

The field can be a String for uncommon verbs. Several are standard:

:favorite, :follow, :like, :"make-friend", :join, :play,
:post, :save, :share, :tag, :update


39
40
41
# File 'lib/nelumba/activity.rb', line 39

def verb
  @verb
end

Instance Method Details

#interaction_count(verb) ⇒ Object

Returns the number of times the given verb has been used with this Activity.



123
124
125
126
127
128
129
130
# File 'lib/nelumba/activity.rb', line 123

def interaction_count(verb)
  hash = self.interactions
  if hash && hash.has_key?(verb)
    hash[verb][:count] || 0
  else
    0
  end
end

#published_ago_in_wordsObject



132
133
134
# File 'lib/nelumba/activity.rb', line 132

def published_ago_in_words
  TimeLord::Period.new(self.published.to_time, Time.now).to_words
end

#sentence(options = {}) ⇒ Object

Generates a sentence describing this activity in the current or given locale.

Usage:

# The default locale
Nelumba::Activity.new(:verb => :post,
                    :object => Nelumba::Note(:content => "hello"),
                    :actor => Nelumba::Person.new(:name => "wilkie"))
               .sentence
# => "wilkie posted a note"

Nelumba::Activity.new(:verb => :follow,
                    :object => Nelumba::Person.new(:name => "carol"),
                    :actor => Nelumba::Person.new(:name => "wilkie"))
               .sentence
# => "wilkie followed carol"

# In Spanish
Nelumba::Activity.new(:verb => :post,
                    :object => Nelumba::Note(:content => "hello"),
                    :actor => Nelumba::Person.new(:name => "wilkie"))
               .sentence(:locale => :es)
# => "wilkie puso una nota"


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/nelumba/activity.rb', line 209

def sentence(options = {})
  object_owner = nil

  if self.verb == :favorite || self.verb == :share
    if self.object.author
      object_owner = self.object.author.name
    elsif self.object.actor.is_a? Nelumba::Person
      object_owner = self.object.actor.name
    end
  end

  object = self.type

  if self.verb == :favorite || self.verb == :share
    if self.object
      object = self.object.type
    end
  end

  actor = nil

  if self.actor
    actor = self.actor.preferred_display_name
  end

  person = nil

  if self.object.is_a?(Nelumba::Person)
    person = self.object.name
  end

  Nelumba::I18n.sentence({
    :actor => actor,
    :object => object,
    :object_owner => object_owner,
    :person => person,
    :verb => self.verb,
    :target => self.target ? self.target.preferred_display_name : nil
  }.merge(options))
end

#to_atomObject

Returns a string containing the Atom representation of this Activity.



161
162
163
164
165
# File 'lib/nelumba/activity.rb', line 161

def to_atom
  require 'nelumba/atom/entry'

  Nelumba::Atom::Entry.from_canonical(self).to_xml
end

#to_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object

Returns a hash of all relevant fields.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/nelumba/activity.rb', line 141

def to_hash(scheme = 'https', domain = 'example.org', port = nil)
  {
    :source => self.source,

    :in_reply_to => self.in_reply_to.dup,
    :replies => self.replies.dup,

    :mentions => self.mentions.dup,
    :likes => self.likes.dup,
    :shares => self.shares.dup,

    :object => self.object,
    :target => self.target,
    :actor => self.actor,
    :verb => self.verb,
    :type => self.type,
  }.merge(super(scheme, domain, port))
end

#to_json_hash(scheme = 'https', domain = 'example.org', port = nil) ⇒ Object

Returns a hash of all relevant fields with JSON activity streams conventions.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/nelumba/activity.rb', line 169

def to_json_hash(scheme = 'https', domain = 'example.org', port = nil)
  {
    :objectType => "activity",
    :object => @object,
    :actor => @actor,
    :target => @target,
    :type => @type,
    :verb => @verb,
    :source => self.source,
    :in_reply_to => self.in_reply_to.dup,
    :replies => self.replies.dup,
    :mentions => self.mentions.dup,
    :likes => self.likes.dup,
    :shares => self.shares.dup,
  }.merge(super(scheme, domain, port))
end

#updated_ago_in_wordsObject



136
137
138
# File 'lib/nelumba/activity.rb', line 136

def updated_ago_in_words
  TimeLord::Period.new(self.updated.to_time, Time.now).to_words
end