Class: RailsXapi::Actor
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- RailsXapi::Actor
- 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
-
#member ⇒ Object
Returns the value of attribute member.
-
#objectType ⇒ Object
Returns the value of attribute objectType.
Class Method Summary collapse
-
.build_actor_from_data(data) ⇒ RailsXapi::Actor
Build the Actor object from the given data and user email.
-
.by_iri_or_create(data) ⇒ RailsXapi::Actor
Find an Actor by its identifiers or create a new one.
Instance Method Summary collapse
-
#as_json ⇒ Hash
Overrides the Hash class method to camelize object_type, according to the xAPI specification.
- #validate_mbox ⇒ Object
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.
32 33 34 35 36 37 |
# File 'app/models/rails_xapi/actor.rb', line 32 def self.build_actor_from_data(data) data = handle_account_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.
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 = handle_account_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
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: 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 |