Module: RelatonDoi::Crossref
Constant Summary collapse
- HEADER =
{ "User-Agent" => "Relaton/RelatonDoi (https://www.relaton.org/guides/doi/; mailto:[email protected])" }.freeze
Instance Method Summary collapse
-
#get(doi) ⇒ RelatonBib::BibliographicItem, ...
Get a document by DOI from the CrossRef API.
-
#get_by_id(id) ⇒ Hash
Get a document by DOI from the CrossRef API.
Instance Method Details
#get(doi) ⇒ RelatonBib::BibliographicItem, ...
Get a document by DOI from the CrossRef API.
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/relaton_doi/crossref.rb', line 20 def get(doi) Util.info "Fetching from search.crossref.org ...", key: doi id = doi.sub(%r{^doi:}, "") = get_by_id id if Util.info "Found: `#{['DOI']}`", key: doi Parser.parse else Util.info "Not found.", key: doi nil end end |
#get_by_id(id) ⇒ Hash
Get a document by DOI from the CrossRef API.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/relaton_doi/crossref.rb', line 40 def get_by_id(id) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength # resp = Serrano.works ids: id n = 0 url = "https://api.crossref.org/works/#{CGI.escape(id)}" loop do resp = Faraday.get url, nil, HEADER case resp.status when 200 work = JSON.parse resp.body return work["message"] if work["status"] == "ok" when 404 then return nil end if n > 1 raise RelatonBib::RequestError, "Crossref error: #{resp.body}" end n += 1 sleep resp.headers["x-rate-limit-interval"].to_i * n end end |