A library for handling DID identifiers used in Bluesky AT Protocol

minor tweaks to operation/document parsing

+19 -15
+4 -6
lib/didkit/document.rb
··· 71 71 def parse_services(service_data) 72 72 raise FormatError, "Invalid service data" unless service_data.is_a?(Array) && service_data.all? { |x| x.is_a?(Hash) } 73 73 74 - services = [] 75 - 76 - service_data.each do |x| 74 + service_data.map do |x| 77 75 id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint') 78 76 79 77 if id.is_a?(String) && id.start_with?('#') && type.is_a?(String) && endpoint.is_a?(String) 80 - services << ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint) 78 + ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint) 79 + else 80 + raise FormatError, "Invalid service data" 81 81 end 82 82 end 83 - 84 - services 85 83 end 86 84 end 87 85 end
+2 -1
lib/didkit/plc_operation.rb
··· 81 81 82 82 type = operation['type'] 83 83 raise FormatError, "Missing operation type: #{json}" if type.nil? 84 + raise FormatError, "Invalid operation type: #{type.inspect}" unless type.is_a?(String) 84 85 85 86 @type = type.to_sym 86 87 return unless @type == :plc_operation 87 88 88 89 services = operation['services'] 89 90 raise FormatError, "Missing services key: #{json}" if services.nil? 90 - raise FormatError, "Invalid services data: #{services}" unless services.is_a?(Hash) 91 + raise FormatError, "Invalid services data: #{services.inspect}" unless services.is_a?(Hash) 91 92 92 93 @services = services.map { |k, x| 93 94 type, endpoint = x.values_at('type', 'endpoint')
+5 -8
spec/document_spec.rb
··· 120 120 end 121 121 end 122 122 123 - context 'when service entries are partially valid' do 123 + context 'when some service entries are not valid' do 124 124 let(:services) { 125 125 [ 126 126 { 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.dholms.xyz' }, ··· 133 133 134 134 let(:json) { base_json.merge('service' => services) } 135 135 136 - it 'should only keep the valid records' do 137 - doc = subject.new(did, json) 138 - 139 - doc.services.length.should == 2 140 - doc.services.map(&:key).should == ['atproto_pds', 'lycan'] 141 - doc.services.map(&:type).should == ['AtprotoPersonalDataServer', 'LycanService'] 142 - doc.services.map(&:endpoint).should == ['https://pds.dholms.xyz', 'https://lycan.feeds.blue'] 136 + it 'should raise a format error' do 137 + expect { 138 + subject.new(did, json) 139 + }.to raise_error(DIDKit::FormatError) 143 140 end 144 141 end 145 142 end
+8
spec/plc_operation_spec.rb
··· 111 111 end 112 112 end 113 113 114 + context 'when operation type is not a string' do 115 + let(:json) { base_json.tap { |h| h['operation']['type'] = 5 }} 116 + 117 + it 'should raise a format error' do 118 + expect { subject.new(json) }.to raise_error(DIDKit::FormatError) 119 + end 120 + end 121 + 114 122 context 'when operation type is not plc_operation' do 115 123 let(:json) { base_json.tap { |h| h['operation']['type'] = 'other' }} 116 124