Class: Xapi::Statements::StatementsBase

Inherits:
Object
  • Object
show all
Defined in:
lib/xapi/statements/statements_base.rb

Direct Known Subclasses

Xapi::Statement, Xapi::SubStatement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ StatementsBase

Returns a new instance of StatementsBase.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/xapi/statements/statements_base.rb', line 8

def initialize(options={}, &block)
  @attachments = []
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    self.actor = attributes['actor']['member'] ? Xapi::Group.new(json: attributes['actor'].to_json) : Xapi::Agent.new(json: attributes['actor'].to_json) if attributes['actor']
    self.verb = Xapi::Verb.new(json: attributes['verb'].to_json) if attributes['verb']
    object_node = attributes['object']
    if object_node
      case object_node['objectType']
        when 'Group', 'Agent'
          self.object = Xapi::Agent.new(json: object_node.to_json)
        when 'StatementRef'
          self.object = Xapi::StatementRef.new(json: object_node.to_json)
        when 'SubStatement'
          self.object = Xapi::SubStatement.new(json: object_node.to_json)
        else
          self.object = Xapi::Activity.new(json: object_node.to_json)
      end
    end
    self.result = Xapi::Result.new(json: attributes['result'].to_json) if attributes['result']
    self.context = Xapi::Context.new(json: attributes['context'].to_json) if attributes['context']
    self.timestamp = Time.parse(attributes['timestamp']) if attributes['timestamp']
    self.voided = attributes['voided'] if attributes['voided']
    if attributes['attachments']
      attributes['attachments'].each do |attachment|
        attachments << Xapi::Attachment.new(json: attachment.to_json)
      end
    end
  else
    self.actor = options.fetch(:actor, nil)
    self.verb = options.fetch(:verb, nil)
    self.object = options.fetch(:object, nil)
    self.result = options.fetch(:result, nil)
    self.context = options.fetch(:context, nil)
    self.timestamp = options.fetch(:timestamp, nil)
    self.attachments = options.fetch(:attachments, nil)
    self.voided = options.fetch(:voided, nil)

    if block_given?
      block[self]
    end
  end
end

Instance Attribute Details

#actorObject

Returns the value of attribute actor.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def actor
  @actor
end

#attachmentsObject

Returns the value of attribute attachments.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def attachments
  @attachments
end

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def context
  @context
end

#objectObject

Returns the value of attribute object.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def object
  @object
end

#resultObject

Returns the value of attribute result.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def result
  @result
end

#timestampObject

Returns the value of attribute timestamp.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def timestamp
  @timestamp
end

#verbObject

Returns the value of attribute verb.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def verb
  @verb
end

#voidedObject

Returns the value of attribute voided.



6
7
8
# File 'lib/xapi/statements/statements_base.rb', line 6

def voided
  @voided
end

Instance Method Details

#serialize(version) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xapi/statements/statements_base.rb', line 53

def serialize(version)
  node = {}
  node['actor'] = actor.serialize(version)
  node['verb'] = verb.serialize(version)
  node['object'] = object.serialize(version)
  node['result'] = result.serialize(version) if result
  node['context'] = context.serialize(version) if context
  node['timestamp'] = timestamp.strftime('%FT%T%:z') if timestamp
  if version.ordinal >= Xapi::TCAPIVersion::V100.ordinal
    if attachments && attachments.any?
      node['attachments'] = attachments.map {|element| element.serialize(version)}
    end
  end
  node
end