Class: ActivityProvider::LRS::Statement

Inherits:
Base
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/activity_provider/lrs/statement.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_json, #validate!

Constructor Details

#initialize(*args) ⇒ Statement

Returns a new instance of Statement.



16
17
18
19
20
21
22
# File 'lib/activity_provider/lrs/statement.rb', line 16

def initialize(*args)
  if args.first.is_a?(String)
    super(Statement.convert_json(args.first))
  else
    super(*args)
  end
end

Class Method Details

.convert_json(statement_json) ⇒ Object

Converting JSON to Hash for easy using Virtus



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/activity_provider/lrs/statement.rb', line 42

def self.convert_json(statement_json)
  statement_hash = JSON.parse(statement_json)
  actor = statement_hash['actor']
  if actor
    identifier = {}
    identifier['account'] = actor.delete('account')
    actor['identifier'] = identifier
  end
  verb = statement_hash['verb']
  if verb
    verb_name = verb['id'].match(%r{\/(\w*)$})
    verb['verb'] = verb_name[1]
  end

  statement_hash['actor'] = actor
  statement_hash['verb'] = verb
  statement_hash
end

.find(id) ⇒ Object



37
38
39
# File 'lib/activity_provider/lrs/statement.rb', line 37

def self.find(id)
  ActivityProvider.fetch_statement(id)
end

Instance Method Details

#as_json(*_args) ⇒ Object



32
33
34
35
# File 'lib/activity_provider/lrs/statement.rb', line 32

def as_json(*_args)
  validate!
  dump_attributes(%i(actor verb object context result))
end

#saveObject



24
25
26
27
28
29
30
# File 'lib/activity_provider/lrs/statement.rb', line 24

def save
  if @id
    ActivityProvider::LRS::Client.new.send_statement(self)
  else
    ActivityProvider::LRS::Client.new.send_statements([self])
  end
end