Class: ActiveLrs::Xapi::Attachment

Inherits:
Object
  • Object
show all
Includes:
LocalizationHelper
Defined in:
lib/active_lrs/xapi/attachment.rb

Overview

Represents an xAPI Attachment object.

Attachments provide additional context or supporting data for an xAPI Statement, such as documents, media files, or extensions. Each Attachment includes metadata such as usage type, display name, content type, length, and a SHA-2 hash for validation.

This class is intended for use in the attachments property of an xAPI Statement.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LocalizationHelper

#get_localized_value

Constructor Details

#initialize(attributes = {}) ⇒ void

Initializes a new Attachment instance with optional attributes.

Parameters:

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

    a hash of attachment attributes

Options Hash (attributes):

  • "usageType" (String)

    An IRI describing the use of the attachment.

  • "display" (Hash{String => String})

    A language map for the display name.

  • "description" (Hash{String => String})

    A language map for the description.

  • "contentType" (String)

    The MIME type of the attachment.

  • "length" (Integer)

    The size of the attachment in bytes.

  • "sha2" (String)

    The SHA-2 hash of the attachment.

  • "fileUrl" (String)

    An IRL pointing to the attachment.



56
57
58
59
60
61
62
63
64
# File 'lib/active_lrs/xapi/attachment.rb', line 56

def initialize(attributes = {})
  self.usage_type = attributes["usageType"] if attributes["usageType"]
  self.display = attributes["display"] if attributes["display"]
  self.description = attributes["description"] if attributes["description"]
  self.content_type = attributes["contentType"] if attributes["contentType"]
  self.length = attributes["length"] if attributes["length"]
  self.sha2 = attributes["sha2"] if attributes["sha2"]
  self.file_url = attributes["fileUrl"] if attributes["fileUrl"]
end

Instance Attribute Details

#content_typeString?

Returns The MIME type of the Attachment (e.g., “image/png”).

Returns:

  • (String, nil)

    The MIME type of the Attachment (e.g., “image/png”).



32
33
34
# File 'lib/active_lrs/xapi/attachment.rb', line 32

def content_type
  @content_type
end

#descriptionHash{String => String}?

Returns A language map providing a human-readable description of the Attachment.

Returns:

  • (Hash{String => String}, nil)

    A language map providing a human-readable description of the Attachment.



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

def description
  @description
end

#displayHash{String => String}?

Returns A language map providing a display name for the Attachment.

Returns:

  • (Hash{String => String}, nil)

    A language map providing a display name for the Attachment.



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

def display
  @display
end

#file_urlString?

Returns An IRL (Internationalized Resource Locator) pointing to the Attachment file.

Returns:

  • (String, nil)

    An IRL (Internationalized Resource Locator) pointing to the Attachment file.



42
43
44
# File 'lib/active_lrs/xapi/attachment.rb', line 42

def file_url
  @file_url
end

#lengthInteger?

Returns The size of the Attachment file in bytes.

Returns:

  • (Integer, nil)

    The size of the Attachment file in bytes.



35
36
37
# File 'lib/active_lrs/xapi/attachment.rb', line 35

def length
  @length
end

#sha2String?

Returns The SHA-2 hash of the Attachment data, used for integrity checks.

Returns:

  • (String, nil)

    The SHA-2 hash of the Attachment data, used for integrity checks.



38
39
40
# File 'lib/active_lrs/xapi/attachment.rb', line 38

def sha2
  @sha2
end

#usage_typeString?

Returns An IRI indicating the intended use of this Attachment (e.g., “id.tincanapi.com/attachment/supporting_media”).

Returns:



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

def usage_type
  @usage_type
end

Instance Method Details

#localize_description(locale: nil) ⇒ String

Returns the localized description of the attachment.

Parameters:

  • locale (String, Symbol, nil) (defaults to: nil)

    Optional locale to use. Defaults to nil (will use configured defaults).

Returns:

  • (String)

    The localized description, or “undefined” if not available.



114
115
116
# File 'lib/active_lrs/xapi/attachment.rb', line 114

def localize_description(locale: nil)
  get_localized_value(description, locale)
end

#localize_display(locale: nil) ⇒ String

Returns the localized display of the attachment.

Parameters:

  • locale (String, Symbol, nil) (defaults to: nil)

    Optional locale to use. Defaults to nil (will use configured defaults).

Returns:

  • (String)

    The localized display, or “undefined” if not available.



106
107
108
# File 'lib/active_lrs/xapi/attachment.rb', line 106

def localize_display(locale: nil)
  get_localized_value(display, locale)
end

#to_hHash{String => String, Hash, Integer}

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

Examples:

attachment = ActiveLrs::Xapi::Attachment.new(
  "usageType" => "http://id.tincanapi.com/attachment/supporting_media",
  "display" => { "en-US" => "Screenshot" },
  "contentType" => "image/png",
  "length" => 12345,
  "sha2" => "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
  "fileUrl" => "https://example.com/screenshot.png"
)
attachment.to_h
# => {
#   "usageType" => "http://id.tincanapi.com/attachment/supporting_media",
#   "display" => { "en-US" => "Screenshot" },
#   "contentType" => "image/png",
#   "length" => 12345,
#   "sha2" => "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12",
#   "fileUrl" => "https://example.com/screenshot.png"
# }

Returns:

  • (Hash{String => String, Hash, Integer})

    a hash including only the present attributes



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/active_lrs/xapi/attachment.rb', line 90

def to_h
  node = {}
  node["usageType"] = usage_type if usage_type
  node["display"] = display if display
  node["description"] = description if description
  node["contentType"] = content_type if content_type
  node["length"] = length if length
  node["sha2"] = sha2 if sha2
  node["fileUrl"] = file_url if file_url
  node
end