Sha256: aed70eb148b5ba8cf679a3d24f25ebc9e4fdff1a24c933fbb38f73e26e2fbc1d

Contents?: true

Size: 1.65 KB

Versions: 19

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Mihari
  module Clients
    #
    # Onyphe API client
    #
    class Onyphe < Base
      PAGE_SIZE = 10

      # @return [String]
      attr_reader :api_key

      #
      # @param [String] base_url
      # @param [String, nil] api_key
      # @param [Hash] headers
      # @param [Integer] pagination_interval
      # @param [Integer, nil] timeout
      #
      def initialize(
        base_url = "https://www.onyphe.io",
        api_key:,
        headers: {},
        pagination_interval: Mihari.config.pagination_interval,
        timeout: nil
      )
        raise(ArgumentError, "api_key is required") if api_key.nil?

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

        @api_key = api_key
      end

      #
      # @param [String] query
      # @param [Integer] page
      #
      # @return [Mihari::Structs::Onyphe::Response]
      #
      def datascan(query, page: 1)
        params = { page: page, apikey: api_key }
        Structs::Onyphe::Response.from_dynamic! get_json("/api/v2/simple/datascan/#{query}", params: params)
      end

      #
      # @param [String] query
      # @param [Integer] pagination_limit
      #
      # @return [Enumerable<Mihari::Structs::Onyphe::Response>]
      #
      def datascan_with_pagination(query, pagination_limit: Mihari.config.pagination_limit)
        Enumerator.new do |y|
          (1..pagination_limit).each do |page|
            res = datascan(query, page: page)

            y.yield res

            break if res.total <= page * PAGE_SIZE

            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/onyphe.rb
mihari-7.3.1 lib/mihari/clients/onyphe.rb
mihari-7.3.0 lib/mihari/clients/onyphe.rb
mihari-7.2.0 lib/mihari/clients/onyphe.rb
mihari-7.1.3 lib/mihari/clients/onyphe.rb
mihari-7.1.2 lib/mihari/clients/onyphe.rb
mihari-7.1.1 lib/mihari/clients/onyphe.rb
mihari-7.1.0 lib/mihari/clients/onyphe.rb
mihari-7.0.5 lib/mihari/clients/onyphe.rb
mihari-7.0.4 lib/mihari/clients/onyphe.rb
mihari-7.0.3 lib/mihari/clients/onyphe.rb
mihari-7.0.2 lib/mihari/clients/onyphe.rb
mihari-7.0.1 lib/mihari/clients/onyphe.rb
mihari-7.0.0 lib/mihari/clients/onyphe.rb
mihari-6.3.0 lib/mihari/clients/onyphe.rb
mihari-6.2.0 lib/mihari/clients/onyphe.rb
mihari-6.1.0 lib/mihari/clients/onyphe.rb
mihari-6.0.0 lib/mihari/clients/onyphe.rb
mihari-5.7.2 lib/mihari/clients/onyphe.rb