Skip to content

Commit

Permalink
added field for doi metadata in bibtex format
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 11, 2020
1 parent 79bd25f commit 06c2282
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
100 changes: 100 additions & 0 deletions app/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ type Affiliation {
}

type Audiovisual implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -480,6 +485,11 @@ type AudiovisualEdge {
}

type Collection implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -1135,6 +1145,11 @@ type DataCatalogEdge {
}

type DataPaper implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -1566,6 +1581,11 @@ type DataPaperEdge {
}

type Dataset implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -2111,6 +2131,11 @@ type Description {
Information about DOIs
"""
interface DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -2505,6 +2530,11 @@ interface DoiItem {
}

type Event implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -2921,6 +2951,11 @@ type EventConnection {
}

type EventData implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -3670,6 +3705,11 @@ type Identifier {
}

type Image implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -4101,6 +4141,11 @@ type ImageEdge {
}

type Instrument implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -4532,6 +4577,11 @@ type InstrumentEdge {
}

type InteractiveResource implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -5358,6 +5408,11 @@ type MemberPrefixEdge {
}

type Model implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -6009,6 +6064,11 @@ type OrganizationEdge {
}

type Other implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -6691,6 +6751,11 @@ type PersonEdge {
}

type PhysicalObject implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -7175,6 +7240,11 @@ type PrefixEdge {
}

type Publication implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -8778,6 +8848,11 @@ type Rights {
}

type Service implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -9209,6 +9284,11 @@ type ServiceEdge {
}

type Software implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -9672,6 +9752,11 @@ type SoftwareEdge {
}

type Sound implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -10158,6 +10243,11 @@ type TextRestriction {
}

type Thesis implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -10717,6 +10807,11 @@ type UsageReportEdge {
}

type Work implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down Expand Up @@ -11149,6 +11244,11 @@ type WorkEdge {
}

type Workflow implements DoiItem {
"""
Metadata in bibtex format
"""
bibtex: String

"""
Total number of citations
"""
Expand Down
23 changes: 23 additions & 0 deletions app/graphql/types/doi_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module DoiItem
argument :style, String, required: false, default_value: "apa"
argument :locale, String, required: false, default_value: "en-US"
end
field :bibtex, String, null: true, description: "Metadata in bibtex format"
field :reference_count, Int, null: true, description: "Total number of references"
field :citation_count, Int, null: true, description: "Total number of citations"
field :view_count, Int, null: true, description: "Total number of views"
Expand Down Expand Up @@ -186,6 +187,28 @@ def descriptions(first: nil)
object.descriptions[0...first]
end

def bibtex
pages = object.container.to_h["firstPage"].present? ? [object.container["firstPage"], object.container["lastPage"]].join("-") : nil

bib = {
bibtex_type: object.types["bibtex"].presence || "misc",
bibtex_key: normalize_doi(object.doi),
doi: object.doi,
url: object.url,
author: authors_as_string(object.creators),
keywords: object.subjects.present? ? Array.wrap(object.subjects).map { |k| parse_attributes(k, content: "subject", first: true) }.join(", ") : nil,
language: object.language,
title: parse_attributes(object.titles, content: "title", first: true),
journal: object.container && container["title"],
volume: object.container.to_h["volume"],
issue: object.container.to_h["issue"],
pages: pages,
publisher: object.publisher,
year: object.publication_year
}.compact
BibTeX::Entry.new(bib).to_s
end

# defaults to style: apa and locale: en-US
def formatted_citation(style: nil, locale: nil)
cp = CiteProc::Processor.new(style: style || "apa", locale: locale || "en-US", format: "html")
Expand Down
1 change: 1 addition & 0 deletions spec/graphql/types/doi_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
it { is_expected.to have_field(:member).of_type("Member") }
it { is_expected.to have_field(:registrationAgency).of_type("String") }
it { is_expected.to have_field(:formattedCitation).of_type("String") }
it { is_expected.to have_field(:bibtex).of_type("String") }
it { is_expected.to have_field(:citationCount).of_type("Int") }
it { is_expected.to have_field(:referenceCount).of_type("Int") }
it { is_expected.to have_field(:viewCount).of_type("Int") }
Expand Down
30 changes: 30 additions & 0 deletions spec/graphql/types/work_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,34 @@
it { is_expected.to have_field(:id).of_type(!types.ID) }
it { is_expected.to have_field(:type).of_type("String!") }
end

describe "find work", elasticsearch: true do
let!(:work) { create(:doi, aasm_state: "findable") }

before do
Doi.import
sleep 2
end

let(:query) do
%(query {
work(id: "https://doi.org/#{work.doi}") {
id
bibtex
}
})
end

it "returns work" do
response = LupoSchema.execute(query).as_json

expect(response.dig("data", "work", "id")).to eq("https://handle.test.datacite.org/#{work.doi.downcase}")
bibtex = BibTeX.parse(response.dig("data", "work", "bibtex")).to_a(quotes: '').first
expect(bibtex[:bibtex_type].to_s).to eq("misc")
expect(bibtex[:bibtex_key]).to eq("https://doi.org/#{work.doi.downcase}")
expect(bibtex[:author]).to eq("Ollomo, Benjamin and Durand, Patrick and Prugnolle, Franck and Douzery, Emmanuel J. P. and Arnathau, Céline and Nkoghe, Dieudonné and Leroy, Eric and Renaud, François")
expect(bibtex[:title]).to eq("Data from: A new malaria agent in African hominids.")
expect(bibtex[:year]).to eq("2011")
end
end
end

0 comments on commit 06c2282

Please sign in to comment.