Module: Relaton::Iho::Bibliography

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

Constant Summary collapse

ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-iho/refs/heads/data-v2/".freeze

Class Method Summary collapse

Class Method Details

.enrich_with_pubid(item, pubid) ⇒ Object

Populate ext.structuredidentifier from the parsed Pubid when the fetched record doesn’t already provide one. Maps ‘pubid.number` (with type prefix, e.g. `S-100`) -> docnumber, `pubid.part` -> part, `pubid.appendix` -> appendixid, `pubid.annex` -> annexid, `pubid.supplement` -> supplementid.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/relaton/iho/bibliography.rb', line 47

def enrich_with_pubid(item, pubid)
  return if item.ext&.structuredidentifier&.any?

  sid = StructuredIdentifier.new(
    docnumber: pubid_docnumber(pubid),
    part: pubid.part,
    appendixid: (pubid.appendix if pubid.respond_to?(:appendix)),
    annexid: (pubid.annex if pubid.respond_to?(:annex)),
    supplementid: (pubid.supplement if pubid.respond_to?(:supplement)),
  )
  item.ext ||= Ext.new
  item.ext.structuredidentifier = [sid]
end

.get(ref, year = nil, opts = {}) ⇒ RelatonIho::IhoBibliographicItem

Parameters:

  • ref (String)

    the IHO standard Code to look up (e..g “IHO B-11”)

  • year (String) (defaults to: nil)

    the year the standard was published (optional)

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

    options

Options Hash (opts):

  • :all_parts (TrueClass, FalseClass)

    restricted to all parts if all-parts reference is required

  • :bibdata (TrueClass, FalseClass)

Returns:

  • (RelatonIho::IhoBibliographicItem)


76
77
78
# File 'lib/relaton/iho/bibliography.rb', line 76

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

.pubid_docnumber(pubid) ⇒ Object

“S-100” rather than “100” — keeps the type prefix that distinguishes the IHO series (S/P/M/B/C). Matches spec/fixtures/iho_part.xml.



63
64
65
# File 'lib/relaton/iho/bibliography.rb', line 63

def pubid_docnumber(pubid)
  pubid.to_s.sub(/^IHO\s/, "").split(/\s+/, 2).first
end

.search(text, _year = nil, _opts = {}) ⇒ RelatonIho::IhoBibliographicItem?

Search for IHO standard by IHO standard Code

Parameters:

  • text (String)

    the IHO standard Code to look up (e..g “IHO B-11”)

Returns:

  • (RelatonIho::IhoBibliographicItem, nil)

    the IHO standard or nil if not found



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/relaton/iho/bibliography.rb', line 16

def search(text, _year = nil, _opts = {}) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  pubid = text.is_a?(String) ? ::Pubid::Iho::Identifier.parse(text) : text
  Util.info "Fetching from Relaton repository ...", key: pubid.to_s
  row = index.search { |r| pubid_match?(r[:id], pubid) }.min_by { |r| row_version(r[:id]) }
  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::Iho::Item.from_yaml resp.body
  enrich_with_pubid(item, pubid)
  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