Skip to content

Commit

Permalink
fix crossref_json file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mfenner committed Nov 23, 2022
1 parent 17792fe commit c9a5a8f
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
briard (2.8.1)
briard (2.8.2)
activesupport (>= 4.2.5, < 8.0)
base32-url (>= 0.5.0, < 1)
benchmark_methods (~> 0.7)
Expand Down
11 changes: 6 additions & 5 deletions lib/briard/readers/crossref_json_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ def get_crossref_json(id: nil, **options)
return { 'string' => nil, 'state' => 'not_found' } unless id.present?

url = crossref_api_url(id, options)
response = Maremma.get(url, raw: true)
string = response.body.fetch('data', nil)
response = Maremma.get(url)
return { 'string' => nil, 'state' => 'not_found' } if response.body.dig('data', 'status') != 'ok'

string = response.body.dig('data', 'message').to_json

{ 'string' => string }
end
Expand All @@ -21,9 +23,8 @@ def read_crossref_json(string: nil, **options)

read_options = ActiveSupport::HashWithIndifferentAccess.new(options.except(:doi, :id, :url,
:sandbox, :validate, :ra))

meta = string.present? ? Maremma.from_json(string).fetch('message', {}) : {}

meta = string.present? ? Maremma.from_json(string) : {}

resource_type = meta.fetch('type', nil)
resource_type = resource_type.present? ? resource_type.underscore.camelcase : nil

Expand Down
4 changes: 0 additions & 4 deletions lib/briard/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,6 @@ module Utils
}

def find_from_format(id: nil, string: nil, ext: nil, filename: nil)
puts id
puts string
puts ext
puts filename
if id.present?
find_from_format_by_id(id)
elsif string.present? && ext.present?
Expand Down
2 changes: 1 addition & 1 deletion lib/briard/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Briard
VERSION = '2.8.1'
VERSION = '2.8.2'
end
67 changes: 64 additions & 3 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@
end

describe "convert from id", vcr: true do
context "crossref_json" do
let(:input) { "10.7554/eLife.01567" }

it 'default' do
expect { subject.convert input }.to output(/additionalType/).to_stdout
end

it 'to schema_org' do
subject.options = { from: "crossref_json", to: "schema_org" }
expect { subject.convert input }.to output(/additionalType/).to_stdout
end

it 'to crossref' do
subject.options = { from: "crossref_json", to: "crossref" }
expect { subject.convert input }.to output(/journal_metadata/).to_stdout
end

it 'to datacite' do
subject.options = { from: "crossref_json", to: "datacite" }
expect { subject.convert input }.to output(/http:\/\/datacite.org\/schema\/kernel-4/).to_stdout
end

it 'to bibtex' do
subject.options = { from: "crossref_json", to: "bibtex" }
expect { subject.convert input }.to output(/@article{https:\/\/doi.org\/10.7554\/elife.01567/).to_stdout
end

it 'to citation' do
subject.options = { from: "crossref_json", to: "citation", style: "vancouver" }
expect { subject.convert input }.to output(/Sankar M, Nieminen K, Ragni L, Xenarios I/).to_stdout
end
end

context "crossref" do
let(:input) { "10.7554/eLife.01567" }

Expand Down Expand Up @@ -133,7 +166,7 @@
let(:file) { fixture_path + "crossref.xml" }

it 'default' do
expect { subject.convert file }.to output(/datePublished/).to_stdout
expect { subject.convert file }.to output(/"@id": "https:\/\/doi.org\/10.7554\/elife.01567"/).to_stdout
end

it 'to schema_org' do
Expand All @@ -148,7 +181,35 @@

it 'to datacite' do
subject.options = { to: "datacite" }
expect { subject.convert file }.to output(/http:\/\/datacite.org\/schema\/kernel-4/).to_stdout
expect { subject.convert file }.to output(/<publisher>/).to_stdout
end

it 'to bibtex' do
subject.options = { to: "bibtex" }
expect { subject.convert file }.to output(/@article{https:\/\/doi.org\/10.7554\/elife.01567/).to_stdout
end
end

context "crossref_json", vcr: true do
let(:file) { fixture_path + "crossref.json" }

it 'default' do
expect { subject.convert file }.to output(/"@id": "https:\/\/doi.org\/10.7554\/elife.01567"/).to_stdout
end

it 'to schema_org' do
subject.options = { to: "schema_org" }
expect { subject.convert file }.to output(/"@context": "http:\/\/schema.org"/).to_stdout
end

it 'to crossref' do
subject.options = { to: "crossref" }
expect { subject.convert file }.to output(/journal_metadata/).to_stdout
end

it 'to datacite' do
subject.options = { to: "datacite" }
expect { subject.convert file }.to output(/<publisher>/).to_stdout
end

it 'to bibtex' do
Expand All @@ -171,7 +232,7 @@

it 'to datacite' do
subject.options = { to: "datacite" }
expect { subject.convert file }.to output(/http:\/\/datacite.org\/schema\/kernel-4/).to_stdout
expect { subject.convert file }.to output(/<publisher>/).to_stdout
end

it 'to bibtex' do
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c9a5a8f

Please sign in to comment.