class PrimoCentralCounter::Cli

Public Class Methods

new(argv = []) click to toggle source
# File lib/primo_central_counter/cli.rb, line 9
def initialize(argv = [])
  @options = {
    index_field: "sid",
    log_level: "error",
    on_campus: true
  }

  option_parser(@options).parse!(argv)

  @logger = ::Logger.new(STDERR).tap do |_new_logger|
    _new_logger.level = ::Logger.const_get @options[:log_level].upcase
  end

  if @options[:base_url] && @options[:institution] && !@options[:help] && !@options[:version]
    call
  elsif @options[:version]
    puts "#{PrimoCentralCounter::VERSION}"
  else
    puts option_parser.help
  end
end

Public Instance Methods

call() click to toggle source
# File lib/primo_central_counter/cli.rb, line 31
def call
  if PrimoCentralCounter::ConnectionChecker.call(@options[:base_url], logger: @logger)
    searcher_url = PrimoCentralCounter::SoapDiscoverer.call(@options[:base_url], logger: @logger)
    queries = [("a".."z").to_a, (0..9).to_a.map(&:to_s)].flatten(1).map { |_char| "#{_char}*" }
    result = PrimoCentralCounter::Counter.call(searcher_url, queries, @options.merge(logger: @logger))
    sum = result.inject(0) do |_sum, (_query, _count)|
      _sum + _count
    end

    puts sum
  else
    log_error(@logger, "Cannot connect to Primo Central! Please check the given url and if you are allowed to access the API.")
    log_error(@logger, "The following primo backoffice settings control the API access privileges:")
    log_error(@logger, "- Advanced Configuration > All Mapping Tables > WS and XS IP")
    log_error(@logger, "- Deploy All > All Client IP Ranges (WS and XS IP mapping table)")
  end
end

Private Instance Methods

log_error(logger, message) click to toggle source
# File lib/primo_central_counter/cli.rb, line 52
def log_error(logger, message)
  logger.error(message) if logger
end
option_parser(result = {}) click to toggle source
# File lib/primo_central_counter/cli.rb, line 56
def option_parser(result = {})
  PrimoCentralCounter::OptionParser.new(nil, 38) do |_option_parser|
    _option_parser.banner = "Usage: primo_central_counter [options]\n\n"

    _option_parser.on("-u", "--base-url=BASE_URL", "Primo base url (e.g. http://primo.kobv.de) *required*") do |_value|
      result[:base_url] = _value
    end

    _option_parser.on("-i", "--institution=INSTITUTION", "Institution (e.g. PAD) *required*") do |_value|
      result[:institution] = _value
    end

    _option_parser.on("-c", "--on-campus=ON_CAMPUS", "Set the on_campus flag (default: #{@options[:on_campus]})") do |_value|
      result[:index_field] = ["true", "yes", "ja", "1"].include?(value.to_s.downcase) ? true : false
    end
    
    _option_parser.on("-f", "--index-field=INDEX_FIELD", "Index field to use (default: #{@options[:index_field]})") do |_value|
      result[:index_field] = _value
    end

    _option_parser.on("--log-level=LOG_LEVEL", "Enable logging (e.g. info or debug)") do |_value|
      result[:log_level] = _value
    end

    _option_parser.on("-v", "--version", "Display version") do |_value|
      result[:version] = true
    end

    _option_parser.on("-h", "--help", "Display usage informations") do
      result[:help] = true
    end
  end
end