Module: RelatonEcma::EcmaBibliography

Defined in:
lib/relaton_ecma/ecma_bibliography.rb

Overview

IETF bibliography module

Constant Summary collapse

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

Class Method Summary collapse

Class Method Details

.compare_edition_volume(aaa, bbb) ⇒ Object



54
55
56
57
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 54

def compare_edition_volume(aaa, bbb)
  comp = bbb[:id][:ed] <=> aaa[:id][:ed]
  comp.zero? ? aaa[:id][:vol] <=> bbb[:id][:vol] : comp
end

.fetch_doc(code) ⇒ Object

rubocop:disable Metrics/AbcSize



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 59

def fetch_doc(code) # rubocop:disable Metrics/AbcSize
  row = search(code).min { |a, b| compare_edition_volume a, b }
  return unless row

  url = "#{ENDPOINT}#{row[:file]}"
  doc = OpenURI.open_uri url
  hash = YAML.safe_load doc
  hash["fetched"] = Date.today.to_s
  BibliographicItem.from_hash hash
rescue OpenURI::HTTPError => e
  return if e.io.status.first == "404"

  raise RelatonBib::RequestError, "No document found for #{code} reference. #{e.message}"
end

.get(code, _year = nil, _opts = {}) ⇒ RelatonEcma::BibliographicItem

Returns Relaton of reference.

Parameters:

  • code (String)

    the ECMA standard Code to look up (e..g “ECMA-6”)

  • year (String)

    not used

  • opts (Hash)

    not used

Returns:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 42

def get(code, _year = nil, _opts = {})
  Util.info "Fetching from Relaton repository ...", key: code
  result = fetch_doc(code)
  if result
    Util.info "Found: `#{result.docidentifier.first.id}`", key: code
    # item
  else
    Util.info "Not found.", key: code
  end
  result
end

.match_ref(refparts, row) ⇒ Object

rubocop:disable Metrics/AbcSize



32
33
34
35
36
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 32

def match_ref(refparts, row) # rubocop:disable Metrics/AbcSize
  row[:id][:id].match?(/^ECMA[-\s]#{refparts[:id]}/) &&
    (refparts[:ed].nil? || row[:id][:ed] == refparts[:ed]) &&
    (refparts[:vol].nil? || row[:id][:vol] == refparts[:vol])
end

.parse_ref(ref) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 24

def parse_ref(ref)
  %r{^ECMA[-\s]
    (?<id>(?:\d[\d-]*|\w+/\d+))
    (?:\sed(?<ed>[\d.]+))?
    (?:\svol(?<vol>\d+))?
  }x.match ref
end

.search(ref) ⇒ Array<Hash>

Search for a reference on the IETF website.

Parameters:

  • ref (String)

    the ECMA standard reference to look up (e..g “ECMA-6”)

Returns:

  • (Array<Hash>)


16
17
18
19
20
21
22
# File 'lib/relaton_ecma/ecma_bibliography.rb', line 16

def search(ref)
  refparts = parse_ref ref
  return [] unless refparts

  index = Relaton::Index.find_or_create :ECMA, url: "#{ENDPOINT}index.zip", id_keys: %i[id ed vol]
  index.search { |row| match_ref refparts, row }
end