Sha256: 6814771463d0bee22853910d547d4cd4d867d6cd00e2c63026e1707afb56d706

Contents?: true

Size: 1.78 KB

Versions: 19

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Mihari
  module Clients
    #
    # urlscan.io API client
    #
    class Urlscan < Base
      #
      # @param [String] base_url
      # @param [String, nil] api_key
      # @param [Hash] headers
      # @param [Integer, nil] pagination_interval
      # @param [Integer, nil] timeout
      #
      def initialize(
        base_url = "https://urlscan.io",
        api_key:,
        headers: {},
        pagination_interval: Mihari.config.pagination_interval,
        timeout: nil
      )
        raise(ArgumentError, "api_key is required") if api_key.nil?

        headers["api-key"] = api_key

        super(base_url, headers: headers, pagination_interval: pagination_interval, timeout: timeout)
      end

      #
      # @param [String] q
      # @param [Integer, nil] size
      # @param [String, nil] search_after
      #
      # @return [Mihari::Structs::Urlscan::Response]
      #
      def search(q, size: nil, search_after: nil)
        params = { q: q, size: size, search_after: search_after }.compact
        Structs::Urlscan::Response.from_dynamic! get_json("/api/v1/search/", params: params)
      end

      #
      # @param [String] q
      # @param [Integer, nil] size
      # @param [Integer] pagination_limit
      #
      # @return [Enumerable<Mihari::Structs::Urlscan::Response>]
      #
      def search_with_pagination(q, size: nil, pagination_limit: Mihari.config.pagination_limit)
        search_after = nil

        Enumerator.new do |y|
          pagination_limit.times do
            res = search(q, size: size, search_after: search_after)

            y.yield res

            break unless res.has_more

            search_after = res.results.last.sort.join(",")

            sleep_pagination_interval
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mihari-7.3.2 lib/mihari/clients/urlscan.rb
mihari-7.3.1 lib/mihari/clients/urlscan.rb
mihari-7.3.0 lib/mihari/clients/urlscan.rb
mihari-7.2.0 lib/mihari/clients/urlscan.rb
mihari-7.1.3 lib/mihari/clients/urlscan.rb
mihari-7.1.2 lib/mihari/clients/urlscan.rb
mihari-7.1.1 lib/mihari/clients/urlscan.rb
mihari-7.1.0 lib/mihari/clients/urlscan.rb
mihari-7.0.5 lib/mihari/clients/urlscan.rb
mihari-7.0.4 lib/mihari/clients/urlscan.rb
mihari-7.0.3 lib/mihari/clients/urlscan.rb
mihari-7.0.2 lib/mihari/clients/urlscan.rb
mihari-7.0.1 lib/mihari/clients/urlscan.rb
mihari-7.0.0 lib/mihari/clients/urlscan.rb
mihari-6.3.0 lib/mihari/clients/urlscan.rb
mihari-6.2.0 lib/mihari/clients/urlscan.rb
mihari-6.1.0 lib/mihari/clients/urlscan.rb
mihari-6.0.0 lib/mihari/clients/urlscan.rb
mihari-5.7.2 lib/mihari/clients/urlscan.rb