class RelatonBsi::BsiBibliography
Class methods for search ISO standards.
Public Class Methods
@param code [String] the BSI 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 [String] Relaton XML serialisation of reference
# File lib/relaton_bsi/bsi_bibliography.rb, line 31 def get(code, year = nil, opts = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity c, y = code.split ':' year ||= y ret = bib_get1(c, year, opts) return nil if ret.nil? # ret = ret.to_most_recent_reference unless year || opts[:keep_year] # ret = ret.to_all_parts if opts[:all_parts] ret end
@param text [String] @return [RelatonBsi::HitCollection]
# File lib/relaton_bsi/bsi_bibliography.rb, line 18 def search(text, year = nil) code = text.sub(/^BSI\s/, "") HitCollection.new code, year rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, Algolia::AlgoliaUnreachableHostError => e raise RelatonBib::RequestError, e.message end
Private Class Methods
# File lib/relaton_bsi/bsi_bibliography.rb, line 96 def bib_get1(code, year, _opts) result = search_filter(code) || return ret = isobib_results_filter(result, year) if ret[:ret] warn "[relaton-bsi] (\"#{code}\") found #{ret[:ret].docidentifier.first&.id}" ret[:ret] else fetch_ref_err(code, year, ret[:years]) end end
# File lib/relaton_bsi/bsi_bibliography.rb, line 44 def fetch_ref_err(code, year, missed_years) # rubocop:disable Metrics/MethodLength id = year ? "#{code}:#{year}" : code warn "[relaton-bsi] 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-bsi] (There was no match for #{year}, though there were matches "\ "found for #{missed_years.join(', ')}.)" end # if /\d-\d/.match? code # warn "[relaton-bsi] The provided document part may not exist, or the document "\ # "may no longer be published in parts." # else # warn "[relaton-bsi] 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
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_bsi/bsi_bibliography.rb, line 82 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
# File lib/relaton_bsi/bsi_bibliography.rb, line 63 def search_filter(code) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity # %r{^BSI\s(?<code1>[^:]+)} =~ code code1 = code.sub(/^BSI\s/, "") warn "[relaton-bsi] (\"#{code}\") fetching..." return [] unless code1 result = search(code) result.select do |i| %r{^(?<code2>[^:]+)} =~ i.hit[:code] code2.include?(code1) end end