···71 def parse_services(service_data)
72 raise FormatError, "Invalid service data" unless service_data.is_a?(Array) && service_data.all? { |x| x.is_a?(Hash) }
7374- service_data.map do |x|
0075 id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')
7677 if id.is_a?(String) && id.start_with?('#') && type.is_a?(String) && endpoint.is_a?(String)
78- ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
79- else
80- raise FormatError, "Invalid service data"
81 end
82 end
0083 end
84 end
85end
···71 def parse_services(service_data)
72 raise FormatError, "Invalid service data" unless service_data.is_a?(Array) && service_data.all? { |x| x.is_a?(Hash) }
7374+ services = []
75+76+ service_data.each do |x|
77 id, type, endpoint = x.values_at('id', 'type', 'serviceEndpoint')
7879 if id.is_a?(String) && id.start_with?('#') && type.is_a?(String) && endpoint.is_a?(String)
80+ services << ServiceRecord.new(id.gsub(/^#/, ''), type, endpoint)
0081 end
82 end
83+84+ services
85 end
86 end
87end
+8-5
spec/document_spec.rb
···120 end
121 end
122123- context 'when some service entries are not valid' do
124 let(:services) {
125 [
126 { 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.dholms.xyz' },
···133134 let(:json) { base_json.merge('service' => services) }
135136- it 'should raise a format error' do
137- expect {
138- subject.new(did, json)
139- }.to raise_error(DIDKit::FormatError)
000140 end
141 end
142 end
···120 end
121 end
122123+ context 'when service entries are partially valid' do
124 let(:services) {
125 [
126 { 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.dholms.xyz' },
···133134 let(:json) { base_json.merge('service' => services) }
135136+ 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']
143 end
144 end
145 end