class BerkeleyLibrary::TIND::Export::ExportCommand

rubocop:disable Metrics/ClassLength

Constants

DEFAULT_FORMAT
FORMATS
OPTS

Attributes

options[R]
out[R]

Public Class Methods

new(*args, out: $stdout) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 20
def initialize(*args, out: $stdout)
  @options = ExportCommand.parse_options(args)
  @out = out
end

Private Class Methods

configure!(opts) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 86
def configure!(opts)
  configure_env(opts)
  BerkeleyLibrary::TIND::Config.base_uri = opts[:tind_base_url] if opts[:tind_base_url]
  BerkeleyLibrary::TIND::Config.api_key = opts[:api_key] if opts[:api_key]
  BerkeleyLibrary::Logging.logger = configure_logger(opts)
end
configure_env(opts) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 100
def configure_env(opts)
  return unless (env_file = opts[:env_file])

  warn "Reading environment from #{env_file}" if opts[:verbose]

  require 'dotenv'
  Dotenv.load(env_file)
end
configure_logger(opts) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 93
def configure_logger(opts)
  return Logger.new(File::NULL) unless opts[:verbose]

  # TODO: simpler log format? different log levels?
  BerkeleyLibrary::Logging::Loggers.new_readable_logger($stderr).tap { |logger| logger.level = Logger::DEBUG }
end
ensure_format(opts) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 109
def ensure_format(opts)
  fmt = opts[:format] || (File.extname(opts[:outfile]).sub(/^\./, '') if opts[:outfile])
  return DEFAULT_FORMAT unless fmt

  ExportFormat.ensure_format(fmt)
end
env_file_path(env_file_opt) click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# File lib/berkeley_library/tind/export/export_command.rb, line 132
def env_file_path(env_file_opt)
  File.realpath(env_file_opt || File.join(Dir.pwd, '.env'))
end
option_parser(opts = {}) click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# File lib/berkeley_library/tind/export/export_command.rb, line 117
def option_parser(opts = {})
  OptionParser.new do |p|
    p.summary_indent = ' '
    p.on('-f', *OPTS[:f]) { |fmt| opts[:format] = fmt }
    p.on('-o', *OPTS[:o]) { |out| opts[:outfile] = out }
    p.on('-l', *OPTS[:l]) { opts[:list] = true }
    p.on('-u', *OPTS[:u]) { |url| opts[:tind_base_url] = url }
    p.on('-k', *OPTS[:k]) { |k| opts[:api_key] = k }
    p.on('-e', *OPTS[:e]) { |e| opts[:env_file] = env_file_path(e) }
    p.on('-v', *OPTS[:v]) { opts[:verbose] = true }
    p.on('-h', *OPTS[:h]) { print_usage_and_exit! }
  end
end
parse_options(argv) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 66
def parse_options(argv)
  {}.tap do |opts|
    option_parser(opts).parse!(argv)
    opts[:collection] = argv.pop
    opts[:format] = ensure_format(opts)
    validate!(opts)
    configure!(opts)
  end
rescue StandardError => e
  print_usage_and_exit!($stderr, EX_USAGE, e.message)
end
print_usage_and_exit!(out = $stdout, exit_code = EX_OK, msg = nil) click to toggle source
summarize_options() click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 160
def summarize_options
  option_parser.summarize.join('  ')
end
usage() click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 142
          def usage
            <<~USAGE
              Usage: tind-export [options] COLLECTION

              Options:
                #{summarize_options}

              Examples:
                1. list collections
                   tind-export --list-collections
                2. export a collection as an OpenOffice/LibreOffice spreadsheet
                   tind-export -o lincoln-papers.ods 'Abraham Lincoln Papers'
                3. export a collection as an OpenOffice/LibreOffice spreadsheet in exploded XML format,
                   where `lincoln-papers` is a directory
                   tind-export -v -f ODS -o lincoln-papers 'Abraham Lincoln Papers'
            USAGE
          end
validate!(opts) click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 80
def validate!(opts)
  return if opts[:list]
  raise ArgumentError, 'Collection not specified' unless opts[:collection]
  raise ArgumentError, 'OpenOffice/LibreOffice export requires a filename' if opts[:format] == ExportFormat::ODS && !opts[:outfile]
end

Public Instance Methods

execute!() click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 25
def execute!
  return list_collections if options[:list]

  export_collection
rescue StandardError => e
  warn(e)
  warn(e.backtrace.join("\n")) if e.backtrace && options[:verbose]

  exit(EX_SOFTWARE)
end

Private Instance Methods

export_collection() click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 42
def export_collection
  BerkeleyLibrary::TIND::Export.export(
    options[:collection],
    options[:format],
    options[:outfile] || out
  )
end
list_collections() click to toggle source
# File lib/berkeley_library/tind/export/export_command.rb, line 38
def list_collections
  BerkeleyLibrary::TIND::API::Collection.each_collection { |c| out.puts "#{c.nb_rec}\t#{c.name}" }
end