Class: DIDKit::PLCOperation

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

Overview

Represents a single operation of changing a specific DID’s data in the [plc.directory](plc.directory) (e.g. changing assigned handles or migrating to a different PDS).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Services

#get_service, #labeler_endpoint, #labeler_host, #pds_endpoint, #pds_host

Constructor Details

#initialize(json) ⇒ PLCOperation

Creates a PLCOperation object.

Parameters:

  • json (Hash)

    operation JSON

Raises:

  • (FormatError)

    when required fields are missing or invalid



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/didkit/plc_operation.rb', line 59

def initialize(json)
  @json = json
  raise FormatError, "Expected argument to be a Hash, got a #{json.class}" unless @json.is_a?(Hash)

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

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

  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?
  raise FormatError, "Invalid operation type: #{type.inspect}" unless type.is_a?(String)

  @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.inspect}" 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

#cidString (readonly)

Returns CID (Content Identifier) of the operation.

Returns:

  • (String)

    CID (Content Identifier) of the operation



28
29
30
# File 'lib/didkit/plc_operation.rb', line 28

def cid
  @cid
end

#created_atTime (readonly)

Returns time when the operation was created.

Returns:

  • (Time)

    time when the operation was created



35
36
37
# File 'lib/didkit/plc_operation.rb', line 35

def created_at
  @created_at
end

#didString (readonly)

Returns the DID which the operation concerns.

Returns:

  • (String)

    the DID which the operation concerns



25
26
27
# File 'lib/didkit/plc_operation.rb', line 25

def did
  @did
end

#handlesArray<String> (readonly)

Returns a list of handles assigned to the DID in this operation.

Note: the handles aren’t guaranteed to be verified (validated in the other direction). Use DID#get_verified_handle or Document#get_verified_handle to find a handle that is correctly verified.

Returns:

  • (Array<String>)


48
49
50
# File 'lib/didkit/plc_operation.rb', line 48

def handles
  @handles
end

#jsonHash (readonly)

Returns the JSON from which the operation is parsed.

Returns:

  • (Hash)

    the JSON from which the operation is parsed



22
23
24
# File 'lib/didkit/plc_operation.rb', line 22

def json
  @json
end

#seqInteger? (readonly)

Returns a sequential number of the operation (only used in the new export API).

Returns:

  • (Integer, nil)

    sequential number of the operation



32
33
34
# File 'lib/didkit/plc_operation.rb', line 32

def seq
  @seq
end

#servicesArray<ServiceRecords> (readonly)

Returns service records like PDS details assigned to the DID.

Returns:

  • (Array<ServiceRecords>)

    service records like PDS details assigned to the DID



51
52
53
# File 'lib/didkit/plc_operation.rb', line 51

def services
  @services
end

#typeString (readonly)

Returns the type field of the operation (usually ‘“plc_operation”`).

Returns:

  • (String)

    the operation type



39
40
41
# File 'lib/didkit/plc_operation.rb', line 39

def type
  @type
end