Class: ActiveLrs::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/active_lrs/client.rb

Overview

Provides an HTTP client for connecting to a remote Learning Record Store (LRS). Uses Faraday for requests and supports Basic Authentication for secure access.

Examples:

client = ActiveLrs::Client.new(
  url: "https://lrs.example.com",
  username: "user",
  password: "pass"
)
statement = client.fetch_statement("some-statement-id")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, username:, password:, more_attribute: "more", version: "2.0.0", options: {}) ⇒ void

Initializes a new LRS client.

Parameters:

  • url (String)

    The base URL of the LRS

  • username (String)

    Username for Basic Authentication

  • password (String)

    Password for Basic Authentication

  • more_attribute (String) (defaults to: "more")

    Attribute path for pagination URL (defaults to “more”)

  • version (String) (defaults to: "2.0.0")

    The xAPI version (defaults to “2.0.0”)

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

    Optional Faraday connection options



51
52
53
54
55
56
57
58
# File 'lib/active_lrs/client.rb', line 51

def initialize(url:, username:, password:, more_attribute: "more", version: "2.0.0", options: {})
  @base_url = url
  @username = username
  @password = password
  @more_attribute = more_attribute
  @version = version
  @connection = build_connection(options)
end

Instance Attribute Details

#base_urlString (readonly)

Returns The base URL of the LRS.

Returns:

  • (String)

    The base URL of the LRS



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

def base_url
  @base_url
end

#connectionFaraday::Connection (readonly)

Returns The Faraday connection instance.

Returns:

  • (Faraday::Connection)

    The Faraday connection instance



36
37
38
# File 'lib/active_lrs/client.rb', line 36

def connection
  @connection
end

#more_attributeString (readonly)

Returns The attribute path for pagination (e.g., “more” or “pagination.more”).

Returns:

  • (String)

    The attribute path for pagination (e.g., “more” or “pagination.more”)



30
31
32
# File 'lib/active_lrs/client.rb', line 30

def more_attribute
  @more_attribute
end

#passwordString (readonly)

Returns Password for Basic Authentication.

Returns:

  • (String)

    Password for Basic Authentication



27
28
29
# File 'lib/active_lrs/client.rb', line 27

def password
  @password
end

#usernameString (readonly)

Returns Username for Basic Authentication.

Returns:

  • (String)

    Username for Basic Authentication



24
25
26
# File 'lib/active_lrs/client.rb', line 24

def username
  @username
end

#versionString (readonly)

Returns The xAPI version to use.

Returns:

  • (String)

    The xAPI version to use



33
34
35
# File 'lib/active_lrs/client.rb', line 33

def version
  @version
end

Instance Method Details

#build_connection(options) ⇒ Faraday::Connection

Builds the Faraday connection with standard xAPI headers and authentication.

Parameters:

  • options (Hash)

    Optional Faraday connection options

Returns:

  • (Faraday::Connection)

    Configured Faraday connection



64
65
66
67
68
69
70
71
# File 'lib/active_lrs/client.rb', line 64

def build_connection(options)
  Faraday.new(url: base_url, **options) do |conn|
    conn.headers["X-Experience-API-Version"] = version
    conn.response :json
    conn.request :authorization, :basic, username, password
    conn.adapter Faraday.default_adapter
  end
end

#fetch_statement(id) ⇒ Hash

Fetches a single xAPI statement by ID.

Parameters:

  • id (String)

    The ID of the statement to fetch

Returns:

  • (Hash)

    Parsed statement from the LRS

Raises:



78
79
80
81
# File 'lib/active_lrs/client.rb', line 78

def fetch_statement(id)
  response = connection.get("statements", statementId: id)
  handle_response(response)
end

#fetch_statements(params = {}) ⇒ Array<Hash>

Fetches multiple xAPI statements with optional query parameters.

Parameters:

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

    Optional query parameters to filter statements

Returns:

  • (Array<Hash>)

    Array of statements retrieved from the LRS

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/active_lrs/client.rb', line 88

def fetch_statements(params = {})
  statements = []
  more = nil

  while more != false
    path = more || "xapi/statements"

    response = connection.get(path, params)
    response_body = handle_response(response)

    statements.concat(response_body.fetch("statements", []))

    more = dig_attribute(response_body, more_attribute)
    more = false if more.nil? || more.empty?
  end

  statements
end