Skip to content

Hipjea/rails-xapi

Repository files navigation

Rails xAPI

xAPI statements creation plugin.

Actions Status

Important

This is an ongoing development. The documentation will be provided as it becomes available.

Installation

Add this line to your application's Gemfile:

gem "rails-xapi", git: "https://github.com/Hipjea/rails-xapi"

And then execute:

$ bundle

Create the migration files:

$ bin/rails rails_xapi:install:migrations

Mount the engine in config/routes.rb:

mount RailsXapi::Engine, at: "rails-xapi"

Usage

Statement creation

Create a service class or controller method within your main application that handles data preparation and invokes RailsXapi::StatementCreator:

class XapiStatementCreator
  def self.create_statement(data:, request: nil, user: nil, async: false)
    if request.present? && user.present?
      user_name = "#{user.firstname} #{user.lastname}"
      actor = {
        objectType: "Agent",
        name: user_name,
        mbox: "mailto:#{user.email}",
        account: {
          homePage: "#{data[:base_url] || request.base_url}/users/#{user&.id}",
          name: user_name
        }
      }
    end

    statement_creator = RailsXapi::StatementCreator.new(data, actor)
    return statement_creator.call_async if async

    statement_creator.call
  end
end

You can then use the class within your controllers, for e.g.:

XapiStatementCreator.create_statement(request: request, user: current_user, data: {
  # We can omit the actor struct if we pass the current_user to create_statement.
  verb: {
    id: "https://brindlewaye.com/xAPITerms/verbs/loggedin/"
  },
  object: {
    id: new_user_session_url,
    definition: {
      name: "log in",
      description: {
        "en-US" => "User signed in"
      },
      type: "sign-in"
    }
  }
})

Data query

def logs_per_month(year = Date.current.year, month = Date.current.month)
  RailsXapi::QueryActor.user_statements_per_month({mbox: "mailto:#{email}"}, year, month)
end

Test

bundle exec rails db:schema:load RAILS_ENV=test
bundle exec rspec spec/

License

The gem is available as open source under the terms of the MIT License.

Releases

No releases published

Packages

No packages published

Languages