class RelatonCen::CenBibliography

Class methods for search Cenelec standards.

Public Class Methods

get(code, year = nil, opts = {}) click to toggle source

@param code [String] the CEN standard Code to look up @param year [String] the year the standard was published (optional) @param opts [Hash] options; restricted to :all_parts if all-parts

reference is required

@return [RelatonBib::BibliographicItem, nil]

# File lib/relaton_cen/cen_bibliography.rb, line 28
def get(code, year = nil, opts = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  code1, year1 = code.split ":"
  code = code1
  year ||= year1

  bib_get1(code, year, opts)
end

Private Class Methods

bib_get1(code, year, _opts) click to toggle source
# File lib/relaton_cen/cen_bibliography.rb, line 90
def bib_get1(code, year, _opts)
  result = search_filter(code) || return
  ret = isobib_results_filter(result, year)
  if ret[:ret]
    warn "[relaton-cen] (\"#{code}\") found #{ret[:ret].docidentifier.first&.id}"
    ret[:ret]
  else
    fetch_ref_err(code, year, ret[:years])
  end
end
fetch_ref_err(code, year, missed_years) click to toggle source
# File lib/relaton_cen/cen_bibliography.rb, line 38
def fetch_ref_err(code, year, missed_years) # rubocop:disable Metrics/MethodLength
  id = year ? "#{code}:#{year}" : code
  warn "[relaton-cen] WARNING: no match found online for #{id}. "\
    "The code must be exactly like it is on the standards website."
  unless missed_years.empty?
    warn "[relaton-cen] (There was no match for #{year}, though there "\
      "were matches found for #{missed_years.join(', ')}.)"
  end
  # if /\d-\d/.match? code
  #   warn "[relaton-cen] The provided document part may not exist, or "\
  #     "the document may no longer be published in parts."
  # else
  #   warn "[relaton-cen] If you wanted to cite all document parts for "\
  #     "the reference, use \"#{code} (all parts)\".\nIf the document "\
  #     "is not a standard, use its document type abbreviation (TS, TR, "]
  #     "PAS, Guide)."
  # end
  nil
end
isobib_results_filter(result, year) click to toggle source

Sort through the results from Isobib, fetching them three at a time, and return the first result that matches the code, matches the year (if provided), and which # has a title (amendments do not). Only expects the first page of results to be populated. Does not match corrigenda etc (e.g. ISO 3166-1:2006/Cor 1:2007) If no match, returns any years which caused mismatch, for error reporting

# File lib/relaton_cen/cen_bibliography.rb, line 76
def isobib_results_filter(result, year)
  missed_years = []
  result.each do |r|
    /:(?<pyear>\d{4})/ =~ r.hit[:code]
    if !year || year == pyear
      ret = r.fetch
      return { ret: ret } if ret
    end

    missed_years << pyear
  end
  { years: missed_years }
end
search_filter(code) click to toggle source

@param code [String] @return [RelatonCen::HitCollection]

# File lib/relaton_cen/cen_bibliography.rb, line 60
def search_filter(code) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  %r{^CEN\s(?<code1>[^-:]+)(?:-(?<part1>\d+))?} =~ code
  warn "[relaton-cen] (\"#{code}\") fetching..."
  result = search(code)
  result.select do |i|
    %r{^(?<code2>[^:-]+)(?:-(?<part2>\d+))?} =~ i.hit[:code]
    code2.include?(code1) && (!part1 || part1 = part2)
  end
end