Sha256: 128e110c358645ff4908baf7ffb73bc7f4778041b0c48dd176d209f0d6429331

Contents?: true

Size: 865 Bytes

Versions: 7

Compression:

Stored size: 865 Bytes

Contents

# frozen_string_literal: true

require "normalize_country"

module Mihari
  module Models
    #
    # Geolocation model
    #
    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.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
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mihari-6.3.0 lib/mihari/models/geolocation.rb
mihari-6.2.0 lib/mihari/models/geolocation.rb
mihari-6.1.0 lib/mihari/models/geolocation.rb
mihari-6.0.0 lib/mihari/models/geolocation.rb
mihari-5.7.2 lib/mihari/models/geolocation.rb
mihari-5.7.1 lib/mihari/models/geolocation.rb
mihari-5.7.0 lib/mihari/models/geolocation.rb