diff --git a/app/models/concerns/helpable.rb b/app/models/concerns/helpable.rb index 2417da0b2..621529c29 100644 --- a/app/models/concerns/helpable.rb +++ b/app/models/concerns/helpable.rb @@ -188,5 +188,20 @@ def delete_doi(options={}) response end end + + def parse_attributes(element, options={}) + content = options[:content] || "__content__" + + if element.is_a?(String) && options[:content].nil? + CGI.unescapeHTML(element) + elsif element.is_a?(Hash) + element.fetch( CGI.unescapeHTML(content), nil) + elsif element.is_a?(Array) + a = element.map { |e| e.is_a?(Hash) ? e.fetch( CGI.unescapeHTML(content), nil) : e }.uniq + a = options[:first] ? a.first : a.unwrap + else + nil + end + end end end diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index fafcc44ee..498425c0c 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -634,7 +634,54 @@ end end + describe "convert_containers" do + let(:doi) { create(:doi)} + + context "container nil" do + let(:container) { nil } + let(:doi) { create(:doi, container: container)} + + it "convert" do + expect(Doi.convert_container_by_id(id: doi.id)).to eq(1) + end + end + + context "container hash with strings" do + let(:container) { { + "type": "Journal", + "issue": "6", + "title": "Journal of Crustacean Biology", + "volume": "32", + "lastPage": "961", + "firstPage": "949", + "identifier": "1937-240X", + "identifierType": "ISSN" + } } + let(:doi) { create(:doi, container: container)} + + it "not convert" do + expect(Doi.convert_container_by_id(id: doi.id)).to eq(0) + end + end + context "container hash with hashes" do + let(:container) { { + "type": "Journal", + "issue": { "xmlns:foaf": "http://xmlns.com/foaf/0.1/", "xmlns:rdfs": "http://www.w3.org/2000/01/rdf-schema#", "__content__": "6"}, + "title": { "xmlns:foaf": "http://xmlns.com/foaf/0.1/", "xmlns:rdfs": "http://www.w3.org/2000/01/rdf-schema#", "__content__": "Journal of Crustacean Biology"}, + "volume": { "xmlns:foaf": "http://xmlns.com/foaf/0.1/", "xmlns:rdfs": "http://www.w3.org/2000/01/rdf-schema#", "__content__": "32"}, + "lastPage": "961", + "firstPage": "949", + "identifier": "1937-240X", + "identifierType": "ISSN" + } } + let(:doi) { create(:doi, container: container)} + + it "convert" do + expect(Doi.convert_container_by_id(id: doi.id)).to eq(1) + end + end + end describe "migrates landing page" do let(:provider) { create(:provider, symbol: "ADMIN") }