Class: ActiveLrs::Xapi::StatementBase
- Inherits:
-
Object
- Object
- ActiveLrs::Xapi::StatementBase
- Defined in:
- lib/active_lrs/xapi/statement_base.rb
Overview
Represents the base structure of an xAPI Statement.
A Statement captures a single experience or action in xAPI. It includes an actor (who did it), a verb (what they did), and an object (the target of the action). Optionally, it may include a result, context, timestamp, and attachments.
This class can serve as a parent or base class for xAPI Statements.
Instance Attribute Summary collapse
-
#actor ⇒ Agent, Group
The actor performing the action.
-
#attachments ⇒ Array<Attachment>?
Optional attachments associated with the statement.
-
#context ⇒ Context?
Optional context providing additional info about the statement.
-
#object ⇒ Activity, ...
The object of the statement.
-
#raw_timestamp ⇒ String?
The original timestamp string.
-
#result ⇒ Result?
Optional result of the activity.
-
#timestamp ⇒ Time?
The timestamp parsed as a Time object.
-
#verb ⇒ Verb
The verb representing the action.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ void
constructor
Initializes a new StatementBase instance.
-
#to_h ⇒ Hash{String => Object}
Converts the StatementBase into a hash suitable for serialization in an xAPI Statement.
Constructor Details
#initialize(attributes = {}) ⇒ void
Initializes a new StatementBase instance.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 51 def initialize(attributes = {}) self.actor = attributes["actor"]["member"] ? Xapi::Group.new(attributes["actor"]) : Xapi::Agent.new(attributes["actor"]) if attributes["actor"] self.verb = Xapi::Verb.new(attributes["verb"]) if attributes["verb"] if (object_node = attributes["object"]) self.object = case object_node["objectType"] when "Group", "Agent" Xapi::Agent.new(object_node) when "StatementRef" Xapi::StatementRef.new(object_node) when "SubStatement" Xapi::SubStatement.new(object_node) else Xapi::Activity.new(object_node) end end self.result = Xapi::Result.new(attributes["result"]) if attributes["result"] self.context = Xapi::Context.new(attributes["context"]) if attributes["context"] self. = Time.parse(attributes["timestamp"]) if attributes["timestamp"] self. = attributes["timestamp"] if attributes["timestamp"] self. = attributes["attachments"]&.map { || Xapi::Attachment.new() } if attributes["attachments"] end |
Instance Attribute Details
#actor ⇒ Agent, Group
Returns The actor performing the action.
16 17 18 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 16 def actor @actor end |
#attachments ⇒ Array<Attachment>?
Returns Optional attachments associated with the statement.
37 38 39 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 37 def @attachments end |
#context ⇒ Context?
Returns Optional context providing additional info about the statement.
28 29 30 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 28 def context @context end |
#object ⇒ Activity, ...
Returns The object of the statement.
22 23 24 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 22 def object @object end |
#raw_timestamp ⇒ String?
Returns The original timestamp string.
34 35 36 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 34 def @raw_timestamp end |
#result ⇒ Result?
Returns Optional result of the activity.
25 26 27 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 25 def result @result end |
#timestamp ⇒ Time?
Returns The timestamp parsed as a Time object.
31 32 33 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 31 def @timestamp end |
#verb ⇒ Verb
Returns The verb representing the action.
19 20 21 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 19 def verb @verb end |
Instance Method Details
#to_h ⇒ Hash{String => Object}
Converts the StatementBase into a hash suitable for serialization in an xAPI Statement.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_lrs/xapi/statement_base.rb', line 91 def to_h node = {} node["actor"] = actor.to_h node["verb"] = verb.to_h node["object"] = object.to_h node["result"] = result.to_h if result node["context"] = context.to_h if context node["timestamp"] = || .strftime("%FT%T%:z") if node["attachments"] = .map { |element| element.to_h } if &.any? node end |