Sha256: 3d8fa8b4c9264583b88d9dc417d0a32232d7b0d3e170ef3ba9ac1d933bfb469b

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Mihari
  module Clients
    class GreyNoise < Base
      PAGE_SIZE = 10_000

      #
      # @param [String] base_url
      # @param [String, nil] api_key
      # @param [Hash] headers
      # @param [Integer, nil] interval
      #
      def initialize(base_url = "https://api.greynoise.io", api_key:, headers: {}, interval: nil)
        raise(ArgumentError, "'api_key' argument is required") unless api_key

        headers["key"] = api_key
        super(base_url, headers: headers, interval: interval)
      end

      #
      # GNQL (GreyNoise Query Language) is a domain-specific query language that uses Lucene deep under the hood
      #
      # @param [String] query GNQL query string
      # @param [Integer] size Maximum amount of results to grab
      # @param [Integer, nil] scroll Scroll token to paginate through results
      #
      # @return [Hash]
      #
      def gnql_search(query, size: PAGE_SIZE, scroll: nil)
        params = { query: query, size: size, scroll: scroll }.compact
        res = get("/v2/experimental/gnql", params: params)
        Structs::GreyNoise::Response.from_dynamic! JSON.parse(res.body.to_s)
      end

      #
      # @param [String] query
      # @param [Integer] size
      # @param [Integer] pagination_limit
      #
      # @return [Enumerable<Structs::GreyNoise::Response>]
      #
      def gnql_search_with_pagination(query, size: PAGE_SIZE, pagination_limit: Mihari.config.pagination_limit)
        scroll = nil

        Enumerator.new do |y|
          pagination_limit.times do
            res = gnql_search(query, size: size, scroll: scroll)

            y.yield res

            scroll = res.scroll
            break if scroll.nil?

            sleep_interval
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mihari-5.4.7 lib/mihari/clients/greynoise.rb
mihari-5.4.6 lib/mihari/clients/greynoise.rb
mihari-5.4.5 lib/mihari/clients/greynoise.rb
mihari-5.4.4 lib/mihari/clients/greynoise.rb
mihari-5.4.3 lib/mihari/clients/greynoise.rb