Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change statement_creator #33

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/"
},
Expand Down
4 changes: 1 addition & 3 deletions app/models/rails_xapi/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions app/services/rails_xapi/statement_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
Loading