<commit_before>require 'timeout'
require 'open-uri'

module Facter::Util::PublicIpaddress
  def self.cache
    '/var/tmp/public_ip.fact.cache'
  end
  
  def self.can_connect? (url, wait_sec=2)
    Timeout::timeout(wait_sec) { open(url) }
    return true
    rescue Timeout::Error
      return false
    rescue
      return false
  end
  
  def self.get_ip (url, wait_sec=2, html=false)
    if can_connect?(url, wait_sec=wait_sec)
      response = open(url).read
      value = html ? response[/.*: ([^<]+)<.*/, 1] : response
      return unless response =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
      update_cache(value) if value
      value
    end
  end
  
  def self.update_cache (value)
    if value
      File.open(cache, 'w') do |f|
        f.write(value)
      end
    end
  end
  
  def self.get_cache
    if File.exists?(cache)
      File.read(cache)
    end
  end
end<commit_msg>Check value, not response<commit_after>require 'timeout'
require 'open-uri'

module Facter::Util::PublicIpaddress
  def self.cache
    '/var/tmp/public_ip.fact.cache'
  end
  
  def self.can_connect? (url, wait_sec=2)
    Timeout::timeout(wait_sec) { open(url) }
    return true
    rescue Timeout::Error
      return false
    rescue
      return false
  end
  
  def self.get_ip (url, wait_sec=2, html=false)
    if can_connect?(url, wait_sec=wait_sec)
      response = open(url).read
      value = html ? response[/.*: ([^<]+)<.*/, 1] : response
      return unless value =~ /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/
      update_cache(value) if value
      value
    end
  end
  
  def self.update_cache(value)
    if value
      File.open(cache, 'w') do |f|
        f.write(value)
      end
    end
  end
  
  def self.get_cache
    if File.exists?(cache)
      File.read(cache)
    end
  end
end
{'fraction_non_alphanumeric': 0.1389201349831271, 'fraction_numerical': 0.013498312710911136, 'mean_word_length': 2.536779324055666, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5090157', 'n_tokens_mistral': 657, 'n_tokens_neox': 654, 'n_words': 145}