Module: Relaton::Oiml::Bibliography

Defined in:
lib/relaton/oiml/bibliography.rb

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-oiml/main/".freeze

Class Method Summary collapse

Class Method Details

.get(ref, year = nil, opts = {}) ⇒ Object

See Also:

  • #search


52
53
54
# File 'lib/relaton/oiml/bibliography.rb', line 52

def get(ref, year = nil, opts = {})
  search(ref, year, opts)
end

.search(text, year = nil, _opts = {}) ⇒ Relaton::Oiml::Item?

Search for an OIML publication by its identifier.

Parameters:

  • text (String, Pubid::Oiml::Identifier)

    the OIML reference to look up (e.g. "OIML R 138" or "OIML R 138:2007 (E)")

  • year (String, nil) (defaults to: nil)

    the edition year (optional; may also be embedded in the reference)

  • _opts (Hash) (defaults to: {})

    options (unused)

Returns:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/relaton/oiml/bibliography.rb', line 20

def search(text, year = nil, _opts = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  pubid = text.is_a?(String) ? ::Pubid::Oiml.parse(text) : text
  Util.info "Fetching from Relaton repository ...", key: pubid.to_s
  # Pass the pubid so Relaton::Index narrows candidates by number via
  # binary search before applying the block. Every row's `:id` is a
  # Pubid::Oiml::Identifier (Relaton::Index deserialized it via the
  # `pubid_class` passed in `#index`), so the block compares pubids and
  # the result picks the latest edition.
  row = index.search(pubid) { |r| pubid_match?(r[:id], pubid, year) }
             .max_by { |r| r[:id].year.to_i }
  unless row
    Util.info "Not found.", key: pubid.to_s
    return
  end

  uri = URI("#{ENDPOINT}#{row[:file]}")
  resp = Net::HTTP.get_response uri
  unless resp.code == "200"
    raise Relaton::RequestError, "Could not access #{uri}: HTTP #{resp.code}"
  end

  item = Relaton::Oiml::Item.from_yaml resp.body
  Util.info "Found: `#{item.docidentifier.first&.content}`", key: pubid.to_s
  item.tap { |i| i.fetched = Date.today.to_s }
rescue SocketError, Errno::EINVAL, Errno::ECONNRESET, EOFError,
       Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
       Net::ProtocolError, Net::ReadTimeout, OpenSSL::SSL::SSLError,
       Errno::ETIMEDOUT => e
  raise Relaton::RequestError, "Could not access #{uri}: #{e.message}"
end