Sha256: 6415b791af2e9eb0bc60e35c5d0ba37130a26b865784bff54714fbd2443c2bf4

Contents?: true

Size: 763 Bytes

Versions: 3

Compression:

Stored size: 763 Bytes

Contents

# frozen_string_literal: true

require "normalize_country"

module Mihari
  class Geolocation < ActiveRecord::Base
    belongs_to :artifact

    class << self
      include Dry::Monads[:result]

      #
      # Build Geolocation
      #
      # @param [String] ip
      # @param [Mihari::Enrichers::IPinfo] enricher
      #
      # @return [Mihari::Geolocation, nil]
      #
      def build_by_ip(ip, enricher: Enrichers::IPInfo.new)
        result = enricher.query_result(ip).bind do |res|
          value = res&.country_code
          if value.nil?
            Success nil
          else
            Success new(country: NormalizeCountry(value, to: :short), country_code: value)
          end
        end
        result.value_or nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mihari-5.6.1 lib/mihari/models/geolocation.rb
mihari-5.6.0 lib/mihari/models/geolocation.rb
mihari-5.5.0 lib/mihari/models/geolocation.rb