Class: ActiveLrs::Client
- Inherits:
-
Object
- Object
- ActiveLrs::Client
- 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.
Instance Attribute Summary collapse
-
#base_url ⇒ String
readonly
The base URL of the LRS.
-
#connection ⇒ Faraday::Connection
readonly
The Faraday connection instance.
-
#more_attribute ⇒ String
readonly
The attribute path for pagination (e.g., “more” or “pagination.more”).
-
#password ⇒ String
readonly
Password for Basic Authentication.
-
#username ⇒ String
readonly
Username for Basic Authentication.
-
#version ⇒ String
readonly
The xAPI version to use.
Instance Method Summary collapse
-
#build_connection(options) ⇒ Faraday::Connection
Builds the Faraday connection with standard xAPI headers and authentication.
-
#fetch_statement(id) ⇒ Hash
Fetches a single xAPI statement by ID.
-
#fetch_statements(params = {}) ⇒ Array<Hash>
Fetches multiple xAPI statements with optional query parameters.
-
#initialize(url:, username:, password:, more_attribute: "more", version: "2.0.0", options: {}) ⇒ void
constructor
Initializes a new LRS client.
Constructor Details
#initialize(url:, username:, password:, more_attribute: "more", version: "2.0.0", options: {}) ⇒ void
Initializes a new LRS client.
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() end |
Instance Attribute Details
#base_url ⇒ String (readonly)
Returns The base URL of the LRS.
21 22 23 |
# File 'lib/active_lrs/client.rb', line 21 def base_url @base_url end |
#connection ⇒ Faraday::Connection (readonly)
Returns The Faraday connection instance.
36 37 38 |
# File 'lib/active_lrs/client.rb', line 36 def connection @connection end |
#more_attribute ⇒ String (readonly)
Returns 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 |
#password ⇒ String (readonly)
Returns Password for Basic Authentication.
27 28 29 |
# File 'lib/active_lrs/client.rb', line 27 def password @password end |
#username ⇒ String (readonly)
Returns Username for Basic Authentication.
24 25 26 |
# File 'lib/active_lrs/client.rb', line 24 def username @username end |
#version ⇒ String (readonly)
Returns 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.
64 65 66 67 68 69 70 71 |
# File 'lib/active_lrs/client.rb', line 64 def build_connection() Faraday.new(url: base_url, **) 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.
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.
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 |