Skip to content

Commit

Permalink
streamline field access in graphql, using mash
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jul 21, 2019
1 parent dd37c03 commit b4bf353
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 67 deletions.
10 changes: 9 additions & 1 deletion app/graphql/types/doi_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 1 addition & 9 deletions app/graphql/types/organization_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions app/graphql/types/researcher_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/models/funder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 6 additions & 6 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"],
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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)
Expand Down
20 changes: 16 additions & 4 deletions spec/models/funder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,23 +25,32 @@
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
query = nil
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
query = "dfg"
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
Expand Down
18 changes: 15 additions & 3 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,15 +27,23 @@
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
query = "lincoln university"
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
Expand Down
60 changes: 30 additions & 30 deletions spec/models/repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -42,47 +42,47 @@
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
repositories = Repository.query(nil, pid: true)
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
repositories = Repository.query(nil, certified: true)
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
repositories = Repository.query(nil, open: true)
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
Expand Down

0 comments on commit b4bf353

Please sign in to comment.