Class: ActiveLrs::Xapi::StatementBase

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new StatementBase instance.

Parameters:

  • attributes (Hash) (defaults to: {})

    A hash of statement attributes

Options Hash (attributes):

  • "actor" (Hash)

    The actor information. Determines if Agent or Group.

  • "verb" (Hash)

    Verb information.

  • "object" (Hash)

    Object information (Activity, Agent, Group, StatementRef, SubStatement).

  • "result" (Hash)

    Result object.

  • "context" (Hash)

    Context object.

  • "timestamp" (String)

    ISO 8601 timestamp string.

  • "attachments" (Array<Hash>)

    Array of attachments.



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.timestamp = Time.parse(attributes["timestamp"]) if attributes["timestamp"]
  self.raw_timestamp = attributes["timestamp"] if attributes["timestamp"]
  self.attachments = attributes["attachments"]&.map { |attachment| Xapi::Attachment.new(attachment) } if attributes["attachments"]
end

Instance Attribute Details

#actorAgent, Group

Returns The actor performing the action.

Returns:

  • (Agent, Group)

    The actor performing the action.



16
17
18
# File 'lib/active_lrs/xapi/statement_base.rb', line 16

def actor
  @actor
end

#attachmentsArray<Attachment>?

Returns Optional attachments associated with the statement.

Returns:

  • (Array<Attachment>, nil)

    Optional attachments associated with the statement.



37
38
39
# File 'lib/active_lrs/xapi/statement_base.rb', line 37

def attachments
  @attachments
end

#contextContext?

Returns Optional context providing additional info about the statement.

Returns:

  • (Context, nil)

    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

#objectActivity, ...

Returns The object of the statement.

Returns:



22
23
24
# File 'lib/active_lrs/xapi/statement_base.rb', line 22

def object
  @object
end

#raw_timestampString?

Returns The original timestamp string.

Returns:

  • (String, nil)

    The original timestamp string.



34
35
36
# File 'lib/active_lrs/xapi/statement_base.rb', line 34

def raw_timestamp
  @raw_timestamp
end

#resultResult?

Returns Optional result of the activity.

Returns:

  • (Result, nil)

    Optional result of the activity.



25
26
27
# File 'lib/active_lrs/xapi/statement_base.rb', line 25

def result
  @result
end

#timestampTime?

Returns The timestamp parsed as a Time object.

Returns:

  • (Time, nil)

    The timestamp parsed as a Time object.



31
32
33
# File 'lib/active_lrs/xapi/statement_base.rb', line 31

def timestamp
  @timestamp
end

#verbVerb

Returns The verb representing the action.

Returns:

  • (Verb)

    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_hHash{String => Object}

Converts the StatementBase into a hash suitable for serialization in an xAPI Statement.

Examples:

statement = ActiveLrs::Xapi::StatementBase.new(
  "actor" => { "mbox" => "mailto:[email protected]" },
  "verb" => { "id" => "http://adlnet.gov/expapi/verbs/completed" },
  "object" => { "id" => "http://example.com/course/1" },
  "timestamp" => "2025-09-19T10:00:00Z"
)
statement.to_h
# => {
#   "actor" => { "objectType" => "Agent", "mbox" => "mailto:[email protected]" },
#   "verb" => { "id" => "http://adlnet.gov/expapi/verbs/completed" },
#   "object" => { "id" => "http://example.com/course/1", "objectType" => "Activity" },
#   "timestamp" => "2025-09-19T10:00:00Z"
# }

Returns:

  • (Hash{String => Object})

    A hash representation of the statement, including only present attributes.



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"] = raw_timestamp || timestamp.strftime("%FT%T%:z") if timestamp
  node["attachments"] = attachments.map { |element| element.to_h } if attachments&.any?
  node
end