module Relaton::Cli

Constants

VERSION

Public Class Methods

parse_xml(doc) click to toggle source

@param content [Nokogiri::XML::Document] @return [RelatonBib::BibliographicItem,

RelatonIsoBib::IsoBibliongraphicItem]
# File lib/relaton/cli.rb, line 60
def parse_xml(doc)
  if (proc = Cli.processor(doc))
    proc.from_xml(doc.to_s)
  else
    RelatonBib::XMLParser.from_xml(doc.to_s)
  end
end
processor(doc) click to toggle source

@param doc [Nokogiri::XML::Element] @return [RelatonIso::Processor, RelatonIec::Processor,

RelatonNist::Processor, RelatonIetf::Processot,
RelatonItu::Processor, RelatonGb::Processor,
RelatonOgc::Processor, RelatonCalconnect::Processor]
# File lib/relaton/cli.rb, line 73
def processor(doc)
  docid = doc.at "docidentifier"
  proc = get_proc docid
  return proc if proc

  Relaton::Registry.instance.by_type(docid&.text&.match(/^\w+/)&.to_s)
end
relaton(dir) click to toggle source

Relaton

Based on current setup, we need to initiate a Db instance to register all of it's supported processor backends. To make it easier we have added it as a class method so we can use this whenever necessary.

@param dir [String, nil] @return [Relaton::Db]

# File lib/relaton/cli.rb, line 53
def relaton(dir)
  RelatonDb.instance.db dir
end
start(arguments) click to toggle source
# File lib/relaton/cli.rb, line 40
def start(arguments)
  Relaton::Cli::Command.start(arguments)
end

Private Class Methods

get_proc(docid) click to toggle source

@param doc [Nokogiri::XML::Element] @return [RelatonIso::Processor, RelatonIec::Processor,

RelatonNist::Processor, RelatonIetf::Processot,
RelatonItu::Processor, RelatonGb::Processor,
RelatonOgc::Processor, RelatonCalconnect::Processor]
# File lib/relaton/cli.rb, line 88
def get_proc(docid)
  return unless docid && docid[:type]

  Relaton::Registry.instance.by_type(docid[:type])
end

Private Instance Methods

fetch_document(code, options) click to toggle source

@param code [String] @param options [Hash] @option options [String] :type @option options [String, NilClass] :format @option options [Integer, NilClass] :year @return [String, nil]

# File lib/relaton/cli/command.rb, line 178
def fetch_document(code, options) # rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/AbcSize
  year = options[:year]&.to_s
  if (processor = Relaton::Registry.instance.by_type options[:type]&.upcase)
    doc = Relaton.db.fetch_std code, year, processor.short, options.dup
  elsif options[:type] then return
  else doc = Relaton.db.fetch(code, year, options.dup)
  end
  return "No matching bibliographic entry found" unless doc

  serialize doc, options[:format]
rescue RelatonBib::RequestError => e
  e.message
end
registered_types() click to toggle source
# File lib/relaton/cli/command.rb, line 207
def registered_types
  @registered_types ||=
    Relaton::Registry.instance.processors.each.map { |_n, pr| pr.prefix }
end
serialize(doc, format) click to toggle source

@param doc [RelatonBib::BibliographicItem] @param format [String] @return [String]

# File lib/relaton/cli/command.rb, line 195
def serialize(doc, format)
  case format
  when "yaml", "yml" then doc.to_hash.to_yaml
  when "bibtex" then doc.to_bibtex
  else doc.to_xml
  end
end
supported_type_message() click to toggle source
# File lib/relaton/cli/command.rb, line 203
def supported_type_message
  ["Recognised types:", registered_types.sort.join(", ")].join(" ")
end