Class: ActiveLrs::Xapi::Activity

Inherits:
Object
  • Object
show all
Defined in:
lib/active_lrs/xapi/activity.rb

Overview

Represents an xAPI Activity object.

An Activity is a type of xAPI Object that describes a learning activity, resource, or experience. It must have an id and an objectType of ‘“Activity”`. Optionally, it may also include a definition that provides human-readable metadata such as name, description, type, or extensions.

This class is intended for use as the object property within an xAPI Statement.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new Activity instance with optional attributes.

Parameters:

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

    a hash of activity attributes

Options Hash (attributes):

  • "id" (String)

    An IRI (Internationalized Resource Identifier) that uniquely identifies the Activity.

  • "object_type" (String)

    The object type. MUST be the literal string “Activity” when present.

  • "definition" (ActivityDefinition)

    Optional metadata about the Activity, including name, description, type, and extensions.



42
43
44
45
46
# File 'lib/active_lrs/xapi/activity.rb', line 42

def initialize(attributes = {})
  @object_type = "Activity"
  self.id = attributes["id"] if attributes["id"]
  self.definition = ActivityDefinition.new(attributes["definition"]) if attributes["definition"]
end

Instance Attribute Details

#definitionActivityDefinition?

including human-readable name, description, type, and extensions.

Returns:



29
30
31
# File 'lib/active_lrs/xapi/activity.rb', line 29

def definition
  @definition
end

#idString?

uniquely identifies the Activity.

Returns:

  • (String, nil)

    An IRI (Internationalized Resource Identifier) that



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

def id
  @id
end

#object_typeString

when present, according to the xAPI specification.

Returns:

  • (String)

    The object type. MUST be the literal string “Activity”



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

def object_type
  @object_type
end

Instance Method Details

#to_hHash{String => Object}

Converts the Activity object into a hash representation suitable for serialization in an xAPI Statement.

Examples:

activity = ActiveLrs::Xapi::Activity.new(
  "id" => "http://example.com/activities/lesson1",
  "object_type" => "Activity",
  "definition" => definition_object
)
activity.to_h
# => {
#   "id" => "http://example.com/activities/lesson1",
#   "objectType" => "Activity",
#   "definition" => { ... }
# }

Returns:

  • (Hash{String => Object})

    a hash including only the present attributes (id, objectType, definition) suitable for xAPI serialization



66
67
68
69
70
71
72
# File 'lib/active_lrs/xapi/activity.rb', line 66

def to_h
  node = {}
  node["objectType"] = object_type
  node["id"] = id.to_s if id
  node["definition"] = definition.to_h if definition
  node
end