class RelatonItu::ItuBibliography
Class methods for search ISO standards.
Public Class Methods
get(code, year = nil, opts = {})
click to toggle source
@param code [String] the ISO standard Code to look up (e..g “ISO 9000”) @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_itu/itu_bibliography.rb, line 40 def get(code, year = nil, opts = {}) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity if year.nil? /^(?<code1>[^\s]+\s[^\s]+)\s\((\d{2}\/)?(?<year1>\d+)\)$/ =~ code unless code1.nil? code = code1 year = year1 end end code += "-1" if opts[:all_parts] ret = itubib_get1(code, 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
search(text, year = nil)
click to toggle source
@param text [String] @return [RelatonItu::HitCollection]
# File lib/relaton_itu/itu_bibliography.rb, line 21 def search(text, year = nil) # code = text.sub(/(?<=ITU-T\s\w)\.(\w+\.)(?=\d+)/, ' \1') if text =~ /(ITU-T\s\w)\.(Suppl\.|Annex)\s?(\w?\d+)/ correct_ref = "#{$~[1]} #{$~[2]} #{$~[3]}" warn "[relaton-itu] WARNING: Incorrect reference #{text}" warn "[relaton-itu] the reference should be #{correct_ref}" end HitCollection.new text, year rescue SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError, URI::InvalidURIError => e raise RelatonBib::RequestError, e.message end
Private Class Methods
fetch_ref_err(code, year, missed_years)
click to toggle source
# File lib/relaton_itu/itu_bibliography.rb, line 60 def fetch_ref_err(code, year, missed_years) # rubocop:disable Metrics/MethodLength id = year ? "#{code}:#{year}" : code warn "[relaton-itu] 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-itu] (There was no match for #{year}, though there "\ "were matches found for #{missed_years.join(', ')}.)" end if /\d-\d/.match? code warn "[relaton-itu] The provided document part may not exist, or "\ "the document may no longer be published in parts." else warn "[relaton-itu] 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_itu/itu_bibliography.rb, line 124 def isobib_results_filter(result, year) missed_years = [] result.each do |r| /\((\d{2}\/)?(?<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
itubib_get1(code, year, _opts)
click to toggle source
# File lib/relaton_itu/itu_bibliography.rb, line 138 def itubib_get1(code, year, _opts) result = search_filter(code, year) || return ret = isobib_results_filter(result, year) if ret[:ret] warn "[relaton-itu] (\"#{code}\") found #{ret[:ret].docidentifier.first&.id}" ret[:ret] else fetch_ref_err(code, year, ret[:years]) end end
search_filter(code, year)
click to toggle source
# File lib/relaton_itu/itu_bibliography.rb, line 79 def search_filter(code, year) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity %r{ ^(?<pref1>ITU)?(-(?<type1>\w))?\s?(?<code1>[^\s\/]+(?:\/\w[\.\d]+)?) (\s\(?(?<ver1>v\d+)\)?)? (\s\(((?<month1>\d{2})\/)?(?<year1>\d{4})\))? (\s-\s(?<buldate1>\d{2}\.\w{1,4}\.\d{4}))? (\s(?<corr1>(Amd|Cor|Amendment|Corrigendum)\.?\s?\d+))? (\s\(((?<cormonth1>\d{2})\/)?(?<coryear1>\d{4})\))? }x =~ code year ||= year1 # docidrx = %r{\w+\.\d+|\w\sSuppl\.\s\d+} # %r{^ITU-T\s[^\s]+} # c = code.sub(/Imp\s?/, "").match(docidrx).to_s warn "[relaton-itu] (\"#{code}\") fetching..." result = search(code) code1.sub! /(?<=\.)Imp(?=\d)/, "" if result.gi_imp if corr1 corr1.sub!(/[\.\s]+/, " ").sub!("Amendment", "Amd") corr1.sub!("Corrigendum", "Corr") end result.select do |i| next true unless i.hit[:code] %r{ ^(?<pref2>ITU)?(-(?<type2>\w))?\s?(?<code2>[\S]+) (\s\(?(?<ver2>v\d+)\)?)? (\s\(((?<month2>\d{2})\/)?(?<year2>\d{4})\))? (\s(?<corr2>(Amd|Cor)\.\s?\d+))? (\s\(((?<cormonth2>\d{2})\/)?(?<coryear2>\d{4})\))? }x =~ i.hit[:code] /:[^\(]+\((?<buldate2>\d{2}\.\w{1,4}\.\d{4})\)/ =~ i.hit[:title] corr2&.sub! /\.\s?/, " " pref1 == pref2 && (!type1 || type1 == type2) && code2.include?(code1) && (!year || year == year2) && (!month1 || month1 == month2) && corr1 == corr2 && (!coryear1 || coryear1 == coryear2) && buldate1 == buldate2 && (!cormonth1 || cormonth1 == cormonth2) && (!ver1 || ver1 == ver2) end end