From b4bf353ba91ca16e02a2f1bcff8c93f4cf0b717e Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Sun, 21 Jul 2019 13:27:18 +0200 Subject: [PATCH] streamline field access in graphql, using mash --- app/graphql/types/doi_item.rb | 10 ++++- app/graphql/types/organization_type.rb | 10 +---- app/graphql/types/researcher_type.rb | 18 ++++---- app/models/funder.rb | 4 +- app/models/organization.rb | 12 +++--- app/models/repository.rb | 4 +- spec/models/funder_spec.rb | 20 +++++++-- spec/models/organization_spec.rb | 18 ++++++-- spec/models/repository_spec.rb | 60 +++++++++++++------------- 9 files changed, 89 insertions(+), 67 deletions(-) diff --git a/app/graphql/types/doi_item.rb b/app/graphql/types/doi_item.rb index aff2da8ea..3b2bc3513 100644 --- a/app/graphql/types/doi_item.rb +++ b/app/graphql/types/doi_item.rb @@ -34,7 +34,15 @@ module DoiItem field :provider, ProviderType, null: true, description: "The provider account managing this resource" def creators(first: nil) - object.creators[0...first] + Array.wrap(object.creators[0...first]).map do |c| + Hashie::Mash.new( + "id" => c.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil), + "name_type" => c.fetch("nameType", nil), + "name" => c.fetch("name", nil), + "given_name" => c.fetch("givenName", nil), + "family_name" => c.fetch("familyName", nil), + "affiliation" => c.fetch("affiliation", [])) + end end def titles(first: nil) diff --git a/app/graphql/types/organization_type.rb b/app/graphql/types/organization_type.rb index 6b3f94c4a..13c5b1a54 100644 --- a/app/graphql/types/organization_type.rb +++ b/app/graphql/types/organization_type.rb @@ -14,7 +14,7 @@ class OrganizationType < BaseObject field :isni, [String], null: true, description: "ISNI identifiers for organization" field :fund_ref, [String], null: true, description: "Crossref Funder ID identifiers for organization" field :wikidata, [String], null: true, description: "Wikidata identifiers for organization" - field :grid, [String], null: true, description: "GRID identifiers for organization" + field :grid, String, null: true, description: "GRID identifiers for organization" field :datasets, OrganizationDatasetConnectionWithMetaType, null: false, description: "Datasets from this organization", connection: true, max_page_size: 100 do argument :first, Int, required: false, default_value: 25 @@ -29,14 +29,6 @@ class OrganizationType < BaseObject argument :first, Int, required: false, default_value: 25 end - def id - object.id || object.fetch("affiliationIdentifier", nil) - end - - def name - object.name || object.fetch("name", nil) - end - def datasets(**args) ids = Event.query(nil, obj_id: object[:id], citation_type: "Dataset-Organization").results.to_a.map do |e| doi_from_url(e.subj_id) diff --git a/app/graphql/types/researcher_type.rb b/app/graphql/types/researcher_type.rb index 873ee29eb..87dd6960b 100644 --- a/app/graphql/types/researcher_type.rb +++ b/app/graphql/types/researcher_type.rb @@ -22,37 +22,35 @@ class ResearcherType < BaseObject end def id - object.uid ? "https://orcid.org/#{object.uid}" : nil || object.fetch(:id, nil) || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil) + object.uid ? "https://orcid.org/#{object.uid}" : object.id end def name object.name || object.fetch("name", nil) end - def given_name - object.given_names || object.fetch("givenName", nil) - end - - def family_name - object.family_name || object.fetch("familyName", nil) + def affiliation + Array.wrap(object.fetch("affiliation", nil)).map do |a| + Hashie::Mash.new(a.merge("id" => a["affiliationIdentifier"]).except("affiliationIdentifier", "affiliationIdentifierScheme", "schemeUri")) + end end def datasets(**args) - ids = Event.query(nil, obj_id: https_to_http(object.uid || object[:id] || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil)), citation_type: "Dataset-Person").results.to_a.map do |e| + ids = Event.query(nil, obj_id: https_to_http(object.uid || object.id), citation_type: "Dataset-Person").results.to_a.map do |e| doi_from_url(e.subj_id) end ElasticsearchLoader.for(Doi).load_many(ids) end def publications(**args) - ids = Event.query(nil, obj_id: https_to_http(object.uid || object[:id] || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil)), citation_type: "Person-ScholarlyArticle").results.to_a.map do |e| + ids = Event.query(nil, obj_id: https_to_http(object.uid || object.id), citation_type: "Person-ScholarlyArticle").results.to_a.map do |e| doi_from_url(e.subj_id) end ElasticsearchLoader.for(Doi).load_many(ids) end def softwares(**args) - ids = Event.query(nil, obj_id: https_to_http(object.uid || object[:id] || object.fetch("nameIdentifiers", []).find { |n| n.fetch("nameIdentifierScheme", nil) == "ORCID" }.to_h.fetch("nameIdentifier", nil)), citation_type: "Person-SoftwareSourceCode").results.to_a.map do |e| + ids = Event.query(nil, obj_id: https_to_http(object.uid || object.id), citation_type: "Person-SoftwareSourceCode").results.to_a.map do |e| doi_from_url(e.subj_id) end ElasticsearchLoader.for(Doi).load_many(ids) diff --git a/app/models/funder.rb b/app/models/funder.rb index 3fc01a689..4977978a4 100644 --- a/app/models/funder.rb +++ b/app/models/funder.rb @@ -53,11 +53,11 @@ def self.parse_message(id: nil, message: nil) country = nil end - { + Hashie::Mash.new({ id: id, name: message["name"], alternate_name: message["alt-names"], country: country, - date_modified: "2019-04-18T00:00:00Z" }.compact + date_modified: "2019-04-18T00:00:00Z" }) end end diff --git a/app/models/organization.rb b/app/models/organization.rb index 11e18eb63..78754df20 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -20,7 +20,7 @@ def self.query(query, options={}) # rows = options[:limit] || 25 if query.present? - url = "https://api.ror.org/organizations?query=#{query}&qp" + url = "https://api.ror.org/organizations?query=#{query}" else url = "https://api.ror.org/organizations" end @@ -50,7 +50,7 @@ def self.parse_message(id: nil, message: nil) name: label["label"] }.compact end - { + Hashie::Mash.new({ id: id, name: message["name"], aliases: message["aliases"], @@ -59,10 +59,10 @@ def self.parse_message(id: nil, message: nil) links: message["links"], wikipedia_url: message["wikipedia_url"], country: country, - isni: message.dig("ISNI", "all"), - fund_ref: message.dig("FundRef", "all"), - wikidata: message.dig("Wikidata", "all"), - grid: message.dig("GRID", "all") }.compact + isni: message.dig("external_ids", "ISNI", "all"), + fund_ref: message.dig("external_ids", "FundRef", "all"), + wikidata: message.dig("external_ids", "Wikidata", "all"), + grid: message.dig("external_ids", "GRID", "all") }) end def self.ror_id_from_url(url) diff --git a/app/models/repository.rb b/app/models/repository.rb index 38d25c13b..c61ece890 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -47,7 +47,7 @@ def self.query(query, options={}) end def self.parse_message(id: nil, message: nil) - { + Hashie::Mash.new({ id: id, re3data_id: message["re3dataId"], name: message["repositoryName"], @@ -65,7 +65,7 @@ def self.parse_message(id: nil, message: nil) data_uploads: message["dataUploads"], pid_systems: message["pidSystems"], apis: message["apis"], - software: message["software"] }.compact + software: message["software"] }) end def self.doi_from_url(url) diff --git a/spec/models/funder_spec.rb b/spec/models/funder_spec.rb index 8e7616631..19730906d 100644 --- a/spec/models/funder_spec.rb +++ b/spec/models/funder_spec.rb @@ -6,7 +6,10 @@ id = "https://doi.org/10.13039/100011326" funders = Funder.find_by_id(id) expect(funders[:data].size).to eq(1) - expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100011326", name: "London School of Economics and Political Science", alternate_name: ["London School of Economics & Political Science", "LSE"], date_modified: "2019-04-18T00:00:00Z") + funder = funders[:data].first + expect(funder.id).to eq("https://doi.org/10.13039/100011326") + expect(funder.name).to eq("London School of Economics and Political Science") + expect(funder.alternate_name).to eq(["London School of Economics & Political Science", "LSE"]) end it "not found" do @@ -22,7 +25,10 @@ funders = Funder.query(query) expect(funders.dig(:meta, "total")).to eq(19662) expect(funders[:data].size).to eq(100) - expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: {"code"=>"US", "name"=>"United States"}, date_modified: "2019-04-18T00:00:00Z") + funder = funders[:data].first + expect(funder.id).to eq("https://doi.org/10.13039/100002569") + expect(funder.name).to eq("American Association of Endodontists Foundation") + expect(funder.alternate_name).to eq(["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"]) end it "limit" do @@ -30,7 +36,10 @@ funders = Funder.query(query, limit: 10) expect(funders.dig(:meta, "total")).to eq(19662) expect(funders[:data].size).to eq(10) - expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/100002569", name: "American Association of Endodontists Foundation", alternate_name: ["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"], country: {"code"=>"US", "name"=>"United States"}, date_modified: "2019-04-18T00:00:00Z") + funder = funders[:data].first + expect(funder.id).to eq("https://doi.org/10.13039/100002569") + expect(funder.name).to eq("American Association of Endodontists Foundation") + expect(funder.alternate_name).to eq(["AAE Foundation for Endodontics", "AAE Foundation", "Foundation for Endodontics", "AAEF"]) end it "found" do @@ -38,7 +47,10 @@ funders = Funder.query(query) expect(funders.dig(:meta, "total")).to eq(3) expect(funders[:data].size).to eq(3) - expect(funders[:data].first).to eq(id: "https://doi.org/10.13039/501100001659", name: "Deutsche Forschungsgemeinschaft", alternate_name: ["DFG", "German Research Association", "German Research Foundation"], date_modified: "2019-04-18T00:00:00Z") + funder = funders[:data].first + expect(funder.id).to eq("https://doi.org/10.13039/501100001659") + expect(funder.name).to eq("Deutsche Forschungsgemeinschaft") + expect(funder.alternate_name).to eq(["DFG", "German Research Association", "German Research Foundation"]) end it "not found" do diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb index 752111040..1912a1a96 100644 --- a/spec/models/organization_spec.rb +++ b/spec/models/organization_spec.rb @@ -6,7 +6,11 @@ id = "https://ror.org/0521rfb23" organizations = Organization.find_by_id(id) expect(organizations[:data].size).to eq(1) - expect(organizations[:data].first).to eq(:id=>"https://ror.org/0521rfb23", :name=>"Lincoln University", :aliases=>["Ashmun Institute"], :acronyms=>["LU"], :labels=>[{:code=>"ES", :name=>"Universidad Lincoln"}], :links=>["http://www.lincoln.edu/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(Pennsylvania)", :country=>{:code=>"US", :name=>"United States"}) + organization = organizations[:data].first + expect(organization.id).to eq("https://ror.org/0521rfb23") + expect(organization.name).to eq("Lincoln University") + expect(organization.labels).to eq([{"code"=>"ES", "name"=>"Universidad Lincoln"}]) + expect(organization.links).to eq(["http://www.lincoln.edu/"]) end it "not found" do @@ -23,7 +27,11 @@ organizations = Organization.query(query) expect(organizations.dig(:meta, "total")).to eq(91625) expect(organizations[:data].size).to eq(20) - expect(organizations[:data].first).to eq(:id=>"https://ror.org/00xqf8t64", :name=>"Padjadjaran University", :aliases=>["Padjadjaran University"], :acronyms=>["UNPAD"], :labels=> [{:code=>"ID", :name=>"Universitas Padjadjaran"}], :links=>["http://www.unpad.ac.id/en/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Padjadjaran_University", :country=>{:code=>"ID", :name=>"Indonesia"}) + organization = organizations[:data].first + expect(organization.id).to eq("https://ror.org/00xqf8t64") + expect(organization.name).to eq("Padjadjaran University") + expect(organization.labels).to eq([{"code"=>"ID", "name"=>"Universitas Padjadjaran"}]) + expect(organization.links).to eq(["http://www.unpad.ac.id/en/"]) end it "found" do @@ -31,7 +39,11 @@ organizations = Organization.query(query) expect(organizations.dig(:meta, "total")).to eq(10475) expect(organizations[:data].size).to eq(20) - expect(organizations[:data].first).to eq(:id=>"https://ror.org/04ps1r162", :name=>"Lincoln University", :aliases=>[], :acronyms=>[], :labels=>[{:code=>"MI", :name=>"Te Whare Wanaka o Aoraki"}], :links=>["http://www.lincoln.ac.nz/"], :wikipedia_url=>"http://en.wikipedia.org/wiki/Lincoln_University_(New_Zealand)", :country=>{:code=>"NZ", :name=>"New Zealand"}) + organization = organizations[:data].first + expect(organization.id).to eq("https://ror.org/04ps1r162") + expect(organization.name).to eq("Lincoln University") + expect(organization.labels).to eq([{"code"=>"MI", "name"=>"Te Whare Wanaka o Aoraki"}]) + expect(organization.links).to eq(["http://www.lincoln.ac.nz/"]) end it "not found" do diff --git a/spec/models/repository_spec.rb b/spec/models/repository_spec.rb index 02ff04d64..fc1e40f65 100644 --- a/spec/models/repository_spec.rb +++ b/spec/models/repository_spec.rb @@ -7,11 +7,11 @@ repositories = Repository.find_by_id(id) expect(repositories[:data].size).to eq(1) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3qp53") - expect(repository[:re3data_id]).to eq("r3d100010468") - expect(repository[:name]).to eq("Zenodo") - expect(repository[:url]).to eq("https://zenodo.org/") - expect(repository[:certificates]).to eq([]) + expect(repository.id).to eq("https://doi.org/10.17616/r3qp53") + expect(repository.re3data_id).to eq("r3d100010468") + expect(repository.name).to eq("Zenodo") + expect(repository.url).to eq("https://zenodo.org/") + expect(repository.certificates).to eq([]) end it "not found" do @@ -29,11 +29,11 @@ expect(repositories.dig(:meta, "total")).to eq(1562) expect(repositories[:data].size).to eq(25) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3w05r") - expect(repository[:re3data_id]).to eq("r3d100011565") - expect(repository[:name]).to eq("1000 Functional Connectomes Project") - expect(repository[:url]).to eq("http://fcon_1000.projects.nitrc.org/fcpClassic/FcpTable.html") - expect(repository[:certificates]).to eq([]) + expect(repository.id).to eq("https://doi.org/10.17616/r3w05r") + expect(repository.re3data_id).to eq("r3d100011565") + expect(repository.name).to eq("1000 Functional Connectomes Project") + expect(repository.url).to eq("http://fcon_1000.projects.nitrc.org/fcpClassic/FcpTable.html") + expect(repository.certificates).to eq([]) end it "found" do @@ -42,11 +42,11 @@ expect(repositories.dig(:meta, "total")).to eq(167) expect(repositories[:data].size).to eq(25) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3qd26") - expect(repository[:re3data_id]).to eq("r3d100011691") - expect(repository[:name]).to eq("ACTRIS Data Centre") - expect(repository[:url]).to eq("http://actris.nilu.no/") - expect(repository[:certificates]).to eq([]) + expect(repository.id).to eq("https://doi.org/10.17616/r3qd26") + expect(repository.re3data_id).to eq("r3d100011691") + expect(repository.name).to eq("ACTRIS Data Centre") + expect(repository.url).to eq("http://actris.nilu.no/") + expect(repository.certificates).to eq([]) end it "pid" do @@ -54,11 +54,11 @@ expect(repositories.dig(:meta, "total")).to eq(651) expect(repositories[:data].size).to eq(25) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3vg6n") - expect(repository[:re3data_id]).to eq("r3d100010216") - expect(repository[:name]).to eq("4TU.Centre for Research Data") - expect(repository[:url]).to eq("https://researchdata.4tu.nl/en/home/") - expect(repository[:pid_systems]).to eq([{"text"=>"DOI"}]) + expect(repository.id).to eq("https://doi.org/10.17616/r3vg6n") + expect(repository.re3data_id).to eq("r3d100010216") + expect(repository.name).to eq("4TU.Centre for Research Data") + expect(repository.url).to eq("https://researchdata.4tu.nl/en/home/") + expect(repository.pid_systems).to eq([{"text"=>"DOI"}]) end it "certified" do @@ -66,11 +66,11 @@ expect(repositories.dig(:meta, "total")).to eq(154) expect(repositories[:data].size).to eq(25) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3vg6n") - expect(repository[:re3data_id]).to eq("r3d100010216") - expect(repository[:name]).to eq("4TU.Centre for Research Data") - expect(repository[:url]).to eq("https://researchdata.4tu.nl/en/home/") - expect(repository[:certificates]).to eq([{"text"=>"DSA"}]) + expect(repository.id).to eq("https://doi.org/10.17616/r3vg6n") + expect(repository.re3data_id).to eq("r3d100010216") + expect(repository.name).to eq("4TU.Centre for Research Data") + expect(repository.url).to eq("https://researchdata.4tu.nl/en/home/") + expect(repository.certificates).to eq([{"text"=>"DSA"}]) end it "open" do @@ -78,11 +78,11 @@ expect(repositories.dig(:meta, "total")).to eq(1374) expect(repositories[:data].size).to eq(25) repository = repositories[:data].first - expect(repository[:id]).to eq("https://doi.org/10.17616/r3w05r") - expect(repository[:re3data_id]).to eq("r3d100011565") - expect(repository[:name]).to eq("1000 Functional Connectomes Project") - expect(repository[:url]).to eq("http://fcon_1000.projects.nitrc.org/fcpClassic/FcpTable.html") - expect(repository[:data_accesses]).to eq([{"restrictions"=>[], "type"=>"open"}]) + expect(repository.id).to eq("https://doi.org/10.17616/r3w05r") + expect(repository.re3data_id).to eq("r3d100011565") + expect(repository.name).to eq("1000 Functional Connectomes Project") + expect(repository.url).to eq("http://fcon_1000.projects.nitrc.org/fcpClassic/FcpTable.html") + expect(repository.data_accesses).to eq([{"restrictions"=>[], "type"=>"open"}]) end it "not found" do