Sha256: 5dea8b6a840a608d1552c0bee6b80e14bf6a6ac1a693745631f5438be53aca63

Contents?: true

Size: 1023 Bytes

Versions: 5

Compression:

Stored size: 1023 Bytes

Contents

# frozen_string_literal: true

require "net/https"

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
            url = "https://ipinfo.io/#{ip}/json"
            res = HTTP.get(url, headers: headers)
            data = JSON.parse(res.body.to_s)

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mihari-4.5.0 lib/mihari/enrichers/ipinfo.rb
mihari-4.4.1 lib/mihari/enrichers/ipinfo.rb
mihari-4.4.0 lib/mihari/enrichers/ipinfo.rb
mihari-4.3.0 lib/mihari/enrichers/ipinfo.rb
mihari-4.2.0 lib/mihari/enrichers/ipinfo.rb