class RelatonBipm::BipmBibliography

Constants

GH_ENDPOINT
IOP_DOMAIN

Public Class Methods

bdate(rsp) click to toggle source

@param rsp [Mechanize::Page] @return [Array<RelatonBib::BibliographicDate>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 180
def bdate(rsp)
  date = rsp.at('//p[@itemprop="issueNumber"]|//h2[@itemprop="volumeNumber"]').text.split(", ").last
  on = date.match?(/^\d{4}$/) ? date : Date.parse(date).strftime("%Y-%m")
  [RelatonBib::BibliographicDate.new(type: "published", on: on)]
end
bibitem(**args) click to toggle source

@param args [Hash] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 222
def bibitem(**args)
  BipmBibliographicItem.new(
    fetched: Date.today.to_s, type: "standard", language: ["en"], script: ["Latn"], **args,
  )
end
btabstract(bibtex) click to toggle source

@param bibtex [BibTeX::Entry] @return [Array<RelatonBib::FormattedString>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 242
def btabstract(bibtex)
  [RelatonBib::FormattedString.new(content: bibtex.abstract.to_s, language: "en", script: "Latn")]
end
btcontrib(bibtex) click to toggle source

@param bibtex [BibTeX::Entry] @return [Array<Hash>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 265
def btcontrib(bibtex)
  surname, initial = bibtex.author.split ", "
  initial = initial.split.map { |i| RelatonBib::LocalizedString.new i, "en", "Latn" }
  surname = RelatonBib::LocalizedString.new surname, "en", "Latn"
  name = RelatonBib::FullName.new surname: surname, initial: initial
  author = RelatonBib::Person.new name: name
  [
    { entity: { name: bibtex.publisher.to_s }, role: [{ type: "publisher" }] },
    { entity: author, role: [{ type: "author" }] },
  ]
end
btdate(bibtex) click to toggle source

@param bibtex [BibTeX::Entry] @return [Array<RelatonBib::BibliographicDate>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 258
def btdate(bibtex)
  on = Date.new(bibtex.year.to_i, bibtex.month_numeric)
  [RelatonBib::BibliographicDate.new(type: "published", on: on)]
end
btdocid(bibtex) click to toggle source

@param bibtex [BibTeX::Entry] @return [Array<RelatonBib::DocumentIdentifier>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 235
def btdocid(bibtex)
  id = "#{bibtex.journal} #{bibtex.volume} #{bibtex.number} #{bibtex.pages.match(/^\d+/)}"
  [RelatonBib::DocumentIdentifier.new(type: "BIPM", id: id)]
end
btextent(vol, ish = nil, bibtex = nil) click to toggle source

@param vol [String] @param ish [String] @param bibtex [BibTeX::Entry] @return [Array<RelatonBib::BibItemLocality>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 281
def btextent(vol, ish = nil, bibtex = nil)
  ext = [RelatonBib::BibItemLocality.new("volume", vol)]
  ext << RelatonBib::BibItemLocality.new("issue", ish) if ish
  ext << RelatonBib::BibItemLocality.new("page", *bibtex.pages.split("--")) if bibtex
  ext
end
btitle(content) click to toggle source

@param content [RelatonBib::TypedTitleString] @return [RelatonBib::TypedTitleString]

# File lib/relaton_bipm/bipm_bibliography.rb, line 168
def btitle(content)
  RelatonBib::TypedTitleString.new type: "main", content: content, language: "en", script: "Latn"
end
doc_id(args) click to toggle source

@param args [Array<String>] @return [RelatonBib::DocumentIdentifier]

# File lib/relaton_bipm/bipm_bibliography.rb, line 188
def doc_id(args)
  id = args.clone.unshift "Metrologia"
  RelatonBib::DocumentIdentifier.new(type: "BIPM", id: id.join(" "))
end
fref(ref) click to toggle source

@param ref [String] @return [RelatonBib::FormattedRef]

# File lib/relaton_bipm/bipm_bibliography.rb, line 133
def fref(ref)
  RelatonBib::FormattedRef.new content: ref, language: "en", script: "Latn"
end
get(ref, year = nil, opts = {}) click to toggle source

@param ref [String] the BIPM standard Code to look up (e..g “BIPM B-11”) @param year [String] not used @param opts [Hash] not used @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 292
def get(ref, year = nil, opts = {})
  search(ref, year, opts)
end
get_article(path, vol, ish, agent) click to toggle source

@param path [String] @param vol [String] @param ish [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 209
def get_article(path, vol, ish, agent) # rubocop:disable Metrics/AbcSize
  rsp = agent.get path
  url = rsp.uri
  bib = rsp.link_with(text: "BibTeX").href
  rsp = agent.get bib
  bt = BibTeX.parse(rsp.body).first
  bibitem(docid: btdocid(bt), title: titles(bt.title.to_s), abstract: btabstract(bt), doctype: bt.type.to_s,
          link: btlink(bt, url), date: btdate(bt), contributor: btcontrib(bt), series: series,
          extent: btextent(vol, ish, bt))
end
get_article_from_issue(vol, ish, art, agent) click to toggle source

@param vol [String] @param ish [String] @param art [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 198
def get_article_from_issue(vol, ish, art, agent)
  url = issue_url vol, ish
  rsp = agent.get url
  get_article rsp.at("//div[@class='indexer'][.='#{art}']/../div/a")[:href], vol, ish, agent
end
get_bipm(ref, agent) click to toggle source

@param ref [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 41
def get_bipm(ref, agent)
  url = "#{GH_ENDPOINT}#{ref.downcase.split.join '-'}.yaml"
  resp = agent.get url
  return unless resp.code == "200"

  bib_hash = HashConverter.hash_to_bib YAML.safe_load(resp.body, [Date])
  BipmBibliographicItem.new(**bib_hash)
end
get_issue(vol, ish, agent) click to toggle source

@param vol [String] @param ish [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 118
def get_issue(vol, ish, agent) # rubocop:disable Metrics/AbcSize
  url = issue_url vol, ish
  rsp = agent.get url
  rel = rsp.xpath('//div[@class="art-list-item-body"]').map do |a|
    { type: "partOf", bibitem: issue_rel(a, vol, ish) }
  end
  did = doc_id [vol, ish]
  title_fref = { title: issue_title(rsp) }
  title_fref[:formattedref] = fref did.id unless title_fref[:title].any?
  bibitem(**title_fref, link: blink(url), relation: rel, docid: [did],
                        date: bdate(rsp), extent: btextent(vol, ish), series: series)
end
get_journal(agent) click to toggle source

@param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 66
def get_journal(agent)
  url = "#{IOP_DOMAIN}/journal/0026-1394"
  rsp = agent.get url
  rel = rsp.xpath('//select[@id="allVolumesSelector"]/option').map do |v|
    { type: "partOf", bibitem: journal_rel(v) }
  end
  did = doc_id []
  bibitem(formattedref: fref(did.id), docid: [did], link: blink(url), relation: rel)
end
get_metrologia(ref, agent) click to toggle source

@param ref [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 53
def get_metrologia(ref, agent)
  agent.redirect_ok = false
  ref_arr = ref.split
  case ref_arr.size
  when 1 then get_journal agent
  when 2 then get_volume ref_arr[1], agent
  when 3 then get_issue(*ref_arr[1..2], agent)
  when 4 then get_article_from_issue(*ref_arr[1..3], agent)
  end
end
get_volume(vol, agent) click to toggle source

@param vol [String] @param agent [Mechanize] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 87
def get_volume(vol, agent)
  url = "#{IOP_DOMAIN}/volume/0026-1394/#{vol}"
  rsp = agent.get url
  rel = rsp.xpath('//li[@itemprop="hasPart"]').map do |i|
    { type: "partOf", bibitem: volume_rel(i, vol) }
  end
  did = doc_id [vol]
  bibitem(formattedref: fref(did.id), docid: [did], link: blink(url), date: bdate(rsp), relation: rel,
          extent: btextent(vol), series: series)
end
issue_rel(elm, vol, ish) click to toggle source

@param elm [Nokogiri::XML::Element] @param vol [String] @param ish [String] @return [RelatonBipm::BipmBibliographicItem]

# File lib/relaton_bipm/bipm_bibliography.rb, line 157
def issue_rel(elm, vol, ish)
  art = elm.at('div[@class="indexer"]').text
  ref = elm.at('div/a[@class="art-list-item-title"]')
  title = titles ref.text.strip
  docid = doc_id [vol, ish, art]
  link = blink IOP_DOMAIN + ref[:href]
  BipmBibliographicItem.new(title: title, docid: [docid], link: link)
end
issue_title(rsp) click to toggle source

@param rsp [Mechanize::Page] @return [RelatonBib::TypedTitleStringCollection]

# File lib/relaton_bipm/bipm_bibliography.rb, line 139
def issue_title(rsp)
  t = rsp.at('//div[@id="wd-jnl-issue-title"]/h4')
  return RelatonBib::TypedTitleStringCollection.new [] unless t

  titles(t.text)
end
issue_url(vol, ish) click to toggle source

@oaran vol [String] @param ish [String] @return [String]

# File lib/relaton_bipm/bipm_bibliography.rb, line 149
def issue_url(vol, ish)
  "#{IOP_DOMAIN}/issue/0026-1394/#{vol}/#{ish}"
end
journal_rel(elm) click to toggle source

@param elm [Nokogiri::XML::Element]

# File lib/relaton_bipm/bipm_bibliography.rb, line 77
def journal_rel(elm)
  vol = elm[:value].split("/").last
  did = doc_id [vol]
  url = IOP_DOMAIN + elm[:value]
  BipmBibliographicItem.new(formattedref: fref(did.id), docid: [did], link: blink(url))
end
magent() click to toggle source

@return [Mechanize]

# File lib/relaton_bipm/bipm_bibliography.rb, line 24
def magent # rubocop:disable Metrics/MethodLength
  a = Mechanize.new
  a.request_headers = {
    "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,"\
      "*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
    "Accept-Encoding" => "gzip, deflate, br",
    "Accept-Language" => "en-US,en;q=0.9,ru-RU;q=0.8,ru;q=0.7",
    "Cache-Control" => "max-age=0",
    "Upgrade-Insecure-Requests" => "1",
  }
  a.user_agent_alias = Mechanize::AGENT_ALIASES.map(&:first).shuffle.first
  a
end
series() click to toggle source

@return [Array<RelatonBib::Series>]

# File lib/relaton_bipm/bipm_bibliography.rb, line 229
def series
  [RelatonBib::Series.new(title: btitle("Metrologia"))]
end
titles(title) click to toggle source

@param title [String] @return [RelatonBib::TypedTitleStringCollection]

# File lib/relaton_bipm/bipm_bibliography.rb, line 110
def titles(title)
  RelatonBib::TypedTitleString.from_string title, "en", "Latn"
end
volume_rel(elm, vol) click to toggle source
# File lib/relaton_bipm/bipm_bibliography.rb, line 98
def volume_rel(elm, vol) # rubocop:disable Metrics/AbcSize
  a = elm.at 'a[@itemprop="issueNumber"]'
  ish = a[:href].split("/").last
  url = IOP_DOMAIN + a[:href]
  docid = doc_id [vol, ish]
  t = elm.at "p"
  title_fref = t ? { title: titles(t.text) } : { formattedref: fref(docid.id) }
  BipmBibliographicItem.new(**title_fref, docid: [docid], link: blink(url))
end