handle

in api-reference-examples/ruby/te-tag-query/TETagQuery.rb [833:949]


  def handle(args, options)

    options['dryRun'] = false;
    options['indicatorTextFromStdin'] = false;

    postParams = {}

    # Local keystroke-saver for this enum
    names = ThreatExchange::TENet::POST_PARAM_NAMES

    loop do
      break if args.length == 0
      break unless args[0][0] == '-'
      option = args.shift

      if option == '-h'
        self.usage(0)
      elsif option == '--help'
        self.usage(0)

      elsif option == '--dry-run'
        options['dryRun'] = true

      elsif option == '-I'
        options['indicatorTextFromStdin'] = true;
      elsif option == '-i' || option == '--indicator'
        self.usage(1) unless args.length >= 1
        postParams[names[:indicator]] = args.shift;

      elsif option == '-t' || option == '--type'
        self.usage(1) unless args.length >= 1
        postParams[names[:type]] = args.shift;

      elsif option == '--tags'
        self.usage(1) unless args.length >= 1
        postParams[names[:tags]] = args.shift;

      else
        handled, args = self.commonPosterOptionCheck(option, args, postParams)
        if not handled
          $stderr.puts "#{$0} #{@verbName}: unrecognized  option #{option}"
          exit 1
        end
      end
    end

    if args.length > 0
      $stderr.puts "#{$0} #{@verbName}: extraneous argument(s) \"#{args.join(' ')}\"."
      exit 1
    end

    if options['indicatorTextFromStdin']
      unless postParams[names[:indicator]].nil?
        $stderr.puts "#{$0} #{@verbName}: only one of -I and -i must be supplied."
        exit 1
      end

      $stdin.readlines.each do |line|
        postParams[names[:indicator]] = line.chomp
        self.submitSingle(
          postParams,
          verbose: options['verbose'],
          showURLs: options['showURLs'],
          dryRun: options['dryRun'],
        )
      end
    else
      if postParams[names[:indicator]].nil?
        $stderr.puts "#{$0} #{@verbName}: exactly one of -I and -i must be supplied."
        exit 1
      end
      self.submitSingle(
        postParams,
        verbose: options['verbose'],
        showURLs: options['showURLs'],
        dryRun: options['dryRun'],
      )
    end

  end # SubmitHandler.handle

  # ----------------------------------------------------------------
  def submitSingle(
    postParams,
    verbose: false,
    showURLs: false,
    dryRun: false)

    validationErrorMessage, response_body, response_code = ThreatExchange::TENet::submitThreatDescriptor(
      postParams,
      showURLs: showURLs,
      dryRun: dryRun)

    unless validationErrorMessage.nil?
      $stderr.puts validationErrorMessage
      exit 1
    end

    puts response_body

    if response_code != 200
      exit 1
    end
  end # SubmitHandler.submitSingle
end # class SubmitHandler

# ================================================================
# NOTE: SubmitHandler and UpdateHandler have a lot of the same code but also
# several differences. I found it simpler (albeit more verbose) to duplicate
# rather than do an abstract-and-override refactor.

class UpdateHandler < AbstractPostSubcommandHandler
  # ----------------------------------------------------------------
  def initialize(verbName)
    super(verbName)
  end