Module: Relaton::Etsi::Bibliography

Extended by:
Bibliography
Defined in:
lib/relaton/etsi/bibliography.rb

Overview

Methods for search IANA standards.

Constant Summary collapse

SOURCE =
"https://raw.githubusercontent.com/relaton/relaton-data-etsi/refs/heads/v2/"
SUFFIX =

What follows the document number in an index id: the part numbers, then the version. A reference matches a whole number only, so a part number is optional but a group of digits of the number is not.

/\A(?:-\d+)*\s(?:V\d|ed\.\d)/

Instance Method Summary collapse

Instance Method Details

#best_match(index, text) ⇒ Hash?

Match the reference against the index and return the newest edition.

An index id renders as ETSI <type> <number> <V1.2.3|ed.4> (<yyyy-mm>). The index matches a String query with include?, so a bare reference matches every edition of the document, but also longer numbers. The matches? filter drops the longer numbers, then edition_key picks the highest version, and among equal versions the latest date.

Parameters:

  • index (Relaton::Index::Type)
  • text (String)

    the reference

Returns:

  • (Hash, nil)

    the winning index row ({ id:, file: })



59
60
61
62
63
64
# File 'lib/relaton/etsi/bibliography.rb', line 59

def best_match(index, text)
  ref = normalize(text)
  index.search(ref)
    .select { |row| matches? ref, row[:id] }
    .max_by { |row| edition_key row[:id] }
end

#get(ref, _year = nil, _opts = {}) ⇒ Relaton::Etsi::ItemData?

Parameters:

  • ref (String)

    the ETSI standard Code to look up

  • year (String, nil)

    year

  • opts (Hash)

    options

Returns:



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/relaton/etsi/bibliography.rb', line 36

def get(ref, _year = nil, _opts = {})
  Util.info "Fetching from Relaton repository ...", key: ref
  result = search(ref)
  unless result
    Util.info "Not found.", key: ref
    return
  end

  Util.info "Found: `#{result.docidentifier[0].content}`", key: ref
  result
end

#search(text) ⇒ Relaton::Etsi::ItemData?

Parameters:

  • text (String)

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/relaton/etsi/bibliography.rb', line 16

def search(text) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  index = Relaton::Index.find_or_create :etsi, url: "#{SOURCE}index-v1.zip", file: INDEX_FILE
  row = best_match(index, text)
  return unless row

  url = "#{SOURCE}#{row[:file]}"
  resp = Net::HTTP.get_response URI(url)
  return unless resp.code == "200"

  Item.from_yaml(resp.body).tap { |item| item.fetched = Date.today.to_s }
rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
       EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
       Net::ProtocolError, Errno::ETIMEDOUT => e
  raise Relaton::RequestError, e.message
end