Class: RelatonIeee::IeeeBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_ieee/ieee_bibliography.rb

Constant Summary collapse

GH_URL =
"https://raw.githubusercontent.com/relaton/relaton-data-ieee/main/".freeze
INDEX_FILE =
"index-v1.yaml".freeze

Class Method Summary collapse

Class Method Details

.get(code, _year = nil, _opts = {}) ⇒ Hash, NilClass

Get IEEE bibliography item by reference.

Parameters:

  • code (String)

    the IEEE standard Code to look up (e..g “528-2019”)

  • year (String)

    the year the standard was published (optional)

  • opts (Hash)

    options

Returns:

  • (Hash, NilClass)

    returns { ret: RelatonBib::BibliographicItem } if document is found else returns NilClass



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/relaton_ieee/ieee_bibliography.rb', line 40

def get(code, _year = nil, _opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  Util.info "Fetching from Relaton repository ...", key: code
  item = search(code)
  if item
    Util.info "Found: `#{item.docidentifier.first.id}`", key: code
    item
  else
    Util.info "Not found.", key: code
    nil
  end
end

.search(code) ⇒ RelatonIeee::IeeeBibliographicItem

Search IEEE bibliography item by reference.

Parameters:

  • code (String)

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/relaton_ieee/ieee_bibliography.rb', line 14

def search(code) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  ref = code.sub(/Std\s/i, "") # .gsub(/[\s,:\/]/, "_").squeeze("_").upcase
  index = Relaton::Index.find_or_create :ieee, url: "#{GH_URL}index-v1.zip", file: INDEX_FILE
  row = index.search(ref).min_by { |r| r[:id] }
  return unless row

  resp = Faraday.get "#{GH_URL}#{row[:file]}"
  return unless resp.status == 200

  hash = YAML.safe_load resp.body
  hash["fetched"] = Date.today.to_s
  IeeeBibliographicItem.from_hash hash
rescue Faraday::ConnectionFailed
  raise RelatonBib::RequestError, "Could not access #{GH_URL}"
end