Class: RailsXapi::Actor

Inherits:
ApplicationRecord show all
Includes:
Serializable
Defined in:
app/models/rails_xapi/actor.rb

Overview

The Actor defines who performed the action. See: https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#242-actor

Constant Summary collapse

OBJECT_TYPES =
%w[Agent Group]

Constants included from Serializable

Serializable::LATIN_LETTERS, Serializable::LATIN_LETTERS_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#member ⇒ Object

Returns the value of attribute member.



11
12
13
# File 'app/models/rails_xapi/actor.rb', line 11

def member
  @member
end

#objectType ⇒ Object

Returns the value of attribute objectType.



11
12
13
# File 'app/models/rails_xapi/actor.rb', line 11

def objectType
  @objectType
end

Class Method Details

.build_actor_from_data(data) ⇒ RailsXapi::Actor

Build the Actor object from the given data and user email.

Parameters:

  • data (Hash) —

    The data used to build the actor object, including optional nested account data.

Returns:



32
33
34
35
36
37
# File 'app/models/rails_xapi/actor.rb', line 32

def self.build_actor_from_data(data)
  data = (data)

  conditions = data.slice(:mbox, :mbox_sha1sum, :openid).compact
  find_by(conditions) || create(data)
end

.by_iri_or_create(data) ⇒ RailsXapi::Actor

Find an Actor by its identifiers or create a new one.

Parameters:

  • data (Hash) —

    The data to find or create the actor.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/rails_xapi/actor.rb', line 43

def self.by_iri_or_create(data)
  data = (data)

  actor =
    find_or_create_by(
      mbox: data[:mbox],
      mbox_sha1sum: data[:mbox_sha1sum],
      openid: data[:openid]
    ) { |a| a.attributes = data }

  unless actor.valid?
    raise RailsXapi::Errors::XapiError,
          I18n.t("rails_xapi.errors.invalid_actor")
  end

  actor
end

Instance Method Details

#as_json ⇒ Hash

Overrides the Hash class method to camelize object_type, according to the xAPI specification. See: https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#part-two-experience-api-data

Returns:

  • (Hash) —

    The actor hash with the camel-case version of object_type.



78
79
80
81
82
83
84
85
86
87
# File 'app/models/rails_xapi/actor.rb', line 78

def as_json
  {
    objectType: object_type,
    name: name,
    mbox: mbox,
    mbox_sha1sum: mbox_sha1sum,
    account: .as_json,
    openid: openid
  }.compact
end

#validate_mbox ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/rails_xapi/actor.rb', line 61

def validate_mbox
  return if mbox.blank?

  mbox_valid =
    mbox.strip =~ /\Amailto:([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/
  unless mbox_valid
    raise RailsXapi::Errors::XapiError,
          I18n.t("rails_xapi.errors.malformed_mbox", name: mbox)
  end

  true
end