Class: ActiveLrs::StatementGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/active_lrs/statement/statement_generator.rb

Overview

Rails generator to fetch an xAPI profile and generate a statement model.

This generator connects to the xAPI Profile Server, fetches the profile document for the given IRI, extracts verbs, and generates a Rails model representing xAPI statements for that profile.

Examples:

Generate a statement model for a profile

rails generate active_lrs:statement "http://example.com/xapi/profile/1234"

Instance Method Summary collapse

Instance Method Details

#create_modelvoid

This method returns an undefined value.

Generates a Rails model file for the xAPI statements of this profile.

The model file is generated using the model.rb.tt template and named based on the xAPI profile’s prefLabel.



72
73
74
75
76
77
78
# File 'lib/generators/active_lrs/statement/statement_generator.rb', line 72

def create_model
  @xapi_profile_name = @xapi_profile_document.dig("prefLabel", "en")
  @file_name = "#{@xapi_profile_name.parameterize.underscore}_statement"
  @class_name = @file_name.camelize

  template "model.rb.tt", "app/models/#{@file_name}.rb"
end

#fetch_xapi_profile_documentvoid

This method returns an undefined value.

Fetches the xAPI profile document from the configured profile server.

Raises:

  • (SystemExit)

    Exits with status 1 if the fetch fails



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/active_lrs/statement/statement_generator.rb', line 25

def fetch_xapi_profile_document
  connection = Faraday.new(url: ActiveLrs.configuration.xapi_profile_server_url) do |builder|
    builder.response :json
  end

  response = connection.get("api/profile", iri: xapi_profile_iri)

  unless response.success?
    say_status :error, "Unable to fetch xAPI profile.", :red
    exit 1
  end

  @xapi_profile_document = response.body
end

#parse_concepts_verbsvoid

This method returns an undefined value.

Parses verbs defined in the “concepts” section of the xAPI profile.



43
44
45
46
47
48
49
50
# File 'lib/generators/active_lrs/statement/statement_generator.rb', line 43

def parse_concepts_verbs
  @verbs = @xapi_profile_document.dig("concepts").filter_map do |concept|
    {
      name: concept.dig("prefLabel", "en"),
      iri: concept.dig("id")
    } if concept.dig("type") == "Verb"
  end
end

#parse_templates_verbsvoid

This method returns an undefined value.

Parses verbs defined in the “templates” section of the xAPI profile and adds them if they are not already present.



56
57
58
59
60
61
62
63
64
# File 'lib/generators/active_lrs/statement/statement_generator.rb', line 56

def parse_templates_verbs
  @verbs += @xapi_profile_document.dig("templates").filter_map do |template|
    iri = template.dig("verb")
    {
      name: template.dig("prefLabel", "en"),
      iri: iri
    } if template.key?("verb") && @verbs.none? { |verb| verb[:iri] == iri }
  end
end

#xapi_profile_iriString

xAPI profile IRI to fetch

Returns:

  • (String)


19
# File 'lib/generators/active_lrs/statement/statement_generator.rb', line 19

argument :xapi_profile_iri, type: :string, required: true