<commit_before># Fact that sucks down all the metadata offered by Digital Ocean other
# than the user data (see note below).
#
# Based on the code written for AWS/EC2 facts that was later merged into
# mainline facter, credit:
# http://projects.puppetlabs.com/projects/1/wiki/Amazon_Ec2_Patterns


require 'open-uri'
require 'timeout'

def metadata(id = "")
  open("http://169.254.169.254/metadata/v1/#{id||=''}").read.
    split("\n").each do |o|
    key = "#{id}#{o.gsub(/\=.*$/, '/')}"
    if key[-1..-1] != '/'
      value = open("http://169.254.169.254/metadata/v1/#{key}").read.
        split("\n")
      value = value.size>1 ? value : value.first
      symbol = "digital_ocean_#{key.gsub(/\-|\//, '_')}".to_sym
  
      # We exclude the user data. This is intentional, the user data often has a
      # bad habit of triggering issues inside Facter due to bad parsing once saved into
      # YAML files and is best left out. You can always pull it on demand/as needed
      # with curl http://169.254.169.254/metadata/v1/user-data if you ever do need it
      unless key == "user-data"
        Facter.add(symbol) { setcode { value } }
      end

    else
      metadata(key)
    end
  end
end

begin
  Timeout::timeout(1) { metadata }
rescue
  puts "Digital Ocean Metadata Unavailable"
end

# vim: ai ts=2 sts=2 et sw=2 ft=ruby<commit_msg>Compatibility fix for Ruby 1.8 series.<commit_after># Fact that sucks down all the metadata offered by Digital Ocean other
# than the user data (see note below).
#
# Based on the code written for AWS/EC2 facts that was later merged into
# mainline facter, credit:
# http://projects.puppetlabs.com/projects/1/wiki/Amazon_Ec2_Patterns


require 'open-uri'
require 'timeout'

def metadata(id = "")
  open("http://169.254.169.254/metadata/v1/#{id||=''}").read.
    split("\n").each do |o|
    key = "#{id}#{o.gsub(/\=.*$/, '/')}"
    if key[-1..-1] != '/'
      value = open("http://169.254.169.254/metadata/v1/#{key}").read.
        split("\n")
      value = value.size>1 ? value : value.first
      symbol = "digital_ocean_#{key.gsub(/\-|\//, '_')}".to_sym
  
      # We exclude the user data. This is intentional, the user data often has a
      # bad habit of triggering issues inside Facter due to bad parsing once saved into
      # YAML files and is best left out. You can always pull it on demand/as needed
      # with curl http://169.254.169.254/metadata/v1/user-data if you ever do need it
      unless key == "user-data"
        Facter.add(symbol) { setcode { value } }
      end

    else
      metadata(key)
    end
  end
end

begin
  Timeout::timeout(1) { metadata }
rescue Exception
  puts "Digital Ocean Metadata Unavailable"
end

# vim: ai ts=2 sts=2 et sw=2 ft=ruby
{'fraction_non_alphanumeric': 0.1438532110091743, 'fraction_numerical': 0.03669724770642202, 'mean_word_length': 2.8340365682137834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '489073', 'n_tokens_mistral': 975, 'n_tokens_neox': 907, 'n_words': 289}