Skip to content

Commit

Permalink
refactored alsoKnownAs parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mackuba committed Mar 18, 2024
1 parent ca39298 commit 6195579
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
14 changes: 14 additions & 0 deletions lib/didkit/at_handles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module DIDKit
module AtHandles
class FormatError < StandardError
end

def parse_also_known_as(aka)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }

aka.map { |x| x.gsub('at://', '') }
end
end
end
12 changes: 3 additions & 9 deletions lib/didkit/document.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require_relative 'at_handles'
require_relative 'resolver'
require_relative 'service_record'
require_relative 'services'
Expand All @@ -7,6 +8,7 @@ class Document
class FormatError < StandardError
end

include AtHandles
include Services

attr_reader :json, :did, :handles, :services
Expand Down Expand Up @@ -36,15 +38,7 @@ def initialize(did, json)
ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
}

if aka = json['alsoKnownAs']
raise FormatError, "Invalid alsoKnownAs" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }

@handles = aka.map { |x| x.gsub('at://', '') }
else
@handles = []
end
@handles = parse_also_known_as(json['alsoKnownAs'] || [])
end

def get_validated_handle
Expand Down
12 changes: 3 additions & 9 deletions lib/didkit/plc_operation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'time'

require_relative 'at_handles'
require_relative 'service_record'
require_relative 'services'

Expand All @@ -8,6 +9,7 @@ class PLCOperation
class FormatError < StandardError
end

include AtHandles
include Services

attr_reader :json, :did, :created_at, :type, :handles, :services
Expand Down Expand Up @@ -49,15 +51,7 @@ def initialize(json)
ServiceRecord.new(k, type, endpoint)
}

if aka = operation['alsoKnownAs']
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.is_a?(Array)
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x.is_a?(String) }
raise FormatError, "Invalid alsoKnownAs: #{aka.inspect}" unless aka.all? { |x| x =~ %r(\Aat://[^/]+\z) }

@handles = aka.map { |x| x.gsub('at://', '') }
else
@handles = []
end
@handles = parse_also_known_as(operation['alsoKnownAs'] || [])
end
end
end

0 comments on commit 6195579

Please sign in to comment.