Class: Relaton::Bipm::Bibliography

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/bipm/bibliography.rb

Constant Summary collapse

GH_ENDPOINT =
"https://raw.githubusercontent.com/relaton/relaton-data-bipm/refs/heads/data-v2/".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:

  • (RelatonBipm::BipmBibliographicItem)


77
78
79
# File 'lib/relaton/bipm/bibliography.rb', line 77

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

.get_bipm(reference) ⇒ RelatonBipm::BipmBibliographicItem

Parameters:

  • reference (String)

Returns:

  • (RelatonBipm::BipmBibliographicItem)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/relaton/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"

  item = Item.from_yaml resp.body
  item.fetched = Date.today.to_s
  item
end

.indexObject



63
64
65
66
67
# File 'lib/relaton/bipm/bibliography.rb', line 63

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

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

Parameters:

  • text (String)

Returns:

  • (RelatonBipm::BipmBibliographicItem)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/relaton/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].content}`", key: text
  item
rescue Mechanize::ResponseCodeError => e
  raise Relaton::RequestError, e.message unless e.response_code == "404"
end