Class: PrimoCentralCounter::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/primo_central_counter/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv = []) ⇒ Cli

Returns a new instance of Cli.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#callObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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