Class: DIDKit::Document

Inherits:
Object
  • Object
show all
Includes:
AtHandles, Services
Defined in:
lib/didkit/document.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(did, json) ⇒ Document

Returns a new instance of Document.

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/didkit/document.rb', line 16

def initialize(did, json)
  raise FormatError, "Missing id field" if json['id'].nil?
  raise FormatError, "Invalid id field" unless json['id'].is_a?(String)
  raise FormatError, "id field doesn't match expected DID" unless json['id'] == did.to_s

  @did = did
  @json = json

  if service = json['service']
    raise FormatError, "Invalid service data" unless service.is_a?(Array) && service.all? { |x| x.is_a?(Hash) }

    @services = service.filter_map { |x|
      id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')
      next unless id.is_a?(String) && id.start_with?('#') && type.is_a?(String) && endpoint.is_a?(String)

      ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
    }
  else
    @services = []
  end

  @handles = parse_also_known_as(json['alsoKnownAs'] || [])
end

Instance Attribute Details

#didObject (readonly)

Returns the value of attribute did.



14
15
16
# File 'lib/didkit/document.rb', line 14

def did
  @did
end

#handlesObject (readonly)

Returns the value of attribute handles.



14
15
16
# File 'lib/didkit/document.rb', line 14

def handles
  @handles
end

#jsonObject (readonly)

Returns the value of attribute json.



14
15
16
# File 'lib/didkit/document.rb', line 14

def json
  @json
end

#servicesObject (readonly)

Returns the value of attribute services.



14
15
16
# File 'lib/didkit/document.rb', line 14

def services
  @services
end

Instance Method Details

#get_validated_handleObject



40
41
42
# File 'lib/didkit/document.rb', line 40

def get_validated_handle
  Resolver.new.pick_valid_handle(did, handles)
end