Class: TinCanApi::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/tin_can_api/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.



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

def initialize(options={}, &block)
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    self.score = TinCanApi::Score.new(json: attributes['score'].to_json) if attributes['score']
    self.success = attributes['success'] if attributes['success']
    self.completion = attributes['completion'] if attributes['completion']
    self.duration = Duration.new(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.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def completion
  @completion
end

#durationObject

Returns the value of attribute duration.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def duration
  @duration
end

#extensionsObject

Returns the value of attribute extensions.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def extensions
  @extensions
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def response
  @response
end

#scoreObject

Returns the value of attribute score.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def score
  @score
end

#successObject

Returns the value of attribute success.



7
8
9
# File 'lib/tin_can_api/result.rb', line 7

def success
  @success
end

Instance Method Details

#serialize(version) ⇒ Object



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

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

  node
end