Sha256: 61fce307a8019086739ad52e04150026c98e383217cddd8aa4cf062cbe621ab5

Contents?: true

Size: 997 Bytes

Versions: 4

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

require "http"

module Mihari
  module Enrichers
    class IPInfo < Base
      # @return [Boolean]
      def valid?
        Mihari.config.ipinfo_api_key.nil?
      end

      private

      def configuration_keys
        %w[ipinfo_api_key]
      end

      class << self
        include Memist::Memoizable

        #
        # Query IPInfo
        #
        # @param [String] ip
        #
        # @return [Mihari::Structs::IPInfo::Response, nil]
        #
        def query(ip)
          headers = {}
          token = Mihari.config.ipinfo_api_key
          unless token.nil?
            headers[:authorization] = "Bearer #{token}"
          end

          begin
            res = HTTP.headers(headers).get("https://ipinfo.io/#{ip}/json")
            data = JSON.parse(res.body.to_s)

            Structs::IPInfo::Response.from_dynamic! data
          rescue HTTP::Error
            nil
          end
        end
        memoize :query
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mihari-4.1.2 lib/mihari/enrichers/ipinfo.rb
mihari-4.1.1 lib/mihari/enrichers/ipinfo.rb
mihari-4.1.0 lib/mihari/enrichers/ipinfo.rb
mihari-4.0.0 lib/mihari/enrichers/ipinfo.rb