Class: RelatonW3c::W3cBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_w3c/w3c_bibliography.rb

Overview

Class methods for search W3C standards.

Constant Summary collapse

SOURCE =
"https://raw.githubusercontent.com/relaton/relaton-data-w3c/main/"
INDEX =
"index1"

Class Method Summary collapse

Class Method Details

.get(ref, _year = nil, _opts = {}) ⇒ RelatonW3c::W3cBibliographicItem

Parameters:

  • ref (String)

    the W3C standard Code to look up

  • year (String, NilClass)

    not used

  • opts (Hash)

    options

Returns:



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

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

  found = result.docidentifier.first.id
  Util.info "Found: `#{found}`", key: ref
  result
end

.search(text) ⇒ RelatonW3c::W3cBibliographicItem

Parameters:

  • text (String)

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/relaton_w3c/w3c_bibliography.rb', line 14

def search(text) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  pubid = PubId.parse text.sub(/^W3C\s/, "")
  index = Relaton::Index.find_or_create(
    :W3C, url: "#{SOURCE}#{INDEX}.zip", file: "#{INDEX}.yaml", id_keys: PubId::PARTS
  )
  row = index.search { |r| pubid == r[:id] }.sort_by{ |r| (r[:id][:date] || r[:id][:year]).to_i }.last
  return unless row

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

  hash = YAML.safe_load resp.body
  hash["fetched"] = Date.today.to_s
  item_hash = ::RelatonW3c::HashConverter.hash_to_bib(hash)
  ::RelatonW3c::W3cBibliographicItem.new(**item_hash)
rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET,
       EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
       Net::ProtocolError, Errno::ETIMEDOUT
  raise RelatonBib::RequestError,
        "Could not access #{url}"
end