diff --git a/README.md b/README.md index 1acc5d9..babb439 100644 --- a/README.md +++ b/README.md @@ -44,22 +44,20 @@ Create a service class or controller method within your main application that ha ```ruby class XapiStatementCreator def self.create_statement(data:, request: nil, user: nil, async: false) - data = data.merge(actor: {objectType: "Agent"}) if data[:actor].blank? - - # We can set the actor's data to be able to omit it in the statements declarations. - # This is an example. Adapt depending on your needs: if request.present? && user.present? - data = data.merge( - actor: data[:actor].merge( - account: { - homePage: "#{data[:base_url] || request.base_url}/users/#{user&.id}", - name: "#{user.firstname} #{user.lastname}" - } - ) - ) + 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, user) + statement_creator = RailsXapi::StatementCreator.new(data, actor) return statement_creator.call_async if async statement_creator.call @@ -71,6 +69,7 @@ You can then use the class within your controllers, for e.g.: ```ruby 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/" }, diff --git a/app/models/rails_xapi/actor.rb b/app/models/rails_xapi/actor.rb index 607f4c9..fa60000 100644 --- a/app/models/rails_xapi/actor.rb +++ b/app/models/rails_xapi/actor.rb @@ -23,10 +23,8 @@ class RailsXapi::Actor < ApplicationRecord # Build the Actor object from the given data and user email. # # @param [Hash] data The data used to build the actor object, including optional nested account data. - # @param [String] user_email The optional email address to be included in the `mbox` field of the data. # @return [RailsXapi::Actor] The actor object initialized with the data. - def self.build_actor_from_data(data, user_email = nil) - data = data.merge(mbox: "mailto:#{user_email}") if user_email.present? + def self.build_actor_from_data(data) data = handle_account_data(data) conditions = data.slice(:mbox, :mbox_sha1sum, :openid).compact diff --git a/app/services/rails_xapi/statement_creator.rb b/app/services/rails_xapi/statement_creator.rb index cb51e2f..ec0d256 100644 --- a/app/services/rails_xapi/statement_creator.rb +++ b/app/services/rails_xapi/statement_creator.rb @@ -3,9 +3,9 @@ class RailsXapi::StatementCreator < ApplicationService attr_reader :data, :user - def initialize(data, user = {}) + def initialize(data, actor = {}) @data = data - @user = user + @actor = actor end def call @@ -23,9 +23,7 @@ def call_async private def prepare_statement - # Send the user infos from the call if passed to it. - user_email = @user.nil? ? nil : @user.presence[:email] - actor = RailsXapi::Actor.build_actor_from_data(@data[:actor], user_email) + actor = RailsXapi::Actor.build_actor_from_data(@actor || @data[:actor]) verb = RailsXapi::Verb.find_or_create_by(id: @data[:verb][:id]) do |v| v.display = @data[:verb][:display]