Class: ActiveLrs::Xapi::Statement

Inherits:
#StatementBase
  • Object
show all
Defined in:
lib/active_lrs/xapi/statement.rb

Overview

Represents a full xAPI Statement.

A Statement captures a single learning experience or action. It consists of an actor (who performed the action), a verb (the action), and an object (the target of the action). Optionally, it may include result, context, timestamp, attachments, and other metadata.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new Statement.

Parameters:

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

    Hash containing statement attributes

Options Hash (attributes):

  • "id" (String)

    Optional UUID for the statement

  • "actor" (Hash)

    Actor information (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

  • "attachments" (Array<Hash>)

    Array of attachment objects

  • "authority" (Hash)

    Authority information

  • "version" (String)

    xAPI version string



51
52
53
54
55
56
57
58
59
60
# File 'lib/active_lrs/xapi/statement.rb', line 51

def initialize(attributes = {})
  super(attributes)
  self.raw_xapi = attributes
  self.id = attributes["id"] if attributes["id"]
  self.stored =  Time.parse(attributes["stored"]) if attributes["stored"]
  self.raw_stored = attributes["stored"] if attributes["stored"]
  self.authority = Xapi::Agent.new(attributes["authority"]) if attributes["authority"]
  self.version = attributes["version"] if attributes["version"]
  self.voided = attributes["voided"] if attributes["voided"]
end

Instance Attribute Details

#authorityAgent?

Returns Authority responsible for the statement.

Returns:

  • (Agent, nil)

    Authority responsible for the statement



28
29
30
# File 'lib/active_lrs/xapi/statement.rb', line 28

def authority
  @authority
end

#idString?

Returns Optional UUID for the statement.

Returns:

  • (String, nil)

    Optional UUID for the statement



18
19
20
# File 'lib/active_lrs/xapi/statement.rb', line 18

def id
  @id
end

#raw_storedString?

Returns The original stored string from the xAPI statement.

Returns:

  • (String, nil)

    The original stored string from the xAPI statement



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

def raw_stored
  @raw_stored
end

#raw_xapiHash?

Returns Raw xAPI payload from the LRS.

Returns:

  • (Hash, nil)

    Raw xAPI payload from the LRS



15
16
17
# File 'lib/active_lrs/xapi/statement.rb', line 15

def raw_xapi
  @raw_xapi
end

#storedTime?

Returns The timestamp when the xAPI statement was received by the LRS, parsed as a Time object.

Returns:

  • (Time, nil)

    The timestamp when the xAPI statement was received by the LRS, parsed as a Time object.



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

def stored
  @stored
end

#versionString?

Returns xAPI version string.

Returns:

  • (String, nil)

    xAPI version string



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

def version
  @version
end

#voidedBoolean

Returns True if the statement has been voided.

Returns:

  • (Boolean)

    True if the statement has been voided



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

def voided
  @voided
end

Instance Method Details

#to_hHash{String => Object}

Converts the Statement into a hash suitable for sending to an LRS.

Examples:

statement = ActiveLrs::Xapi::Statement.new(
  "id" => "123e4567-e89b-12d3-a456-426614174000",
  "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
# => {
#   "id" => "123e4567-e89b-12d3-a456-426614174000",
#   "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.



82
83
84
85
86
87
88
89
90
# File 'lib/active_lrs/xapi/statement.rb', line 82

def to_h
  node = super
  node["id"] = id if id
  node["stored"] = raw_stored || stored.strftime("%FT%T%:z") if stored
  node["authority"] = authority.to_h if authority
  node["voided"] = voided if voided
  node["version"] = version.to_s if version
  node
end