Sha256: 60e12577ada11b51ae41de374053c4f071a19009f4bbd247d5278d6ef42457e0

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Mihari
  module Enrichers
    #
    # IPInfo enricher
    #
    class IPInfo < Base
      # @return [String, nil]
      attr_reader :api_key

      #
      # @param [Hash, nil] options
      # @param [String, nil] api_key
      #
      def initialize(options: nil, api_key: nil)
        @api_key = api_key || Mihari.config.ipinfo_api_key

        super(options: options)
      end

      def configuration_keys
        %w[ipinfo_api_key]
      end

      #
      # Query IPInfo
      #
      # @param [String] ip
      #
      # @return [Mihari::Structs::IPInfo::Response, nil]
      #
      def call(ip)
        url = "https://ipinfo.io/#{ip}/json"
        res = http.get(url)
        data = JSON.parse(res.body.to_s)

        Structs::IPInfo::Response.from_dynamic! data
      end

      private

      def headers
        authorization = api_key.nil? ? nil : "Bearer #{api_key}"
        { authorization: authorization }.compact
      end

      def http
        HTTP::Factory.build headers: headers, timeout: timeout
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mihari-5.7.0 lib/mihari/enrichers/ipinfo.rb