Class: RelatonBipm::BipmBibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_bipm/bipm_bibliography.rb

Constant Summary collapse

GH_ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-bipm/main/".freeze
INDEX_FILE =
"index2.yaml".freeze

Class Method Summary collapse

Class Method Details

.get(ref, year = nil, opts = {}) ⇒ RelatonBipm::BipmBibliographicItem

Parameters:

  • ref (String)

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

  • year (String) (defaults to: nil)

    not used

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

    not used

Returns:



78
79
80
# File 'lib/relaton_bipm/bipm_bibliography.rb', line 78

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

.get_bipm(reference) ⇒ RelatonBipm::BipmBibliographicItem

Parameters:

  • reference (String)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/relaton_bipm/bipm_bibliography.rb', line 48

def get_bipm(reference) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  ref_id = Id.new.parse reference
  rows = index.search { |r| ref_id == r[:id] }
  return unless rows.any?

  row = rows.sort_by { |r| r[:id][:year] }.last
  url = "#{GH_ENDPOINT}#{row[:file]}"
  resp = Mechanize.new.get url
  return unless resp.code == "200"

  yaml = RelatonBib.parse_yaml resp.body, [Date]
  yaml["fetched"] = Date.today.to_s
  bib_hash = HashConverter.hash_to_bib yaml
  BipmBibliographicItem.new(**bib_hash)
end

.indexObject



64
65
66
67
68
# File 'lib/relaton_bipm/bipm_bibliography.rb', line 64

def index
  Relaton::Index.find_or_create(
    :bipm, url: "#{GH_ENDPOINT}index2.zip", file: INDEX_FILE, id_keys: %i[group type number year corr part append]
  )
end

.search(text, _year = nil, _opts = {}) ⇒ RelatonBipm::BipmBibliographicItem

Parameters:

  • text (String)

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/relaton_bipm/bipm_bibliography.rb', line 11

def search(text, _year = nil, _opts = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  Util.info "Fetching from Relaton repository ...", key: text
  ref = text.sub(/^BIPM\s/, "")
  item = get_bipm ref
  unless item
    Util.info "Not found.", key: text
    return
  end

  Util.info "Found: `#{item.docidentifier[0].id}`", key: text
  item
rescue Mechanize::ResponseCodeError => e
  raise RelatonBib::RequestError, e.message unless e.response_code == "404"
end