Class: Xapi::Result

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

Overview

Result Model class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Result.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xapi/result.rb', line 10

def initialize(options={}, &block)
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    self.score = Xapi::Score.new(json: attributes['score'].to_json) if attributes['score']
    self.success = attributes['success'] unless attributes['success'].nil?
    self.completion = attributes['completion'] unless attributes['completion'].nil?
    self.duration = ActiveSupport::Duration.parse(attributes['duration']) if attributes['duration']
    self.response = attributes['response'] if attributes['response']
    self.extensions = attributes['extensions'] if attributes['extensions']
  else
    self.score = options.fetch(:score, nil)
    self.success = options.fetch(:success, nil)
    self.completion = options.fetch(:completion, nil)
    self.duration = options.fetch(:duration, nil)
    self.response = options.fetch(:response, nil)
    self.extensions = options.fetch(:extensions, nil)

    if block_given?
      block[self]
    end
  end
end

Instance Attribute Details

#completionObject

Returns the value of attribute completion.



8
9
10
# File 'lib/xapi/result.rb', line 8

def completion
  @completion
end

#durationObject

Returns the value of attribute duration.



8
9
10
# File 'lib/xapi/result.rb', line 8

def duration
  @duration
end

#extensionsObject

Returns the value of attribute extensions.



8
9
10
# File 'lib/xapi/result.rb', line 8

def extensions
  @extensions
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/xapi/result.rb', line 8

def response
  @response
end

#scoreObject

Returns the value of attribute score.



8
9
10
# File 'lib/xapi/result.rb', line 8

def score
  @score
end

#successObject

Returns the value of attribute success.



8
9
10
# File 'lib/xapi/result.rb', line 8

def success
  @success
end

Instance Method Details

#serialize(version) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xapi/result.rb', line 34

def serialize(version)
  node = {}
  node['score'] = score.serialize(version) if score
  node['success'] = success unless success.nil?
  node['completion'] = completion unless completion.nil?
  node['duration'] = duration.iso8601 if duration
  node['response'] = response if response
  node['extensions'] = extensions if extensions

  node
end