Class: DIDKit::PLCOperation

Inherits:
Object
  • Object
show all
Includes:
AtHandles, Services
Defined in:
lib/didkit/plc_operation.rb

Defined Under Namespace

Classes: FormatError

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services

#get_service, #labeler_endpoint, #pds_endpoint

Methods included from AtHandles

#parse_also_known_as

Constructor Details

#initialize(json) ⇒ PLCOperation

Returns a new instance of PLCOperation.

Raises:



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
52
53
54
55
# File 'lib/didkit/plc_operation.rb', line 17

def initialize(json)
  @json = json
  @did = json['did']
  raise FormatError, "Missing DID: #{json}" if @did.nil?
  raise FormatError, "Invalid DID: #{@did}" unless @did.is_a?(String) && @did.start_with?('did:')

  timestamp = json['createdAt']
  raise FormatError, "Missing createdAt: #{json}" if timestamp.nil?
  raise FormatError, "Invalid createdAt: #{timestamp.inspect}" unless timestamp.is_a?(String)

  @created_at = Time.parse(timestamp)

  operation = json['operation']
  raise FormatError, "Missing operation key: #{json}" if operation.nil?
  raise FormatError, "Invalid operation data: #{operation.inspect}" unless operation.is_a?(Hash)

  type = operation['type']
  raise FormatError, "Missing operation type: #{json}" if type.nil?

  @type = type.to_sym
  return unless @type == :plc_operation

  services = operation['services']
  raise FormatError, "Missing services key: #{json}" if services.nil?
  raise FormatError, "Invalid services data: #{services}" unless services.is_a?(Hash)

  @services = services.map { |k, x|
    type, endpoint = x.values_at('type', 'endpoint')

    raise FormatError, "Missing service type" unless type
    raise FormatError, "Invalid service type: #{type.inspect}" unless type.is_a?(String)
    raise FormatError, "Missing service endpoint" unless endpoint
    raise FormatError, "Invalid service endpoint: #{endpoint.inspect}" unless endpoint.is_a?(String)

    ServiceRecord.new(k, type, endpoint)
  }

  @handles = parse_also_known_as(operation['alsoKnownAs'])
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def created_at
  @created_at
end

#didObject (readonly)

Returns the value of attribute did.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def did
  @did
end

#handlesObject (readonly)

Returns the value of attribute handles.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def handles
  @handles
end

#jsonObject (readonly)

Returns the value of attribute json.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def json
  @json
end

#servicesObject (readonly)

Returns the value of attribute services.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def services
  @services
end

#typeObject (readonly)

Returns the value of attribute type.



15
16
17
# File 'lib/didkit/plc_operation.rb', line 15

def type
  @type
end