Class: TinCanApi::Statement

Inherits:
TinCanApi::Statements::StatementsBase show all
Defined in:
lib/tin_can_api/statement.rb

Overview

Statement Class

Instance Attribute Summary collapse

Attributes inherited from TinCanApi::Statements::StatementsBase

#actor, #attachments, #context, #object, #result, #timestamp, #verb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Statement.



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

def initialize(options={}, &block)
  super(options, &block)
  json = options.fetch(:json, nil)
  if json
    attributes = JSON.parse(json)
    self.id = attributes['id'] if attributes['id']
    self.stored =  Time.parse(attributes['stored']) if attributes['stored']
    self.authority = TinCanApi::Agent.new(json: attributes['authority'].to_json) if attributes['authority']
    self.version = attributes['version'] if attributes['version']
    self.voided = attributes['voided'] if attributes['voided']
  else
    self.id = options.fetch(:id, nil)
    self.stored = options.fetch(:stored, nil)
    self.authority = options.fetch(:authority, nil)
    self.version = options.fetch(:version, nil)
    self.voided = options.fetch(:voided, nil)

    if block_given?
      block[self]
    end
  end
end

Instance Attribute Details

#authorityObject

Returns the value of attribute authority.



6
7
8
# File 'lib/tin_can_api/statement.rb', line 6

def authority
  @authority
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/tin_can_api/statement.rb', line 6

def id
  @id
end

#storedObject

Returns the value of attribute stored.



6
7
8
# File 'lib/tin_can_api/statement.rb', line 6

def stored
  @stored
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/tin_can_api/statement.rb', line 6

def version
  @version
end

#voidedObject

Returns the value of attribute voided.



6
7
8
# File 'lib/tin_can_api/statement.rb', line 6

def voided
  @voided
end

Instance Method Details

#serialize(version) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tin_can_api/statement.rb', line 31

def serialize(version)
  node = super(version)

  node['id'] = id if id
  node['stored'] = stored.strftime('%FT%T%:z') if stored
  node['authority'] = authority.serialize(version) if authority

  if version == TinCanApi::TCAPIVersion::V095
    node['voided'] = voided if voided
  end

  if version.ordinal <= TinCanApi::TCAPIVersion::V100.ordinal
    node['version'] = version.to_s if version
  end
  node
end

#stampObject



48
49
50
51
# File 'lib/tin_can_api/statement.rb', line 48

def stamp
  @id = SecureRandom.uuid
  @timestamp = Time.now
end